JavaFX按钮大小相同 [英] JavaFX buttons with same size

查看:169
本文介绍了JavaFX按钮大小相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同大小的这些按钮:

I have these buttons with different size:

图片

如何使所有按钮的大小相同?

How I can make all buttons with same with size?

推荐答案

这取决于按钮所在的布局。例如,如果将所有按钮添加到GridPane或BorderPane中,则必须指定每个按钮宽度以对应于某个变量。在下面的示例中,我将所有按钮包装在VBox中,设置VBox首选项宽度并将所有按钮最小宽度绑定到它:

It depends on layout where the button is located. For example, if you add all the buttons into GridPane or BorderPane, you have to specify each button width to correspond to certain variable. In the following example I wrap all buttons inside VBox, set VBox preference width and tie up all buttons minimum width to it:

VBox vBox = new VBox();
vBox.setPrefWidth(100);

Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");

btn1.setMinWidth(vBox.getPrefWidth());
btn2.setMinWidth(vBox.getPrefWidth());

vBox.getChildren().addAll(btn1, btn2);

值得一提的是,有两种方法可以指定按钮大小。您可以在java代码中执行此操作,也可以在javafx .fxml文件中指定它。上面的方法是java代码实现的一个例子。

It is also worth to mention that there are two ways to specify the button size. You can do it in the java code or specify it in javafx .fxml file. The above method is an example for java code implementation.

你也可以松开一个按钮的最大尺寸,这样它就会增长到填充可用空间(与大多数节点不同,默认情况下,按钮节点将其最大大小限制为其首选大小,因此通常不会增长以填充可用空间)。有关调整和对齐节点的提示更详细地解释了这一点。

You can also unclamp a button's maximum dimensions so it will grow to fill the available space (unlike most nodes, by default a button node has it's max size clamped to it's preferred size so it doesn't usually grow to fill available space). An Oracle tutorial on Tips for Sizing and Aligning Nodes explains this in more detail.

VBox vBox = new VBox();

Button btn1 = new Button("Short");
Button btn2 = new Button("Super Long Button");

btn1.setMaxWidth(Double.MAX_VALUE);
btn2.setMaxWidth(Double.MAX_VALUE);

vBox.getChildren().addAll(btn1, btn2);

这篇关于JavaFX按钮大小相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆