java - 无法使该网格正确对齐

标签 java layout grid javafx

我今天遇到了一个问题,网格没有按照我想要的方式对齐。 我怎样才能按照我想要的方式对齐它?

示例+代码:

enter image description here

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

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

提前致谢, 贾斯珀。

最佳答案

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

尽管由于第二列中有一些空格,网格现在没有完全居中,但它可能可以在网格的另一侧用额外的填充将其居中:-)我不知道这是否是正确的方法解决了问题,但它对我有用:-P

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

关于java - 无法使该网格正确对齐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21218647/

相关文章:

iOS iphone 格式 : What is best way to format page so it works on all screens?

java - 如何用 Java 实现 MongoDB 聚合

java - 如何等待Java中线程的启动

ruby-on-rails - Rails 3,设计 : Using different layout for signup, 登录页面

android - 动态更改View Stub的layout_height属性?

header - 如何修复 Extjs 4.2 中的表头

java - 使用 Java POST 请求在 Django Tastypie 上创建资源,但收到 500 错误代码

Java断言异常

wpf - 在WPF网格中,如何在鼠标位置找到“行和列”?

CSS相对大小列不等宽问题