【问题标题】:Fix the last element of a GridBagLayout to the bottom of the page将 GridBagLayout 的最后一个元素固定到页面底部
【发布时间】:2015-02-11 15:57:46
【问题描述】:

我有一个 GridBagLayout。最后一个元素(startButton)根据上面文本框中的文本数量上下跳跃。有没有办法固定它的位置?我尝试将 PAGE_START 更改为 PAGE_END,但没有任何效果。

contentPanel.add(titleText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.HORIZONTAL, new Insets(50, 100, 0, 100), 0, 0));
        contentPanel.add(expText, new GridBagConstraints(0, 0, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(120, 100, 00, 100), 0, 0));
        contentPanel.add(buttonsPanel, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.HORIZONTAL, new Insets(120, 100, 0, 100), 0, 0));

        contentPanel.add(userText, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(120, 0, 0, 0), 0, 0));
        contentPanel.add(startButton, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                GridBagConstraints.CENTER, new Insets(180, 0, 0, 0), 0, 0));

        contentPanel.add(Box.createVerticalGlue(), new GridBagConstraints(0, 1, 2, 2, 1, 1,
                GridBagConstraints.PAGE_START, GridBagConstraints.BOTH, new Insets(0, 0, 0, 0), 0, 0));

【问题讨论】:

  • 我在您发布的代码中没有看到 PAGE_END。您可以尝试创建一个带有 BorderLayout 的 JPanel,将内容面板放在中间,将按钮面板放在南方。
  • 嗯,它没有;没有任何效果。我这样尝试: contentPanel.add(startButton, new GridBagConstraints(0, 1, 1, 1, 1, 0, GridBagConstraints.PAGE_END, GridBagConstraints.CENTER, new Insets(180, 0, 0, 0), 0, 0 ));

标签: java swing gridbaglayout


【解决方案1】:

不能在你的上下文中尝试这个,但这应该让它坚持到底:

contentPanel.add(startButton,
   new GridBagConstraints(0, 1, 1, 1, 1, 1, GridBagConstraints.PAGE_END,
            GridBagConstraints.CENTER, new Insets(180, 0, 0, 0), 0, 0));

【讨论】:

    【解决方案2】:

    我可以这样做:

    contentPanel.add(startButton, new GridBagConstraints(0, 3, 1, 1, 1, 0, GridBagConstraints.PAGE_START,
                    GridBagConstraints.CENTER, new Insets(-180, 0, 0, 0), 0, 0));
    

    【讨论】:

      猜你喜欢
      • 2019-08-18
      • 2013-09-25
      • 2022-01-10
      • 2012-09-13
      • 2017-06-27
      • 2012-02-05
      • 1970-01-01
      • 2016-12-18
      相关资源
      最近更新 更多