gpt4 book ai didi

java - tilepane javafx 中相同大小的按钮

转载 作者:行者123 更新时间:2023-12-02 01:37:54 27 4
gpt4 key购买 nike

大家好,我对 GUI 完全陌生......到目前为止,我所有的程序都是基于文本的。我刚刚开始使用 JAVAFX。
我想重新创建一个电话键盘。它应该有 4 行,每行 3 个按钮,每个按钮都应该有其相关的数字及其字母。确实如此。但按钮的大小不一样。

enter image description here

我的意思是......因为我使用的是tilePane,所以按钮占据相同数量的像素(或者至少我猜是这样),但按钮的实际可见大小是不同的,因为每个按钮只占用显示其内容所需的大小。我将按钮存储在数组中。有没有办法让它们都和最大的一样大小?

public class PhoneKeyboard extends Application
{

Button buttons [];
@Override
public void start(Stage stage) throws Exception
{
// Create the set of necessary buttons.
buttons = new Button[12];
fillButtons(buttons);
//Create the grid for the buttons.
TilePane pane = new TilePane();
pane.setPadding(new Insets(10,10,10,10));
pane.setPrefColumns(3);
pane.setMaxWidth(Region.USE_PREF_SIZE);// Use the pref size as max size to make sure there will be the expected size
pane.setVgap(5.0);// to set ome spacing between each tile of the pane.
pane.setHgap(5.0);
//Put the buttons on the pane (layout);
pane.getChildren().addAll(buttons);
// JavaFX must have a Scene (window content) inside a Stage (window)
Scene scene = new Scene(new StackPane(pane), 300,400);// In order to use the pfer size of the pane, the tile Pane doesn"t have to be the root in the scene. Therefore we created a scene with a stack pane containing our tile pane as the root.
stage.setTitle("Keyboard");
stage.setScene(scene);
// Show the Stage (window)
stage.show();
}

最佳答案

按钮具有最大宽度和最大高度,其大小足以容纳按钮的内容。大多数布局(包括 TilePane)不会使子节点大于其最大尺寸。

如果您希望每个按钮都能够变大,请在调用 fillButtons 后设置其最大宽度和高度:

for (Button button : buttons) {
button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}

关于java - tilepane javafx 中相同大小的按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54953627/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com