質問

I want to have a vertical FlowLayout to host my JPanels. A lot of ppl suggest using BoxLayout. However, I realize its behavior is not exactly same as FlowLayout

FlowLayout

enter image description here

enter image description here

BoxLayout with Y axis

enter image description here

enter image description here

As you can see, in FlowLayout, when I stretch parent panel's width, its child panels' width remains the same.

However, in BoxLayout, when I stretch parent panel's height, its child panels' height changed!. This seems to have similar behavior as 1 column 2 rows GridLayout. This is not what I want.

Is there any way to prevent this?

I try to have vertical filler on the top and bottom of parent panel.

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

But it doesn't help much. My 2 child panels' height still stretch along when I change parent's height.

役に立ちましたか?

解決

  • see how BoxLayout(accepting min, max and preferred size, then resize depends of this value) works,

  • in compare with FlowLayout (accepting only PreferredSize, child arent resizable with container)

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

他のヒント

A simple and flexible way to achieve this behaviour, is to use 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();
            }
        });
    }

}
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top