我目前正在尝试将一个应用程序迁移到JavaFX (它实际上部分地使用AWT),虽然使用边框布局的JPanels切换到BorderPanes相当容易,但我在如何使用GridBagLayout和GridPanes时遇到了一些困难。(我从未在Swing中使用过这种布局)
在我的代码中,GridBagLayout被使用了2次(我不确定这是否是自动生成的代码):
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));和
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,因为我已经把那些.
任何帮助都是非常感谢的!
发布于 2016-09-09 15:24:56
指定将值传递给构造函数的GridBagConstraints。
GridBagConstraints(
int gridx,
int gridy,
int gridwidth,
int gridheight,
double weightx,
double weighty,
int anchor,
int fill,
Insets insets,
int ipadx,
int ipady)让我们描述一下如何在JavaFX中使用这些参数的等价物:
Node node = ...
GridPane gridPane = ...网格,网格宽度,网格高度
通常使用适当的add方法GridPane来指定这些值。他们被称为columnIndex,rowIndex,columnSpan和rowSpan,在JavaFX中。如果columnSpan和rowSpan为1时,则只需使用带有3个参数的add方法即可:
gridPane.add(node, gridx, gridy);如果其中一个columnSpan/rowSpan更大,则可以使用该方法的重载版本:
gridPane.add(node, gridx, gridy, gridwidth, gridheight);重,重
这些没有直接的等价物。相反,您需要通过使用percentWidth和percentHeight的ColumnConstraints和RowConstraints来定义整个行/列(除非您对标准布局感到满意)。
行和列约束被添加到columnConstraints和rowConstraints列表中。
锚点
可以为此使用halignment/RowConstraints的ColumnConstrants/RowConstraints属性,也可以使用GridPane.setHalignment和GridPane.setValignment为单个节点指定此属性。
GridPane.setHalignment(node, HPos.LEFT);
GridPane.setValignment(node, VPos.TOP);填充
与此等效的是设置行和列约束的fillHeight和fillWidth值,以便使用GridPane.setFillWidth和GridPane.setFillHeight为单个节点指定此约束。
GridPane.setFillWidth(node, Boolean.FALSE);
GridPane.setFillHeight(node, Boolean.FALSE);对于这些属性,默认值是true。
嵌体
您可以通过使用GridPane.setMargin来指定这一点。如果对所有值都使用0,则不需要指定此值。
GridPane.setMargin(node, new Insets(top, right, bottom, left));爱帕德
在JavaFX中没有类似的情况。
有一些static setConstraints方法允许您一次设置多个约束。
https://stackoverflow.com/questions/39414189
复制相似问题