使 JavaFX ToolBar 紧贴其项目
Fit JavaFX ToolBar snugly around its items
JavaFX 工具栏始终在其子项周围填充 space。有没有办法控制它,所以没有 space 到侧面(第一个项目,最后一个项目在另一边),顶部和底部?
只需将工具栏的内边距设置为 0px。例如:
toolbar.setStyle("-fx-padding: 0px;");
建议您在外部样式中设置任何样式sheet,而不是在代码中。
没有填充:
有填充:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class Girlfriend extends Application {
@Override
public void start(Stage stage) throws Exception {
ToolBar toolbar = new ToolBar(
new Button("Open Fridge"),
new Button("Get Beer"),
new Button("Repeat")
);
toolbar.setStyle("-fx-padding: 0px;");
stage.setScene(new Scene(toolbar));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
其他问题更新
is there a way to specifically control what padding gets placed and not? For example, I want to keep the top padding on, but get rid of the bottom one etc.
是的,参考JavaFX CSS reference。
-fx-padding
<size> | <size> <size> <size> <size>
A sets of four padding values, separated by commas. For each item in the series, a single padding value means that all padding are the same; and if a set of four padding values is specified, they are used for the top, right, bottom, and left edges of the region, in that order.
.tool-bar
CSS class 的默认填充可以在 JRE 的 jfxrt.jar
中的 modena.css
中找到,并为 Java 8u40 为:
-fx-padding: 0.416667em 0.5em 0.416667em 0.5em; /* 5 6 5 6 */
因此,要保留顶部、右侧和左侧的内边距,但移除底部内边距,请使用以下值:
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
可能应该是 CSS 风格 sheet,所以你真的会这样做:
toolbar.getStyleClass().add("flush-bottom"); // hmm unfortunate name.
自定义样式中定义样式的地方sheet为:
.flush-bottom {
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
}
JavaFX 工具栏始终在其子项周围填充 space。有没有办法控制它,所以没有 space 到侧面(第一个项目,最后一个项目在另一边),顶部和底部?
只需将工具栏的内边距设置为 0px。例如:
toolbar.setStyle("-fx-padding: 0px;");
建议您在外部样式中设置任何样式sheet,而不是在代码中。
没有填充:
有填充:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.stage.Stage;
public class Girlfriend extends Application {
@Override
public void start(Stage stage) throws Exception {
ToolBar toolbar = new ToolBar(
new Button("Open Fridge"),
new Button("Get Beer"),
new Button("Repeat")
);
toolbar.setStyle("-fx-padding: 0px;");
stage.setScene(new Scene(toolbar));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
其他问题更新
is there a way to specifically control what padding gets placed and not? For example, I want to keep the top padding on, but get rid of the bottom one etc.
是的,参考JavaFX CSS reference。
-fx-padding
<size> | <size> <size> <size> <size>
A sets of four padding values, separated by commas. For each item in the series, a single padding value means that all padding are the same; and if a set of four padding values is specified, they are used for the top, right, bottom, and left edges of the region, in that order.
.tool-bar
CSS class 的默认填充可以在 JRE 的 jfxrt.jar
中的 modena.css
中找到,并为 Java 8u40 为:
-fx-padding: 0.416667em 0.5em 0.416667em 0.5em; /* 5 6 5 6 */
因此,要保留顶部、右侧和左侧的内边距,但移除底部内边距,请使用以下值:
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
可能应该是 CSS 风格 sheet,所以你真的会这样做:
toolbar.getStyleClass().add("flush-bottom"); // hmm unfortunate name.
自定义样式中定义样式的地方sheet为:
.flush-bottom {
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
}