JPanel GridBagLayout到GridPane

Roi*_*iEX 1 java user-interface swing javafx

我目前正在尝试将应用程序迁移到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));
Run Code Online (Sandbox Code Playgroud)

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));
Run Code Online (Sandbox Code Playgroud)

我如何使用JavaFX GridPane执行此操作?
不要担心转换那些JComponents,因为我已经转换了那些...

任何帮助非常感谢!

fab*_*ian 5

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

GridBagConstraints(
        int gridx,
        int gridy,
        int gridwidth,
        int gridheight,
        double weightx,
        double weighty,
        int anchor,
        int fill,
        Insets insets,
        int ipadx,
        int ipady)
Run Code Online (Sandbox Code Playgroud)

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

Node node = ...
GridPane gridPane = ...
Run Code Online (Sandbox Code Playgroud)

gridx,gridy,gridwidth,gridheight

通常使用适当的add方法GridPane来指定这些值.他们被称为columnIndex,rowIndex,columnSpanrowSpanJavaFX中.如果columnSpanrowSpan是1,它足以为使用add方法服用3个参数:

gridPane.add(node, gridx, gridy);
Run Code Online (Sandbox Code Playgroud)

如果一个columnSpan/ rowSpan是更大的,你可以使用方法的重载版本:

gridPane.add(node, gridx, gridy, gridwidth, gridheight);
Run Code Online (Sandbox Code Playgroud)

重量,重量

那些没有直接的等价物.相反,你需要通过定义此为一整行/列percentWidthpercentHeightColumnConstraintsRowConstraints(即除非你满意的标准布局).

行和列约束将添加到列表columnConstraintsrowConstraints列表中.

您可以使用halignment/ valignment的性能ColumnConstrants/ RowConstraints本或指定此使用单个节点GridPane.setHalignmentGridPane.setValignment:

GridPane.setHalignment(node, HPos.LEFT);
GridPane.setValignment(node, VPos.TOP);
Run Code Online (Sandbox Code Playgroud)

相当于使用和设置为单个节点指定此行的行和列约束的值fillHeightfillWidth值:GridPane.setFillWidthGridPane.setFillHeight

GridPane.setFillWidth(node, Boolean.FALSE);
GridPane.setFillHeight(node, Boolean.FALSE);
Run Code Online (Sandbox Code Playgroud)

默认值适用true于这些属性.

插图

您可以使用指定GridPane.setMargin.如果您使用的0是所有值,则无需指定此值.

GridPane.setMargin(node, new Insets(top, right, bottom, left));
Run Code Online (Sandbox Code Playgroud)

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)