为什么我的 JavaFX 按钮间距不均匀?

dex*_*ter 3 java javafx

我是 JavaFX 新手,尝试构建一个 GUI 程序,当您单击餐厅的桌子时,该程序会显示该桌子的账单。表格按钮之间的间距关闭,我不确定为什么。

在此输入图像描述

我的程序的 GUI 类:

package restaurantBillingProgram;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.geometry.Pos;

public class BillingGUI extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create grid pane
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(5);
        pane.setVgap(5);

        // Label
        pane.add(new Label("Generate bill"), 1, 0);

        // Buttons
        Button btT1 = new Button("Table 1");
        pane.add(btT1, 0, 1);
        btT1.setOnAction(e - > Billing.generateT1());

        Button btT2 = new Button("Table 2");
        pane.add(btT2, 1, 1);
        btT2.setOnAction(e - > Billing.generateT2());

        Button btT3 = new Button("Table 3");
        pane.add(btT3, 2, 1);
        btT3.setOnAction(e - > Billing.generateT3());

        // Create scene and place in stage
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setTitle("Restaurant Billing Program");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    // Main method
    public static void main(String[] args) {
        launch(args);
    }
    }
Run Code Online (Sandbox Code Playgroud)

Jim*_*son 5

来自 Javadoc:

  • 行/列大小调整

    默认情况下,行和列的大小将适合其内容;一根柱子的宽度足以容纳最宽的孩子,......

第 0 行第 1 列中的标签强制该列变宽。

您可能希望标签居中并跨越所有 3 列


Sed*_*ick 5

在进行布局时,使用pane.setGridLinesVisible(true). 这应该只在调试期间使用。它对于像您当前的情况这样的情况非常有用。正如@Jim Garrison 指出的,你Label造成了这个问题:

问题:

在此输入图像描述

解决此问题的一种方法是让文本Label跨越所有列并将Label's文本居中。

使固定:

在此输入图像描述

关键代码:

label.setMaxWidth(Double.MAX_VALUE);
label.setAlignment(Pos.CENTER);
pane.add(label, 0, 0, 3, 1);// Look at the following link to see how this add method works. https://openjfx.io/javadoc/11/javafx.graphics/javafx/scene/layout/GridPane.html#add(javafx.scene.Node,int,int,int,int)
Run Code Online (Sandbox Code Playgroud)

完整代码:

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.scene.control.Label;
import javafx.scene.control.Button;
import javafx.geometry.Pos;

public class BillingGUI extends Application {

    @Override
    public void start(Stage primaryStage) {

        // Create grid pane
        GridPane pane = new GridPane();
        pane.setAlignment(Pos.CENTER);
        pane.setHgap(5);
        pane.setVgap(5);
        pane.setGridLinesVisible(true);//Use for debugging only!!!!

        // Label
        Label label = new Label("Generate bill");
        label.setMaxWidth(Double.MAX_VALUE);
        label.setAlignment(Pos.CENTER);
        pane.add(label, 0, 0, 3, 1);

        // Buttons
        Button btT1 = new Button("Table 1");
        pane.add(btT1, 0, 1);
       

        Button btT2 = new Button("Table 2");
        pane.add(btT2, 1, 1);
       

        Button btT3 = new Button("Table 3");
        pane.add(btT3, 2, 1);
       

        // Create scene and place in stage
        Scene scene = new Scene(pane, 250, 250);
        primaryStage.setTitle("Restaurant Billing Program");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    // Main method
    public static void main(String[] args) {
        launch(args);
    }
}
Run Code Online (Sandbox Code Playgroud)