在我的工作看起来并不正确的项目中的组件的布局,我怀疑有一个错误在Swing。 基本上,似乎发生的是, 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;
}
}
示例程序有三个组...第一个是各种各样的“尺子”,显示50%的大关。 所述第二和第三组是使用的GridBagLayout其中每个单元具有一个面板weightx
0.5。 组与组之间的唯一区别是按钮上的文字长度,但第二组甚至没有均匀的比例列尽管它有足够的空间来做到这一点。
那么我的问题是:有没有人遇到过这个问题,有一个解决办法,他们将建议?
PS - 我使用jdk1.6.0_11,如果有差别。