【问题标题】:GridBagLayout and ScrollPaneGridBagLayout 和 ScrollPane
【发布时间】:2012-09-22 19:06:58
【问题描述】:

好的,设置如下: 一个面板中的 3 个组件(一个 JScrollpane、一个 JPanel 和一个 JTabbedPane)。

this.plan = new JPanel();
this.plan.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();

//The ScrollPane
this.SrsDocument = new JTextArea();
this.SrsDocument.setEditable(false);
this.SrsDocumentScroll = new JScrollPane(this.SrsDocument);
c.fill = GridBagConstraints.BOTH;
c.weightx = 0.33;
c.weighty = 1.0;
c.gridx = 0;
this.plan.add(this.SrsDocumentScroll, c);
...
//The Panel
this.TreePanel = new JPanel();
this.TreePanel.setLayout(new BoxLayout(this.TreePanel, BoxLayout.Y_AXIS));
this.Tree = new JTree();
((DefaultTreeModel)(this.Tree.getModel())).setRoot(null);
this.Tree.getSelectionModel().setSelectionMode(TreeSelectionModel.SINGLE_TREE_SELECTION);
this.TreeScroll = new JScrollPane(this.Tree);
this.TreePanel.add(this.TreeScroll);
c.gridx = 1;
c.weightx = 0.33;
this.plan.add(this.TreePanel, c);
...
//The TabbedPane
this.currentFunction = new JTabbedPane();
c.gridx = 2;
c.weightx = 0.33;
this.plan.add(this.currentFunction, c);

我曾预计此布局有 3 列,宽度均等。但是初始显示 this.SrsDocumentScroll 的宽度比其他 2 窄得多。此外,当我调整窗口大小时 this.SrsDocumentScroll 被消耗,而另一个在 this.SrsDocumentScroll 完全消耗之前不会调整大小。我曾预计这三个都将调整大小,因为 this.plan 调整大小。 weightx 是否像我假设的那样在调整大小时无法确定如何分配空间?

我还应该补充一点,这三个的内容是:textArea 填充了文本,树只是一个根,而选项卡式窗格只有一个选项卡。所有的内容都是动态变化的,但调整大小并不是我遇到问题的行为,只是调整窗口的大小,它消耗(随着窗口缩小)最左边->最右边而不是相等。

编辑:这更像是一个测试,这就是为什么我使用 0.33 作为我的 weightx 的原因。最终结果是选项卡窗格的权重比其他窗格要重。更像 (0.25,0.25,0.5) 但如果你关注我,那么这些值更易于管理。

【问题讨论】:

    标签: java swing layout resize gridbaglayout


    【解决方案1】:

    我曾预计此布局有 3 列,宽度均等。 ...我原以为这三个都可以同样调整大小...

    很大程度上可能取决于添加到 GridBagLayout 的组件的 preferredSize、maximumSize 和 minimumSize。如果您希望 3 列中大小相等的组件,请不要使用 GridBagLayout,而应使用 GridLayout(1, 3)(1 行 3 列)或 GridLayout(0, 3)(3 列和可变行数)。

    为了说明我的意思,运行下面的代码,注释或取消注释这两行:

          // setSizes(btn, scrollpane);
          // setSizes(label, scrollpane);
    

    GridBagTest.java

    import java.awt.*;
    import javax.swing.*;
    
    public class GridBagTest {
       public static void main(String[] args) {
          JPanel panel = new JPanel(new GridBagLayout());
    
          JTextArea textarea = new JTextArea(5, 30);
          JScrollPane scrollpane = new JScrollPane(textarea);
    
          JButton btn = new JButton("Foo");
    
          JLabel label = new JLabel("Bar");
          label.setBorder(BorderFactory.createTitledBorder("Bar"));
    
          GridBagConstraints gbc = new GridBagConstraints();
          gbc.fill = GridBagConstraints.BOTH;
          gbc.gridheight = 1;
          gbc.gridwidth = 1;
          gbc.gridx = 0;
          gbc.gridy = 0;
          gbc.weightx = 1.0;
          gbc.weighty = 1.0;
    
          panel.add(scrollpane, gbc);
    
          gbc.gridx = 1;
          panel.add(btn, gbc);
    
          gbc.gridx = 2;
          panel.add(label, gbc);
    
          // **** comment or uncomment the lines below
          // setSizes(btn, scrollpane);
          // setSizes(label, scrollpane);
    
          JFrame frame = new JFrame("GBC Test");
          frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          frame.getContentPane().add(panel);
          frame.pack();
          frame.setLocationRelativeTo(null);
          frame.setVisible(true);
    
       }
    
       private static void setSizes(Component changeComponent,
             Component templateComponent) {
          changeComponent.setPreferredSize(templateComponent.getPreferredSize());
          changeComponent.setMaximumSize(templateComponent.getMaximumSize());
          changeComponent.setMinimumSize(templateComponent.getMinimumSize());
       }
    }
    

    【讨论】:

    • 感谢您的回复。这更像是一个测试,这就是为什么我使用 0.33 作为我的 weightx 的原因。最终结果是选项卡窗格的权重比其他窗格要重。更像 (0.25,0.25,0.5) 如果你跟我说。我可能应该提到这一点。谢谢
    • @Billium813: 并且 GridBagLayout 将无法通过该测试。同样,您没有考虑添加到容器中的组件的 preferredSize 和其他属性。
    • 作为后续问题,为什么它不像带有这些参数的网格布局?既然你提到它,这些数字不应该复制这种行为吗?
    • 当我设置 this.SrsDocumentScroll 的preferredSize 时,它​​似乎没有任何区别。我必须设置 textArea 的preferredSize 吗?编辑:更改 textArea 尺寸也无济于事。
    • 好的,关键是要确保它们的尺寸都相同?我认为将 maxsize 和 minsize 设置为相同的值会导致静态组件。这有帮助,但我仍然不知道我是否不希望它们都一样大。
    猜你喜欢
    • 1970-01-01
    • 2015-09-14
    • 2021-01-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-04-21
    相关资源
    最近更新 更多