【问题标题】:JPanel GridBagLayout to GridPaneJPanel GridBagLayout 到 GridPane
【发布时间】:2017-01-17 18:12:52
【问题描述】:

我目前正在尝试将应用程序迁移到 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,因为我已经转换了那些......

非常感谢任何帮助!

【问题讨论】:

    标签: java swing user-interface javafx


    【解决方案1】:

    您指定 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, 权重

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

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

    锚点

    您可以为此使用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)

    【讨论】:

    • 感谢您的回答。这对我帮助很大!您能否为其中一个组件添加代码示例?
    • 请注意,hgrowvgrow 属性可以通过静态方法在单个节点上设置,也可以分别在ColumnConstraintsRowConstraints 上设置,它们提供与@ 有点相似的功能987654373@ 和weighty。然而,它们不是连续的值规模,而是只取三个值:NEVER(相当于 weightx=0)、ALWAYS(相当于 weightx 是布局中所有 weightx 的最大值)和SOMETIMES(相当于weightx等于0到最大值之间的某个固定值)。
    • GridPane 提供了一个静态方法setConstraints,类似于 swing add 方法...您可能需要稍微修改一下答案...
    • @RoiEX:在这里使用原语没有帮助。它只会导致不必要的自动装箱,导致相同的对象作为参数传递。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多