首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JPanel GridBagLayout to GridPane

JPanel GridBagLayout to GridPane
EN

Stack Overflow用户
提问于 2016-09-09 14:41:44
回答 1查看 615关注 0票数 1

我目前正在尝试将一个应用程序迁移到JavaFX (它实际上部分地使用AWT),虽然使用边框布局的JPanels切换到BorderPanes相当容易,但我在如何使用GridBagLayout和GridPanes时遇到了一些困难。(我从未在Swing中使用过这种布局)

在我的代码中,GridBagLayout被使用了2次(我不确定这是否是自动生成的代码):

代码语言:javascript
复制
JPanel bottomMessagePanel = new JPanel();
bottomMessagePanel.setLayout(new GridBagLayout());
bottomMessagePanel.setBorder(BorderFactory.createEmptyBorder());
bottomMessagePanel.add(someJComponent, new GridBagConstraints(0, 0, 1, 1, .35, 1, GridBagConstraints.WEST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
bottomMessagePanel.add(someOtherJComponent, new GridBagConstraints(1, 0, 1, 1, .65, 1, GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

代码语言:javascript
复制
JPanel stepPanel = new JPanel();
stepPanel.setLayout(new GridBagLayout());
stepPanel.add(someJComponent, new GridBagConstraints(1, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
stepPanel.add(someOtherJComponent, new GridBagConstraints(0, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));
stepPanel.add(some3rdJComponent, new GridBagConstraints(2, 0, 1, 1, 0, 0, GridBagConstraints.EAST, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

我如何使用JavaFX GridPane来完成这个任务?

别担心转换那些JComponents,因为我已经把那些.

任何帮助都是非常感谢的!

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-09 15:24:56

指定将值传递给构造函数的GridBagConstraints

代码语言:javascript
复制
GridBagConstraints(
        int gridx,
        int gridy,
        int gridwidth,
        int gridheight,
        double weightx,
        double weighty,
        int anchor,
        int fill,
        Insets insets,
        int ipadx,
        int ipady)

让我们描述一下如何在JavaFX中使用这些参数的等价物:

代码语言:javascript
复制
Node node = ...
GridPane gridPane = ...

网格,网格宽度,网格高度

通常使用适当的add方法GridPane来指定这些值。他们被称为columnIndexrowIndexcolumnSpanrowSpan,在JavaFX中。如果columnSpanrowSpan为1时,则只需使用带有3个参数的add方法即可:

代码语言:javascript
复制
gridPane.add(node, gridx, gridy);

如果其中一个columnSpan/rowSpan更大,则可以使用该方法的重载版本:

代码语言:javascript
复制
gridPane.add(node, gridx, gridy, gridwidth, gridheight);

重,重

这些没有直接的等价物。相反,您需要通过使用percentWidthpercentHeightColumnConstraintsRowConstraints来定义整个行/列(除非您对标准布局感到满意)。

行和列约束被添加到columnConstraintsrowConstraints列表中。

锚点

可以为此使用halignment/RowConstraintsColumnConstrants/RowConstraints属性,也可以使用GridPane.setHalignmentGridPane.setValignment为单个节点指定此属性。

代码语言:javascript
复制
GridPane.setHalignment(node, HPos.LEFT);
GridPane.setValignment(node, VPos.TOP);

填充

与此等效的是设置行和列约束的fillHeightfillWidth值,以便使用GridPane.setFillWidthGridPane.setFillHeight为单个节点指定此约束。

代码语言:javascript
复制
GridPane.setFillWidth(node, Boolean.FALSE);
GridPane.setFillHeight(node, Boolean.FALSE);

对于这些属性,默认值是true

嵌体

您可以通过使用GridPane.setMargin来指定这一点。如果对所有值都使用0,则不需要指定此值。

代码语言:javascript
复制
GridPane.setMargin(node, new Insets(top, right, bottom, left));

爱帕德

在JavaFX中没有类似的情况。

有一些static setConstraints方法允许您一次设置多个约束。

setConstraints(Node child, int columnIndex, int rowIndex, int columnspan, int rowspan, HPos halignment, VPos valignment, Priority hgrow, Priority vgrow, Insets margin)

票数 5
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39414189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档