问题
I have a frame with the following layout of 3 JPanels.
----------------------------
| | |
| l | |
| e | //toppanel |
| f | |
| t |----------------------|
| p | |
| n | //bottompanel |
| l | |
| | |
----------------------------
I achieved this with the use of GridBagLayout
This is the code for that layout
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();
}
}
Problem is, when I add a JTable
in the top panel, the bottom panel's height is shrinked, like some of its height is taken by the top panel. Here is the code for the table in the top panel , added after setting the background of three main panels.
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);
The frame will now look like
----------------------------
| | |
| l | |
| e | //toppanel |
| f | //new table created |
| t | |
| p | |
| n | |
| l |----------------------|
| | //bottompanel |
----------------------------
So, how can I make the space created by 'weightx' or 'weighty' fixed?
回答1:
weightx/weighty
are used to distribute extra space. So in fact container asks children for their preferred size and if it's smaller than available the extra pixels are distributed according to the weights.
So you can define preferred size of the JScrollPane
which holds the JTable
来源:https://stackoverflow.com/questions/15600565/gridbaglayout-fix-space-set-by-weight-x-y