【问题标题】:Can't get this grid to align properly无法让此网格正确对齐
【发布时间】: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 列也有问题. :-(
  • 我也很好奇你的问题。或许以下地址能给你带来一些帮助:docs.oracle.com/javafx/2/api/javafx/scene/layout/…javacodegeeks.com/2012/07/javafx-20-layout-panes-gridpane.html
  • 感谢您的回复,通过阅读 GridPane 静态方法,我使用第二个链接取得了一定的成功。所以我使用 GridPane.setMargin();虽然它不能以这种方式完美地将我的网格居中,但也许我也可以从外部添加一个边距以将其推到中心。
  • 如果你的程序布局成功了,要求你和我们分享你的代码是不是太过分了?也许您可以编辑您的问题并添加您的最终代码。我真的很想知道如何做到这一点。

标签: java layout grid javafx


【解决方案1】:

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

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

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

【讨论】:

  • 有趣的事实:我在 (0,0) 中添加了另一个徽标,然后突然第二列完全停止在底部图像的末尾,消除了对边距和填充的需要。我真的大吃一惊:-P
  • 好吧,欢迎您。很高兴能帮助到你。祝你的项目好运。
猜你喜欢
  • 1970-01-01
  • 2019-07-20
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
  • 2021-03-23
  • 2011-10-10
  • 1970-01-01
  • 2018-04-08
相关资源
最近更新 更多