【发布时间】:2012-03-26 20:06:21
【问题描述】:
这对你们来说很难:)
基本上我有一个 GridBagLayout 有 2 列:一个 list 包裹在一个 scrollpane 中。滚动窗格沿 BOTH 方向拉伸。
如果我逐渐减小此面板的高度(通过拖动窗口的边缘),我会看到滚动窗格上发生“随机”调整大小:
当第一个的 Hscroll bar 出现时,第二个的宽度会减小
那么,第二个的宽度又无缘无故地缩小了……
如果我不包装我的组件,请不要出现这种行为。
如果你用 tree 替换右边的列表,它的行为会有所不同:
将窗口的高度缩小到 380 像素以下,树的大小会被调整...
如果我不包装我的组件,如果你不断调整窗口大小,树就会重新调整大小!
你们知道发生了什么吗???
PS:我尝试构建的实际布局比这个示例更复杂。与此同时,我使用 SpringLayout 来做我想做的事,但它需要很多(不是那么漂亮)的东西来设置
protected static ListModel newListModel(int n) {
DefaultListModel lm = new DefaultListModel();
for (int i = 0; i < n; ++i)
lm.addElement("AAA");
return lm;
}
protected static JComponent createContentPane() {
JPanel pane = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.anchor = GridBagConstraints.CENTER;
gbc.fill = GridBagConstraints.BOTH;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.gridy = 0;
gbc.gridx = 0;
pane.add(new JScrollPane(new JList(newListModel(12))), gbc);
++gbc.gridx;
pane.add(new JScrollPane(new JList(newListModel(4))), gbc);
return pane;
}
public static void main(String[] args) {
JFrame f = new JFrame();
f.getContentPane().add(createContentPane());
f.setSize(800, 400);
f.setLocationRelativeTo(null);
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setVisible(true);
}
【问题讨论】:
标签: java swing resize jscrollpane gridbaglayout