提问者:小点点

对齐Javafx按钮的问题


这里是JavaFX新手。我目前正在开发一个JavaFX项目。我以某种方式设法创建了带有文本、文本字段和两个按钮的主窗口:

然而,我有一些麻烦定位一个按钮,我确切地希望它是(红色填充框)

输出示例

这是我的启动方法的一部分(仅与按钮相关的代码)。请注意,grid是一个私有的GridPane属性。

this.grid = new GridPane();
this.grid.setPadding(new Insets(15));
this.grid.setVgap(2);
this.grid.setHgap(2);
this.grid.setAlignment(Pos.CENTER);

Button submitButton = new Button("Submit");
submitButton.setStyle("-fx-font: 20 arial; -fx-base: #b6e7c9; -fx-background-radius: 10, 10, 10, 10;");
submitButton.setBackground(new Background(new BackgroundFill(Color.LIGHTGREEN, null, null)));

Button exitButton = new Button("Exit");

exitButton.setTextFill(Color.RED);
exitButton.setStyle("-fx-font: 20 arial; -fx-base: #dbd8d6; -fx-background-radius: 10, 10, 10, 10;");
exitButton.setBackground(new Background(new BackgroundFill(Color.RED, null, null)));

this.grid.add(submitButton, 0, 2);
this.grid.add(exitButton, 1, 2);

我怎样才能使退出按钮更向左对齐,与提交按钮对称,即就在文本字段下面?我非常感谢您的帮助。

EDIT1:问题(文本)位于0,0。文本字段位于0,1。

文本和文本字段的EDIT2代码:

    TextField answer = new TextField();
    answer.autosize();
    this.answer = answer.getText();
    this.grid.add(answer, 0, 1);

    Text question = new Text("Hi there ! Press Submit to start! Exit to quit.");
    question.setFont(Font.font("Century Gothic", FontWeight.BOLD, 22));
    question.setFill(Color.BLACK);
    this.grid.add(question, 0, 0);

共1个答案

匿名用户

使文本和文本字段跨越两列:

TextField answer = new TextField();
answer.autosize();
this.answer = answer.getText();

// node, columnIndex, rowIndex, columnSpan, rowSpan:
this.grid.add(answer, 0, 1, 2, 1);

Text question = new Text("Hi there ! Press Submit to start! Exit to quit.");
question.setFont(Font.font("Century Gothic", FontWeight.BOLD, 22));
question.setFill(Color.BLACK);
this.grid.add(question, 0, 0, 2, 1);

然后使“退出”按钮右对齐,它应该按照您想要的方式工作:

this.grid.add(submitButton, 0, 2);
this.grid.add(exitButton, 1, 2);
GridPane.setHalignment(exitButton, HPos.RIGHT);