gpt4 book ai didi

java - JPanel GridBagLayout 到 GridPane

转载 作者:行者123 更新时间:2023-11-29 07:33:09 25 4
gpt4 key购买 nike

我目前正在尝试将应用程序迁移到 JavaFX(它实际上部分使用 AWT),虽然将具有边框布局的 JPanel 切换到 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,因为我已经转换了那些...

非常感谢任何帮助!

最佳答案

您指定 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 = ...

gridx, gridy, gridwidth, gridheight

通常您使用GridPane 的适当add 方法来指定这些值。它们在 JavaFX 中称为 columnIndexrowIndexcolumnSpanrowSpan。如果 columnSpanrowSpan 为 1,则可以使用带 3 个参数的 add 方法:

gridPane.add(node, gridx, gridy);

如果 columnSpan/rowSpan 之一更大,您可以使用该方法的重载版本:

gridPane.add(node, gridx, gridy, gridwidth, gridheight);

weightx, 权重

那些没有直接的等价物。相反,您需要使用 ColumnConstraintspercentWidthpercentHeight 为整行/列定义它和 RowConstraints (除非您对标准布局感到满意)。

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

anchor

您可以为此使用 ColumnConstrants/RowConstraintshalignment/valignment 属性,或为此指定使用 GridPane.setHalignmentGridPane.setValignment 的单个节点:

GridPane.setHalignment(node, HPos.LEFT);
GridPane.setValignment(node, VPos.TOP);

填充

这等效于设置行和列约束的 fillHeightfillWidth 值,使用 GridPane.setFillWidthGridPane.setFillHeight:

GridPane.setFillWidth(node, Boolean.FALSE);
GridPane.setFillHeight(node, Boolean.FALSE);

这些属性的默认值为 true

插入

您可以使用 GridPane.setMargin 指定它。如果您对所有值都使用 0,则不需要指定它。

GridPane.setMargin(node, new Insets(top, right, bottom, left));

ipadx, ipady

在 JavaFX 中没有对应的东西。


static setConstraints 方法允许您一次设置多个约束,例如

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

关于java - JPanel GridBagLayout 到 GridPane,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39414189/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com