tilepane javafx 中相同大小的按钮

Buttons of the same size in tilepane javafx

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

我的意思是……因为我使用的是 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();
}

Button 有最大宽度和最大高度,刚好足以容纳 Button 的内容。大多数布局,包括 TilePane,都不会使子节点大于它们的最大尺寸。

如果您希望每个按钮都能够变大,请在调用 fillButtons:

后设置其最大宽度和高度
for (Button button : buttons) {
    button.setMaxSize(Double.MAX_VALUE, Double.MAX_VALUE);
}