Java GridBagLayout固定空间按权重x/y设置
我有一个框架,下面是3个JPanel的布局Java GridBagLayout固定空间按权重x/y设置,java,swing,layout-manager,gridbaglayout,Java,Swing,Layout Manager,Gridbaglayout,我有一个框架,下面是3个JPanel的布局 ---------------------------- | | | | l | | | e | //toppanel | | f | | | t |----------------------| | p | | | n | //bottompanel
----------------------------
| | |
| l | |
| e | //toppanel |
| f | |
| t |----------------------|
| p | |
| n | //bottompanel |
| l | |
| | |
----------------------------
我通过使用GridBagLayout
这是该布局的代码
public class UITests extends JFrame{
public UITests(){
setLayout(new GridBagLayout());
//the three main panels
JPanel leftPanel = new JPanel();
JPanel topPanel = new JPanel();
JPanel bottomPanel = new JPanel();
leftPanel.setBackground(Color.YELLOW);
topPanel.setBackground(Color.GREEN);
bottomPanel.setBackground(Color.PINK);
//position the three main panels
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 2;
gbc.gridwidth = 1;
gbc.fill = gbc.BOTH;
gbc.weightx = .2;
gbc.weighty = 1;
getContentPane().add(leftPanel, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.fill = gbc.BOTH;
gbc.weightx = .8;
gbc.weighty = .5;
getContentPane().add(topPanel, gbc);
gbc = new GridBagConstraints();
gbc.gridx = 1;
gbc.gridy = 1;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.fill = gbc.BOTH;
gbc.weightx = .8;
gbc.weighty = .5;
getContentPane().add(bottomPanel, gbc);
setSize(600,600);
setLocationRelativeTo(null);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public static void main(String[] args){
new UITests();
}
}
问题是,当我在顶部面板中添加一个JTable
时,底部面板的高度会缩小,就像它的一些高度被顶部面板占据一样。下面是顶部面板中表格的代码,是在设置三个主面板的背景后添加的
Object[][] contents = {{"haha","hehe", "hihi", "hoho", "huhu"},
{"haha","hehe", "hihi", "hoho", "huhu"},
{"haha","hehe", "hihi", "hoho", "huhu"},
{"haha","hehe", "hihi", "hoho", "huhu"}};
Object[] heads = {"Haha Head", "Hehe Head", "Hihi Head", "Hoho Head", "Huhu Head"};
JTable table = new JTable(contents, heads);
JScrollPane scrollTable = new JScrollPane(table);
//add components to topPanel
GridBagConstraints gbc = new GridBagConstraints();
gbc.gridx = 0;
gbc.gridy = 0;
gbc.gridheight = 1;
gbc.gridwidth = 1;
gbc.weightx = 1;
gbc.weighty = 1;
gbc.fill = gbc.BOTH;
topPanel.setLayout(new GridBagLayout());
topPanel.add(scrollTable, gbc);
框架现在看起来像
----------------------------
| | |
| l | |
| e | //toppanel |
| f | //new table created |
| t | |
| p | |
| n | |
| l |----------------------|
| | //bottompanel |
----------------------------
那么,如何使“weightx”或“weighty”创建的空间固定?
weightx/weighty
用于分配额外的空间。所以事实上,容器要求孩子们选择他们喜欢的尺寸,如果尺寸小于可用尺寸,额外的像素将根据权重分布
因此,您可以定义
JScrollPane
的首选大小,其中包含JTable问题leftpanel和bottompanel可以在两个方向上调整大小,它们应该是固定的。我理解,但根据setPreferredSize()
,应该避免使用LayoutManager
处理大小调整。我想知道如果不使用这种方法是否可以实现这一点。您可以将顶部和底部面板放置到具有GridLayout(2行1列)的容器中。或者您可以覆盖bottomPanel的首选大小,以返回首选的topPanel高度。我感谢您的建议。但我只想使用GridBagLayout
实现这一点。我听说这个布局非常灵活,所以我想知道是否可以只使用GridBagLayout
来实现,而不干扰组件的SetPreferedSize
。好吧,看来除了遵循您的建议之外没有其他方法了,所以答案被接受。