如何使用 JavaFX 将我的 "inputPane" 居中

How to center my "inputPane" using JavaFX

如何在 JavaFX 中将面板 inputPane 置于 BorderPane 中心?

这是我的代码。

public class MainStage {
    private Scene mainScene;
    private InputPane inputPane;
    private DisplayPane displayPane;
    private EditPane editPane;
    private BorderPane borderPane;

    private VBox mainPane;

    public MainStage(){
        borderPane = new BorderPane();
        inputPane=new InputPane();
        displayPane=new DisplayPane();
        editPane=new EditPane();


        mainPane=new VBox();
        mainPane.getChildren().addAll(inputPane.getPane(),displayPane.getTable(),editPane.getPane());
        mainPane.setSpacing(10);

        mainScene=new Scene(mainPane,800,480);
        mainScene.getStylesheets().add("styleSheet.css");
    }
    public Scene getMainScene() {
        return mainScene;
    }
    public InputPane getInputPane() {
        return inputPane;
    }
    public DisplayPane getDisplayPane() {
        return displayPane;
    }
    public EditPane getEditPane(){
        return editPane;
    }
}

我正在尝试修改 InputPane class。

public class InputPane {

    private Text server;
    private Text textNumber;
    private TextField tfNumber;

    private Button addButton;
    private GridPane griglia;

    public InputPane() {

        textNumber=new Text("Insert Port Number: ");
        server = new Text("SERVER");
        server.setStyle("-fx-font-weight:bold;"
                       +"-fx-color: black;"
                       +"-fx-font-size: 18;");
        tfNumber = new TextField();
        tfNumber.setTextFormatter(new TextFormatter<>(new NumberStringConverter()));

        griglia=new GridPane();

        griglia.add(server,1,0);
        griglia.add(textNumber,0,1);
        griglia.add(tfNumber,1,1);
        griglia.setVgap(5);
        griglia.setHgap(10);

        GridPane.setHalignment(server, HPos.CENTER);
        GridPane.setHalignment(textNumber, HPos.CENTER);

    }

    public GridPane getPane() {
        return griglia;
    }
    public Button getAddButton() {
        return addButton;
    }
}

我给你一些关于如何查看我的 GUI 的想法:

这是结果

我想做一些这样的思考

谢谢

你问的问题不是很清楚:

How can I center my panel inputPane in a BorderPane, in JavaFX?

但是在你的例子中,你从不使用你的BorderPane,你只使用VBox

要回答您的问题,只需使用 HBox 作为 GridPane 的父级,并将对齐方式 属性 设置为 Pos.CENTER

public class Main extends Application {
    @Override
    public void start(Stage primaryStage) {
        BorderPane bord = new BorderPane();
        HBox hb = new HBox();
        GridPane griglia = new GridPane();
        Text server = new Text("SERVER");
        griglia.add(server, 1, 0);
        griglia.add(new Text("Numero Porta"), 0, 1);
        griglia.add(new TextField(), 1, 1);
        griglia.setVgap(5.0);
        griglia.setHgap(5.0);
        GridPane.setValignment(server, VPos.CENTER);
        GridPane.setHalignment(server, HPos.CENTER);
        hb.getChildren().add(griglia);
        hb.setStyle("-fx-border-color:red");
        hb.setAlignment(Pos.CENTER);
        bord.setTop(hb);
        primaryStage.setScene(new Scene(bord, 800, 500));
        primaryStage.show();
    }

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