A JavaFX toolbar always has padding space around its child items. Is there a way to control this so there's no space to the side (first item, and last item at the other edge), top, and bottom?
JavaFX工具栏始终在其子项周围具有填充空间。有没有办法控制这个,所以侧面没有空间(第一个项目,另一个边缘的最后一个项目),顶部和底部?
1 个解决方案
#1
Just set the padding for the toolbar to 0px. For instance:
只需将工具栏的填充设置为0px即可。例如:
toolbar.setStyle("-fx-padding: 0px;");
It is recommended you set any styles in an external stylesheet rather than in code.
建议您在外部样式表中而不是在代码中设置任何样式。
Without padding:
With padding:
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);
}
}
Update for Additional Questions
其他问题的更新
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.
有没有办法专门控制什么填充放置而不是?例如,我想保持顶部填充,但摆脱底部填充等。
Yes, refer to the JavaFX CSS reference.
是的,请参阅JavaFX CSS参考。
-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.
一组四个填充值,以逗号分隔。对于系列中的每个项目,单个填充值意味着所有填充都相同;如果指定了一组四个填充值,它们将按顺序用于区域的顶部,右侧,底部和左侧边缘。
The default padding for a .tool-bar
CSS class can be found in modena.css
in your jfxrt.jar
from your JRE and is defined for Java 8u40 as:
.tool-bar CSS类的默认填充可以在jRE的jfxrt.jar中的modena.css中找到,并且在Java 8u40中定义为:
-fx-padding: 0.416667em 0.5em 0.416667em 0.5em; /* 5 6 5 6 */
So, to keep the top, right and left padding, but remove the bottom padding, use a value of:
因此,要保持顶部,右侧和左侧填充,但删除底部填充,请使用以下值:
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
Which should probably be in a CSS style sheet, so you would really do something like:
哪个应该在CSS样式表中,所以你真的应该这样做:
toolbar.getStyleClass().add("flush-bottom"); // hmm unfortunate name.
Where you define the style in a custom stylesheet as:
您在自定义样式表中将样式定义为:
.flush-bottom {
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
}
#1
Just set the padding for the toolbar to 0px. For instance:
只需将工具栏的填充设置为0px即可。例如:
toolbar.setStyle("-fx-padding: 0px;");
It is recommended you set any styles in an external stylesheet rather than in code.
建议您在外部样式表中而不是在代码中设置任何样式。
Without padding:
With padding:
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);
}
}
Update for Additional Questions
其他问题的更新
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.
有没有办法专门控制什么填充放置而不是?例如,我想保持顶部填充,但摆脱底部填充等。
Yes, refer to the JavaFX CSS reference.
是的,请参阅JavaFX CSS参考。
-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.
一组四个填充值,以逗号分隔。对于系列中的每个项目,单个填充值意味着所有填充都相同;如果指定了一组四个填充值,它们将按顺序用于区域的顶部,右侧,底部和左侧边缘。
The default padding for a .tool-bar
CSS class can be found in modena.css
in your jfxrt.jar
from your JRE and is defined for Java 8u40 as:
.tool-bar CSS类的默认填充可以在jRE的jfxrt.jar中的modena.css中找到,并且在Java 8u40中定义为:
-fx-padding: 0.416667em 0.5em 0.416667em 0.5em; /* 5 6 5 6 */
So, to keep the top, right and left padding, but remove the bottom padding, use a value of:
因此,要保持顶部,右侧和左侧填充,但删除底部填充,请使用以下值:
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
Which should probably be in a CSS style sheet, so you would really do something like:
哪个应该在CSS样式表中,所以你真的应该这样做:
toolbar.getStyleClass().add("flush-bottom"); // hmm unfortunate name.
Where you define the style in a custom stylesheet as:
您在自定义样式表中将样式定义为:
.flush-bottom {
-fx-padding: 0.416667em 0.5em 0em 0.5em; /* 5 6 0 6 */
}