FlowLayout包装在嵌套的JPanel中不起作用



我正在尝试按照以下模式组织一个框架:

 _______________________
|  ___________________  |
| | +   +   +   +   + | |
| | +   +             | |
| |___________________| |
|  ___________          |
| | +   +   + |         |
| |___________|         |
|_______________________|

以下是我的要求:

  • 我不知道在编译时我将有多少组件在每个嵌套的JPanel
  • 用户应该能够调整窗口的大小
  • 如果没有足够的空间在一行上显示所有组件,则嵌套的JPanel应该垂直扩展
  • 嵌套的JPanel(s)不应该垂直扩展,如果有可用的空间(即,我不想在嵌套的JPanel(s)有垂直的空白,但它是好的,如果有一些在主框架)

对于嵌套的JPanel(s),在我看来,FlowLayout是最好的解决方案,我试图使用BoxLayoutGridBagLayout来组织我的面板,但没有成功:内部组件从不包装。

下面是我的代码:
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);
}
}

如果你知道如何继续…

谢谢!

对于你的布局,你需要确保面板将填充水平空间时,框架的大小调整。

然而,你仍然不能使用FlowLayout,因为FlowLayout不会重新计算组件包装时面板的首选大小。相反,你需要使用换行布局,它将重新计算首选的大小。

以下是使用WrapLayout和GridBagLayout更改的"panel 2"的更新示例:

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);
}
}

相关内容

  • 没有找到相关文章

最新更新