对齐Javafx按钮的问题

时间:2016-12-21 20:29:15

标签: java user-interface button javafx javafx-8

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

但是,我确定按钮的位置有些困难,我确切想要它(红色的盒子)

Output example

这是我的launch方法的一部分(只有与按钮相关的代码)。请注意,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);

如何设置将“退出”按钮更多地左对齐,与“提交”按钮对齐,即在TextField下面? 我非常感谢你的帮助。

EDIT1: 问题(文字)是0,0。 TextField为0,1。

EDIT2 Text和TextField的代码:

    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 个答案:

答案 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);