首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >GridBagLayout和ScrollPane

GridBagLayout和ScrollPane
EN

Stack Overflow用户
提问于 2012-10-02 05:51:41
回答 1查看 3.1K关注 0票数 2

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

代码语言:javascript
复制
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宽度比其他两个窄得多。另外,当我调整窗口大小时,this.SrsDocumentScroll会被消耗掉,而另一个窗口直到this.SrsDocumentScroll完全被消耗后才会调整大小。我曾期望随着this.plan的大小调整,这三个组件的大小都会相应地调整。像我假设的那样,在调整大小时,weightx不能决定如何分配空间吗?

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

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2012-10-02 05:55:10

我曾期望这个布局有3列,所有列的宽度都是偶数。..。我原以为这三个会平均调整大小..。

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

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

代码语言:javascript
复制
      // setSizes(btn, scrollpane);
      // setSizes(label, scrollpane);

GridBagTest.java

代码语言:javascript
复制
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());
   }
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12681649

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档