这是我想要实现的目标。
------------------------| 150px |
| | width |
| | p2 |
| | |
| Fill frame | |
| p1 | |
| | |
| | |
------------------------| |
150px height | |
| p3 | |
---------------------------------
'填充框架'必须按比例缩放,但其他两个正方形具有固定的宽度/高度。
我的代码确实生成了这个布局,但是p1没有扩展到填满整个框架。请问有谁能告诉我如何使p1动态变化?
先行致谢。
这是我目前的代码:
setTitle(Rolit.TITLE);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
mainPanel = new JPanel();
mainPanel.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
//P1 FILL P1 WITH 64 BUTTONS
p1 = new JPanel();
p1.setLayout(new GridLayout(8, 8));
p1.setMinimumSize(new Dimension(600, 600));
p1.setMaximumSize(new Dimension(600, 600));
p1.setPreferredSize(new Dimension(600, 600));
for(int y = 0; y < 8; y++){
for(int x = 0; x < 8; x++){
JButton btn = new JButton(x + " , " + y);
p1.add(btn);
}
}
//P2 FILL P2 WITH 4 BUTTONS
p2 = new JPanel();
p2.setLayout(new GridLayout(4, 1));
p2.setMinimumSize(new Dimension(150, 100));
p2.setMaximumSize(new Dimension(150, 100));
p2.setPreferredSize(new Dimension(150, 100));
for(int i = 0; i < 4; i++){
p2.add(new JButton("Button"));
}
//P3
p3 = new JPanel();
p3.setMinimumSize(new Dimension(100, 150));
p3.setMaximumSize(new Dimension(100, 150));
p3.setPreferredSize(new Dimension(100, 150));
p3.setBackground(Color.BLUE);
//ADD ALL TO mainPanel
c.gridx = 0;
c.gridy = 0;
c.fill = GridBagConstraints.BOTH;
mainPanel.add(p1, c);
c.gridx = 1;
c.gridheight = 2;
c.fill = GridBagConstraints.VERTICAL;
mainPanel.add(p2, c);
c.gridx = 0;
c.gridy = 1;
c.gridheight = 1;
c.gridwidth = 2;
c.fill = GridBagConstraints.HORIZONTAL;
mainPanel.add(p3, c);
setContentPane(mainPanel);
pack();
setLocationRelativeTo(null);
setVisible(true);
GridBagLayout
,如果以后想要更改它,复合布局更容易处理 - 在我看来。 - MadProgrammer