使用BoxLayout作为垂直FlowLayout来保存JPanel

时间:2013-09-11 08:02:25

标签: java swing layout-manager flowlayout boxlayout

我希望有一个垂直FlowLayout来托管我的JPanels。很多人建议使用BoxLayout。但是,我意识到它的行为与FlowLayout

不完全相同

的FlowLayout

enter image description here

enter image description here

Y轴的BoxLayout

enter image description here

enter image description here

如您所见,在FlowLayout中,当我拉伸父面板的宽度时,其子面板的宽度保持不变。

但是,在BoxLayout中,当我拉伸父面板的高度时,其子面板的高度发生了变化!。这似乎与1列2行GridLayout具有相似的行为。这不是我想要的。

有没有办法阻止这种情况?

我尝试在父面板的顶部和底部使用垂直填充。

new javax.swing.Box.Filler(new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 0), new java.awt.Dimension(0, 32767));

但它没有多大帮助。当我改变父母的身高时,我的2个儿童面板的高度仍然会延伸。

3 个答案:

答案 0 :(得分:2)

  • 了解BoxLayout(接受min,max和首选大小,然后调整大小取决于此值)的工作方式,

  • 与FlowLayout比较(仅接受PreferredSize,子容器不能用容器调整大小)

enter image description here

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BoxStructAndJComponents {

    private JFrame frame;
    private JPanel intro;
    private JPanel name;

    public BoxStructAndJComponents() {
        frame = new JFrame("JFrame");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JComponent newContentPane = createUI();
        newContentPane.setOpaque(true);
        frame.setContentPane(newContentPane);
        frame.pack();
        frame.setVisible(true);
    }

    private JPanel createUI() {
        intro = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(100, 100);
            }
        };
        intro.setBackground(Color.red);
        //intro.setLabelFor(name);
        name = new JPanel() {
            @Override
            public Dimension getMinimumSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getPreferredSize() {
                return new Dimension(100, 100);
            }

            @Override
            public Dimension getMaximumSize() {
                return new Dimension(100, 100);
            }
        };
        name.setBackground(Color.blue);
        final JButton button = new JButton("Pick a new name...");
        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
            }
        });
        JPanel panel = new JPanel();
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));
        panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 10, 20));
        intro.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        name.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        button.setAlignmentX(JComponent.CENTER_ALIGNMENT);
        panel.add(intro);
        //panel.add(Box.createVerticalStrut(5));
        panel.add(Box.createHorizontalStrut(5));
        panel.add(name);
        panel.add(Box.createRigidArea(new Dimension(150, 10)));
        panel.add(button);
        return panel;
    }

    public static void main(String[] args) {
        javax.swing.SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                BoxStructAndJComponents listDialogRunner = new BoxStructAndJComponents();
            }
        });
    }
}

答案 1 :(得分:1)

答案 2 :(得分:1)

实现此行为的一种简单而灵活的方法是使用GridBagLayout

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class TestGridBagLayout {

    protected void initUI1() {
        final JFrame frame = new JFrame("Grid bag layout");
        frame.setTitle(TestGridBagLayout.class.getSimpleName());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel(new GridBagLayout());
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        // gbc.weighty = 1.0; Uncomment this line if you want the labels to spread vertically
        for (int i = 0; i < 10; i++) {
            panel.add(new JLabel("Label " + (i + 1)), gbc);
        }
        frame.add(panel);
        frame.pack();
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                TestGridBagLayout test = new TestGridBagLayout();
                test.initUI1();
            }
        });
    }

}