我必须做一些基本的javafx工作,但我有一个问题,我不能处理。首先,我的目标布局是:
我所做的是创建两个网格窗格,并尝试将它们彼此对齐。一开始,好像没事。但当我写下剩下的部分时,它开始变得非常糟糕,我开始无法调整任何东西。制作底部按钮菜单非常简单。但不管我做了什么,我都无法对齐文本字段和标签。这是我的密码`
addButton = new Button("Add");
firstButton = new Button("First");
nextButton = new Button("Next");
previousButton = new Button("Previous");
lastButton = new Button("Last");
updateByIdButton = new Button("Update By Id");
searchByIdButton = new Button("Search By Id");
cleanButton = new Button("Clean textFields");
GridPane root = new GridPane();
HBox bottomMenu = new HBox();
bottomMenu.getChildren().addAll(addButton, firstButton, nextButton, previousButton, lastButton, updateByIdButton, searchByIdButton, cleanButton);
bottomMenu.setSpacing(10);
BorderPane border = new BorderPane();
bottomMenu.setPadding(new Insets(10, 10, 10, 20));
border.setBottom(bottomMenu);
GridPane labels = new GridPane();
border.setCenter(root);
labels.setVgap(8);
labels.setHgap(5);
labels.setPadding(new Insets(10, 10, 10, 80));
Label idLabel = new Label("ID : ");
GridPane.setConstraints(idLabel, 0, 4);
Label nameLabel = new Label("Name : ");
GridPane.setConstraints(nameLabel, 0, 6);
Label streetLabel = new Label("Street : ");
GridPane.setConstraints(streetLabel, 0, 10);
Label cityLabel = new Label("City : ");
GridPane.setConstraints(cityLabel, 0, 13);
Label searchUpdateLabel = new Label("Search/Update ID : ");
GridPane.setConstraints(searchUpdateLabel, 19, 3);
Label genderLabel = new Label("Gender : ");
GridPane.setConstraints(genderLabel, 20, 6);
Label zipLabel = new Label("Zip : ");
GridPane.setConstraints(zipLabel, 25, 6);
labels.getChildren().addAll(idLabel, nameLabel, streetLabel, cityLabel, searchUpdateLabel, genderLabel, zipLabel);
labels.getColumnConstraints().add(new ColumnConstraints(50));
labels.getColumnConstraints().add(new ColumnConstraints(400));
root.getChildren().add(labels);
GridPane fields = new GridPane();
fields.setPadding(new Insets(10, 10, 10, 80));
fields.setVgap(10);
TextField idField = new TextField();
idField.setDisable(true);
idField.setMaxWidth(50);
GridPane.setConstraints(idField, 1, 3);
TextField nameField = new TextField();
GridPane.setConstraints(nameField, 1, 5);
TextField streetField = new TextField();
GridPane.setConstraints(streetField, 1, 6);
TextField cityField = new TextField();
GridPane.setConstraints(cityField, 1, 7);
TextField zipField = new TextField();
GridPane.setConstraints(zipField, 16, 6);
TextField genderField = new TextField();
GridPane.setConstraints(genderField, 21, 6);
TextField searchUpdateField = new TextField();
GridPane.setConstraints(searchUpdateField, 21, 3);
fields.getChildren().addAll(idField, nameField, streetField, cityField, zipField, genderField, searchUpdateField);
fields.getColumnConstraints().add(new ColumnConstraints(50));
fields.getColumnConstraints().add(new ColumnConstraints(400));
root.getChildren().add(fields);
而且,当我尝试使用 GridPane.setConstraints()
或添加新列。
2条答案
按热度按时间zlwx9yxi1#
看起来你把gridpane当成了一维对象,而实际上它是二维的。目标中的所有元素不是像网格一样排列的,但是只需一个网格窗格就可以获得非常接近目标的内容,并且必须使用跨越多个列的一些元素来设置它:
它变得有点难看,并且不太适合gridpane的用例。使用vbox,然后将每一行放入hbox可能更容易。您可以将第一个textfield“column”对齐,方法是将每行的第一个标签放在自己的hbox中,并将该hbox的minwidth设置为每行相同的值。这样地:
4smxwvx52#
这是你的名字
FXML
. 也许你可以把它翻译成纯代码。它被分解成一个VBox
作为根。这个VBox
有一个GridPane
和一个HBox
为了孩子。fxml公司