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

时间:2014-08-19 20:22:55

标签: java swing layout-manager flowlayout

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

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

以下是我的要求:

  • 我不知道在编译时我将在每个嵌套的JPanel中有多少组件
  • 用户应该能够调整窗口大小
  • 如果没有足够的空间在一行显示所有组件,嵌套的JPanel应垂直延伸
  • 如果有可用空间,嵌套的JPanel不应该垂直延伸(即我不想在嵌套的JPanel中有垂直的空白区域,但如果主框架中有一些空格那么就没问题了)< / LI>

对于嵌套的JPanel,在我看来,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);

}

}

如果你知道如何继续......

谢谢!

1 个答案:

答案 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);

}

}