次の3つのJPanelsのレイアウトのフレームがあります。
----------------------------
| | |
| 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
と、ボトムパネルの高さの一部がトップパネルによって取得されるように、ボトムパネルの高さが縮小されることです。これは、3つのメインパネルの背景を設定した後に追加された、トップパネルのテーブルのコードです。
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」によって作成されたスペースを固定するにはどうすればよいですか?