无法使此网格正确对齐

时间:2014-01-19 15:25:16

标签: java layout grid javafx

今天我一直遇到麻烦,网格不符合我想要的方式。 我怎样才能按照我想要的方式对齐它?

示例+代码:

enter image description here

我希望标记为红色的第3列中的部分消失,并使其在我的图像末尾结束。

    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;
    }

提前致谢, 碧玉。

1 个答案:

答案 0 :(得分:1)

感谢Loa我已经通过使用静态GridPane方法并以这种方式向某些节点添加Margin来解决这个问题。这是他/她分享的链接: http://www.javacodegeeks.com/2012/07/javafx-20-layout-panes-gridpane.html

虽然网格现在没有完全居中,因为第二列中有一些空格,它可能能够在网格的另一边用额外的填充来居中:-)我不知道这是否是一种正确的方法解决问题,但它适用于我:-P

编辑:确实通过在边缘的节点的另一侧添加额外的填充来使其居中。