如何在舞台顶部添加标签
How to add label to top of stage
我试图将 "Registration form" 文本放在顶部,但是当我这样做时 pane.add 它分隔了标签和文本框之间的间距。如何将它添加到顶部而不影响它下面的所有内容?
public class RegistrationForm extends Application {
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
Text text = new Text ("Registration Form");
text.setFont(Font.font("Times New Roman", FontWeight.BOLD,
FontPosture.ITALIC, 20));
pane.getChildren().add(text);
pane.add(new Label("User Name: "), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(new TextField(), 1, 2);
pane.add(new Label("Email: "), 0, 3);
pane.add(new TextField(), 1, 3);
pane.add(new Label("Phone: "), 0, 4);
pane.add(new TextField(), 1, 4);
Button btReg = new Button("Register");
pane.add(btReg, 0, 5);
GridPane.setHalignment(btReg, HPos.LEFT);
Button btCan = new Button("Cancel");
pane.add(btCan, 1, 5);
GridPane.setHalignment(btCan, HPos.LEFT);
Scene scene = new Scene(pane);
primaryStage.setTitle("Registration Form");
primaryStage.setScene(scene);
primaryStage.show();
}
}
在文档中
add(Node child, int columnIndex, int rowIndex, int colspan, int
rowspan) Adds a child to the gridpane at the specified column,row
position and spans.
你应该使用:
pane.add(label, 0, 0, 2, 1);
我试图将 "Registration form" 文本放在顶部,但是当我这样做时 pane.add 它分隔了标签和文本框之间的间距。如何将它添加到顶部而不影响它下面的所有内容?
public class RegistrationForm extends Application {
public void start(Stage primaryStage) {
GridPane pane = new GridPane();
pane.setAlignment(Pos.CENTER);
pane.setPadding(new Insets(11.5, 12.5, 13.5, 14.5));
pane.setHgap(5.5);
pane.setVgap(5.5);
Text text = new Text ("Registration Form");
text.setFont(Font.font("Times New Roman", FontWeight.BOLD,
FontPosture.ITALIC, 20));
pane.getChildren().add(text);
pane.add(new Label("User Name: "), 0, 1);
pane.add(new TextField(), 1, 1);
pane.add(new Label("Password: "), 0, 2);
pane.add(new TextField(), 1, 2);
pane.add(new Label("Email: "), 0, 3);
pane.add(new TextField(), 1, 3);
pane.add(new Label("Phone: "), 0, 4);
pane.add(new TextField(), 1, 4);
Button btReg = new Button("Register");
pane.add(btReg, 0, 5);
GridPane.setHalignment(btReg, HPos.LEFT);
Button btCan = new Button("Cancel");
pane.add(btCan, 1, 5);
GridPane.setHalignment(btCan, HPos.LEFT);
Scene scene = new Scene(pane);
primaryStage.setTitle("Registration Form");
primaryStage.setScene(scene);
primaryStage.show();
}
}
在文档中
add(Node child, int columnIndex, int rowIndex, int colspan, int rowspan) Adds a child to the gridpane at the specified column,row position and spans.
你应该使用:
pane.add(label, 0, 0, 2, 1);