JavaFX 流窗格对齐

JavaFX Flow Pane Alignment

我正在努力创建一个贷款计算器,但现在确定如何对齐流程窗格的位置,以便所有内容都居中,因为它当前是倾斜的。

public class Main extends Application {
 
    private HBox hboxAnnualIntRate = new HBox(lbAnnualIntRate,tfAnnualInterestRate);
    private HBox hboxNumberOfYears = new HBox(lbNumberOfYears,tfNumberOfYears);
    private HBox hboxLoanAmount = new HBox(lbLoanAmount,tfLoanAmount);
    private HBox hboxMonthlyPayment = new HBox(lbMonthlyPayment,tfMonthlyPayment);
    private HBox hboxTotalPayment = new HBox(lbTotalPayment,tfTotalPayment);

    @Override
    public void start(Stage primaryStage) throws Exception{

        primaryStage.setTitle("Loan Calculator");
        FlowPane rootNode = new FlowPane(20,20);
        rootNode.setAlignment(Pos.CENTER);
        Scene scene = new Scene (rootNode, 500, 300);
        primaryStage.setScene(scene);

      
        calculateButton.setOnAction(actionEvent -> {
            calculatePayment();
        });
        rootNode.getChildren().addAll(hboxAnnualIntRate,hboxNumberOfYears,hboxLoanAmount,hboxMonthlyPayment,hboxTotalPayment,calculateButton);
        primaryStage.show();
    }

正如@trashgod 在评论中提到的那样,您可以使用 GridPane:

public class App extends Application {

    @Override
    public void start(Stage stage) {

        Label lbl1 = new Label("Annual Interest Rate:");
        Label lbl2 = new Label("Number Of Years:");
        Label lbl3 = new Label("Loan Amount:");
        Label lbl4 = new Label("Monthly Payment:");
        Label lbl5 = new Label("Total Payment:");
    
        TextField tf1 = new TextField();
        TextField tf2 = new TextField();
        TextField tf3 = new TextField();
        TextField tf4 = new TextField();
        TextField tf5 = new TextField();
    
        Button button = new Button("Calculate");

        GridPane gp = new GridPane();
    
        gp.addColumn(0, lbl1, lbl2, lbl3, lbl4, lbl5);
        gp.addColumn(1, tf1, tf2, tf3, tf4, tf5);
        gp.add(button, 2, 4);
    
        gp.setHgap(5d);
        gp.setVgap(10d);
    
        gp.setPadding(new Insets(20));

        Scene scene = new Scene(new StackPane(gp));
    
        stage.setScene(scene);

        stage.show();
    }

    public static void main(String[] args) {
        launch();
    }

}

输出: