1

如何在 JavaFXinputPane中将我的面板居中?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课程。

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:

这是结果
这是结果

我想做一些这样的想法
我想做一些这样的想法

谢谢

4

1 回答 1

1

你的问题并不像你问的那样清楚:

如何在 JavaFX 中的 BorderPane 中将面板 inputPane 居中?

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

要回答您的问题,只需使用 aHBox作为您的父级,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);
    }
}
于 2018-04-28T09:45:47.387 回答