我正在处理的项目中组件的布局看起来不正确,我怀疑Swing中有一个bug。基本上,似乎正在发生的情况是,当布局的单元格具有不同的最小尺寸和/或首选尺寸时,weightx和weighty比例没有得到遵守。我创建了一个示例程序来演示这一点,以下是源代码:
package com.ensoftcorp.product.simmerge.gui.swing.dialogs;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JPanel;
public class TestClass {
/**
* @param args
*/
public static void main(String[] args) {
try {
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
new TestClass();
} catch (Exception e) {
e.printStackTrace();
}
}
static final GridBagConstraints GBC_CELL = new GridBagConstraints();
static final GridBagConstraints GBC_ROWEND = new GridBagConstraints();
static final GridBagConstraints GBC_FILLROW = new GridBagConstraints();
static {
GBC_CELL.anchor = GridBagConstraints.NORTHWEST;
GBC_CELL.weightx = 0.5;
GBC_CELL.fill = GridBagConstraints.BOTH;
GBC_ROWEND.gridwidth = GridBagConstraints.REMAINDER;
GBC_FILLROW.gridwidth = GridBagConstraints.REMAINDER;
GBC_FILLROW.weightx = 1.0;
GBC_FILLROW.fill = GridBagConstraints.BOTH;
}
public TestClass() {
JFrame frame = new JFrame();
JPanel pnlContent = new JPanel(new GridBagLayout());
pnlContent.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
/*
* Layout "ruler" panel
*/
JPanel pnlRuler = new JPanel(new GridBagLayout());
pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);
pnlRuler.add(createRulerCell(Color.BLACK, Color.WHITE),GBC_CELL);
pnlRuler.add(Box.createHorizontalGlue(),GBC_ROWEND);
/*
* Layout "correct" panel
*/
JPanel pnlGoodLayout = new JPanel(new GridBagLayout());
pnlGoodLayout.add(new JButton("JButton1"),GBC_CELL);
pnlGoodLayout.add(new JButton("JButton2"),GBC_CELL);
pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);
pnlGoodLayout.add(new JButton("JButton3"),GBC_CELL);
pnlGoodLayout.add(new JButton("JButton4"),GBC_CELL);
pnlGoodLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);
/*
* Layout "incorrect" panel
*/
JPanel pnlBadLayout = new JPanel(new GridBagLayout());
pnlBadLayout.add(new JButton("JButton1"),GBC_CELL);
pnlBadLayout.add(new JButton("JButton2"),GBC_CELL);
pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);
pnlBadLayout.add(new JButton("JButton number 3 is wide"),GBC_CELL);
pnlBadLayout.add(new JButton("JButton4"),GBC_CELL);
pnlBadLayout.add(Box.createHorizontalGlue(),GBC_ROWEND);
/*
* Add panels to main panel
*/
pnlContent.add(pnlRuler,GBC_FILLROW);
pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
pnlContent.add(pnlGoodLayout,GBC_FILLROW);
pnlContent.add(Box.createVerticalStrut(8),GBC_FILLROW);
pnlContent.add(pnlBadLayout,GBC_FILLROW);
/*
* Configure frame
*/
frame.getContentPane().add(pnlContent);
frame.setTitle("GridBagLayout Weight Bug?");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(400,200);
frame.setVisible(true);
}
JComponent createRulerCell(Color border, Color background) {
JPanel glue = new JPanel();
glue.setBorder(BorderFactory.createLineBorder(border));
glue.setBackground(background);
return glue;
}
}这个示例程序有三个groups...the,第一个是一个排序的“标尺”,显示50%的标记。第二组和第三组是使用GridBagLayout的面板,其中每个单元格的weightx为0.5。这两个组之间唯一的区别是按钮文本长度,但是第二个组没有按比例平均分配列,即使它有足够的空间这样做。
我的问题是:以前有没有人遇到过这个问题,并有他们推荐的解决方法?
附言-我正在使用jdk1.6.0_11,如果这有什么不同的话。
发布于 2009-06-18 01:59:17
从GridBagConstraints.weightx JavaDoc
指定如何分布额外的水平空间。
网格包布局管理器将列的权重计算为列中所有组件的最大权重x。如果生成的布局在水平方向上小于需要填充的区域,则额外的空间将按每列的权重比例分配给每列。
考虑到这一点,我不认为这是一个bug,因为带有宽按钮的列需要更多的空间。一旦计算了该值和正常按钮列的大小,就分配了剩余的空间。
解决方法:
要解决此问题,可以在添加每个按钮之前设置GBC_CELL.gridx和GBC_CELL.gridy,在与大按钮共享一行的按钮之前,可以设置
GBC_CELL.ipadx=100;
然后在添加宽按钮(#3)之前将其设置为0。这应该会使你的按钮在网格中对齐。
发布于 2009-06-18 07:05:43
使用内置的GroupLayout。您拥有比GridBagLayout更多的控制权。
发布于 2011-07-25 08:11:10
这可能对(source)有帮助
当多个列具有非零权重时,将使用权重值在非零权重列之间分配剩余空间。具体地说,如果多余的空间是P个像素,并且^i列的列权重是weight^i,那么^i列权重正好是(weight^i * P) /(所有列权重之和)。例如,如果列1的权重为1,列2的权重为2,并且剩余空间为90像素,则列1将获得额外的30个像素,列2将获得额外的60个像素。具有非零权重值的行的行为类似。
https://stackoverflow.com/questions/1010323
复制相似问题