java - GridBagLayout 和 ScrollPane

标签 java swing layout resize gridbaglayout

好的,设置如下: 面板中有 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 充满文本,tree 仅作为根,选项卡式 Pane 只有一个选项卡。所有的内容都是动态变化的,但是调整大小并不是我遇到麻烦的行为,只是调整窗口大小并且它消耗(随着窗口缩小)最左->最右而不是平均。

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

最佳答案

I had expected this layout to have 3 columns, all with even width. ... I had expected all three to resize equally ...

很大程度上可能取决于添加到 GridBagLayout 的组件的首选尺寸、最大尺寸和最小尺寸。如果您希望 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());
   }
}

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

相关文章:

java - JFileChooser 过滤器

java - 使用完整目录路径将文件添加到 JEditorPane

swing - 启用/禁用时的 JTextField 背景颜色

c++ - 在 QMainWindow 中显示 QGridLayout

html - 包含数据网格的响应式网页布局

java - Tycho 引发 Eclipse IDE 不存在的错误 : Cannot refer to a non-final variable

java - commons math3 和 ojalgo 库的 SVD 解决方案输出之间存在巨大差异

java - 数据库查询以在没有结果时显示消息

java - 正则表达式 [a-zA-Z]{0,2}

android - 初学者 android 开发人员 - 布局有困难