3

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

流式布局

在此处输入图像描述

在此处输入图像描述

带 Y 轴的 BoxLayout

在此处输入图像描述

在此处输入图像描述

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

但是,BoxLayout当我拉伸父面板的高度时,它的子面板的高度发生了变化!. 这似乎与 1 column 2 rows 具有相似的行为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 个子面板的高度仍然延伸。

4

3 回答 3

2
  • 查看 BoxLayout(接受最小、最大和首选大小,然后根据该值调整大小)的工作原理,

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

在此处输入图像描述

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();
            }
        });
    }
}
于 2013-09-11T08:28:27.000 回答
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();
            }
        });
    }

}
于 2013-09-11T14:03:57.020 回答
1

您必须使用 Glue(请参阅:http ://docs.oracle.com/javase/tutorial/uiswing/layout/box.html#glue )。

于 2013-09-11T08:19:36.960 回答