GUI,BoxLayout添加面板

时间:2014-07-19 09:42:35

标签: java swing layout-manager boxlayout

一般学习BoxLayout和GUI。我想在一个框架上放置一个面板。稍后我会添加一个相同的面板,并测试BoxLaoyout。但我无法理解为什么这个代码不会产生200x400的面板,而只是框架左侧中间的红点(坐标大约为(300,0))。

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.setLayout(new BoxLayout(panel, BoxLayout.X_AXIS));

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

        JPanel p1 = new JPanel();
        p1.setSize(200, 400);
        p1.setBorder(border);
        p1.setLayout(new BoxLayout(p1, BoxLayout.Y_AXIS));


        panel.add(p1);

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

}

1 个答案:

答案 0 :(得分:2)

布局管理器(BoxLayout)正在使用其管理的容器组件的首选大小。默认情况下,空JPanel的首选大小为0x0,在边框中添加产生的首选大小接近2x2

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

<强>更新

似乎两个BoxLayout的组合似乎与你对抗。如果我从BoxLayout删除第二个p1,它似乎可以正常工作。

此外,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);
            }
        });
    }
}