我正在尝试按照以下模式组织一个框架:
_______________________
| ___________________ |
| | + + + + + | |
| | + + | |
| |___________________| |
| ___________ |
| | + + + | |
| |___________| |
|_______________________|
以下是我的要求:
对于嵌套的JPanel,在我看来,FlowLayout
是最佳解决方案,我尝试使用BoxLayout
和GridBagLayout
来组织我的面板,但没有成功:内部组件永远不会包装。
这是我的代码:
public class Example {
private static final int NB1 = 5;
private static final int NB2 = 13;
private static final int NB3 = 15;
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("Example");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
f.setSize(300,600);
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 1"));
for(int i=0; i<NB1; i++)
p1.add(new JButton("Button " + i));
JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p2.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 2"));
for(int i=NB1; i<NB2; i++)
p2.add(new JButton("Button " + i));
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p3.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 3"));
for(int i=NB2; i<NB3; i++)
p3.add(new JButton("Button " + i));
// JPanel global = new JPanel(new GridBagLayout());
// GridBagConstraints gbc = new GridBagConstraints();
// gbc.gridy = 0;
// global.add(p1, gbc);
// gbc.gridy++;
// global.add(p2, gbc);
// gbc.gridy++;
// global.add(p3, gbc);
JPanel global = new JPanel();
Box vb = Box.createVerticalBox();
vb.add(p1);
vb.add(Box.createVerticalStrut(10));
vb.add(p2);
vb.add(Box.createVerticalStrut(10));
vb.add(p3);
global.add(vb);
f.add(global);
f.setVisible(true);
}
}
如果你知道如何继续......
谢谢!
答案 0 :(得分:2)
对于布局,您需要确保在调整框架大小时面板将填充水平空间。
但是,您仍然无法使用FlowLayout,因为当组件换行时,FlowLayout不会重新计算面板的首选大小。相反,您需要使用Wrap Layout来重新计算首选大小。
以下是&#34;面板2&#34;的更新示例使用WrapLayout
和GridBagLayout更改:
import java.awt.*;
import javax.swing.*;
import javax.swing.border.*;
public class Example2 {
private static final int NB1 = 5;
private static final int NB2 = 13;
private static final int NB3 = 15;
public static void main(String[] args) {
JFrame f = new JFrame();
f.setTitle("Example2");
f.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
JPanel p1 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p1.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 1"));
for(int i=0; i<NB1; i++)
p1.add(new JButton("Button " + i));
// JPanel p2 = new JPanel(new FlowLayout(FlowLayout.CENTER));
JPanel p2 = new JPanel(new WrapLayout(FlowLayout.CENTER));
p2.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 2"));
for(int i=NB1; i<NB2; i++)
p2.add(new JButton("Button " + i));
JPanel p3 = new JPanel(new FlowLayout(FlowLayout.CENTER));
p3.setBorder(new TitledBorder(new LineBorder(Color.BLACK), "JPanel 3"));
for(int i=NB2; i<NB3; i++)
p3.add(new JButton("Button " + i));
JPanel global = new JPanel(new GridBagLayout());
GridBagConstraints gbc = new GridBagConstraints();
gbc.fill = GridBagConstraints.HORIZONTAL; // added
gbc.weightx = 1.0f; // added
gbc.gridy = 0;
global.add(p1, gbc);
gbc.gridy++;
global.add(p2, gbc);
gbc.gridy++;
global.add(p3, gbc);
f.add(global);
f.pack();
f.setVisible(true);
}
}