Java GUI,BoxLayout添加面板

Java GUI,BoxLayout添加面板,java,swing,layout-manager,boxlayout,Java,Swing,Layout Manager,Boxlayout,总体上学习BoxLayout和GUI。我想在框架上放置一个面板。稍后,我将添加一个相同的面板,并测试BoxLaoyout。但我不明白为什么这个代码不能产生一个面板大小的xx400,而只是在框架左侧的中间点有一个红色点(坐标大约为(300,0))。p> 布局管理器(BoxLayout)正在使用其管理的容器组件的首选大小。默认情况下,空JPanel的首选大小为0x0,添加边框会产生更接近2x2的首选大小 使用布局管理器时,调用setSize没有意义,因为当容器重新验证时,布局管理器将覆盖您指定的任何

总体上学习BoxLayout和GUI。我想在框架上放置一个面板。稍后,我将添加一个相同的面板,并测试BoxLaoyout。但我不明白为什么这个代码不能产生一个面板大小的xx400,而只是在框架左侧的中间点有一个红色点(坐标大约为(300,0))。p> 布局管理器(
BoxLayout
)正在使用其管理的容器组件的首选大小。默认情况下,空
JPanel
的首选大小为0x0,添加边框会产生更接近2x2的首选大小

使用布局管理器时,调用
setSize
没有意义,因为当容器重新验证时,布局管理器将覆盖您指定的任何内容

已更新

看来这两个
BoxLayout
s的组合似乎在与您作对。如果我从
p1
中删除第二个
BoxLayout
,它似乎工作正常

另外,
BoxLayout
似乎希望使用组件的最大尺寸

import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class View extends JFrame {

    public View() {
        this.setPreferredSize(new Dimension(600, 600));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.pack();

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(Color.BLUE));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);

        JPanel p1 = new JPanel() {
            public Dimension getMaximumSize() {
                return new Dimension(200, 400);
            }
        };
        p1.setBorder(border);
        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));

        panel.add(p1);

        this.add(panel);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new View();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}

看(是的。)我已经尝试过首选尺寸。我没有使用setSize,而是使用了这行代码:p1.setPreferredSize(新维度(200400));但结果是一样的。
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.WindowConstants;
import javax.swing.border.BevelBorder;
import javax.swing.border.Border;
import javax.swing.border.LineBorder;

public class View extends JFrame {

    public View() {
        this.setPreferredSize(new Dimension(600, 600));
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        this.pack();

        JPanel panel = new JPanel();
        panel.setBorder(new LineBorder(Color.BLUE));
        panel.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

        Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);

        JPanel p1 = new JPanel() {
            public Dimension getMaximumSize() {
                return new Dimension(200, 400);
            }
        };
        p1.setBorder(border);
        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));

        panel.add(p1);

        this.add(panel);
        this.setVisible(true);
    }

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new View();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }
}