【发布时间】:2014-02-08 17:11:48
【问题描述】:
我今天遇到了一个网格不符合我想要的方式的问题。 我怎样才能按照我想要的方式对齐它?
示例+代码:
我希望第三列中我标记为红色的部分消失,并使其在我的图像末尾结束。
private GridPane login(final BorderPane rootLayout) {
// Create a grid layout
final GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 50));
// Set header text and add it to the grid
Text sceneTitle = new Text("Welcome");
sceneTitle.setFont(Font.font("Tahoma", FontWeight.NORMAL, 25));
grid.add(sceneTitle, 0, 0, 2, 1);
// Create username label and textfield and add it to the grid
Label labelUser = new Label("Username: ");
grid.add(labelUser, 0, 1);
TextField textUser = new TextField();
grid.add(textUser, 1, 1);
// Create password label and field and add it to the grid
Label labelPass = new Label("Password: ");
grid.add(labelPass, 0, 2);
PasswordField textPass = new PasswordField();
grid.add(textPass, 1, 2);
// Create a button to login and add it to the grid
Button buttonLogin = new Button("Login");
HBox hbButton = new HBox(10);
hbButton.setAlignment(Pos.BOTTOM_RIGHT);
hbButton.getChildren().add(buttonLogin);
grid.add(hbButton, 1, 4);
// Create kone logo and add it to the bottom of the layout
ImageView imageKone = new ImageView(
new Image(MainWindow.class.getResourceAsStream("Resources/konecranes.png")));
grid.add(imageKone, 0, 7, 3, 2);
// Create capman logo and add it to the top_right
ImageView imageCapman = new ImageView(
new Image(MainWindow.class.getResourceAsStream("Resources/Capman.png")));
// Login button actionHandler, make it do login and edit root layout.
buttonLogin.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent e) {
grid.setGridLinesVisible(!grid.isGridLinesVisible());
}
});
return grid;
}
提前致谢, 贾斯珀。
【问题讨论】:
-
我一直在尝试在为 2 列添加节点之前添加 ColumConstraints,然后设置每列的 maxWidth,这似乎不会影响第 2 列,而且第 1 列也有问题. :-(
-
感谢您的回复,通过阅读 GridPane 静态方法,我使用第二个链接取得了一定的成功。所以我使用 GridPane.setMargin();虽然它不能以这种方式完美地将我的网格居中,但也许我也可以从外部添加一个边距以将其推到中心。
-
如果你的程序布局成功了,要求你和我们分享你的代码是不是太过分了?也许您可以编辑您的问题并添加您的最终代码。我真的很想知道如何做到这一点。