了解BoxLayout

时间:2014-07-19 18:56:39

标签: java swing layout-manager boxlayout

你能帮我理解BoxLayout吗?我想为来自BoxLayout的javadoc组织类似的东西

拜托,看看图片

enter image description here

好吧,两张矩形面板如图所示。但添加第一个方块会带来意想不到的东西。第一块面板在正方形的一侧变长。添加另一个广场更加破坏了图片。好吧,添加第三个方格会得到this picture。框架被手动拉伸到全屏,而最初的图片应该只有600 x 600.如果我不拉伸窗户,第一个面板占据整个空间从上到下展出一个半正方形。

你能帮我理解我该怎样做才能得到理想的结果吗?

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

        // LayoutManager for the whole frame.
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));


        JPanel firstPanel = getPanel(200, 400);

        JPanel squareOne = getPanel(100, 100);
        JPanel squareTwo = getPanel(100, 100);
        JPanel squareThree = getPanel(100, 100);

        //firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareOne);
        //firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareTwo);
        //firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareThree);
        //firstPanel.add(Box.createVerticalGlue());


        JPanel secondPanel = getPanel(200, 400);


        this.add(Box.createHorizontalGlue());
        this.add(firstPanel);
        this.add(Box.createHorizontalGlue());
        this.add(secondPanel);
        this.add(Box.createHorizontalGlue());

        this.setVisible(true);
    }

    private JPanel getPanel(int width, int height) {
        Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);
        JPanel panel = new JPanel();
        panel.add(Box.createRigidArea(new Dimension(width, height)));
        panel.setBorder(border);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        return panel;
    }
}

稍后添加:

我改变了下面的代码。图片没有改变。

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

        // LayoutManager for the whole frame.
        this.setLayout(new BoxLayout(this.getContentPane(), BoxLayout.X_AXIS));


        MyPanel firstPanel = getPanel(200, 400);

        MyPanel squareOne = getPanel(100, 100);
        MyPanel squareTwo = getPanel(100, 100);
        MyPanel squareThree = getPanel(100, 100);

        //firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareOne);
        firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareTwo);
        firstPanel.add(Box.createVerticalGlue());
        firstPanel.add(squareThree);
        firstPanel.add(Box.createVerticalGlue());


        MyPanel secondPanel = getPanel(200, 400);


        this.add(Box.createHorizontalGlue());
        this.add(firstPanel);
        this.add(Box.createHorizontalGlue());
        this.add(secondPanel);
        this.add(Box.createHorizontalGlue());
//
        this.setVisible(true);
    }

    private class MyPanel extends JPanel{
        int width;
        int height;
        public MyPanel(int width, int height){
            this.width = width;
            this.height = height;
        }
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(width, height);
        }
    }
    private MyPanel getPanel(int width, int height) {
        Border border = BorderFactory.createBevelBorder(BevelBorder.RAISED, Color.RED, Color.BLACK);
        MyPanel panel = new MyPanel(width, height);
        panel.add(Box.createRigidArea(new Dimension(width, height)));
        panel.setBorder(border);
        panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
        return panel;
    }
}

1 个答案:

答案 0 :(得分:0)

如果需要,覆盖getPreferredSize()以设置JPanel的首选大小。

JPanel panel =  new JPanel() {

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

了解更多Should I avoid the use of set(Preferred|Maximum|Minimum)Size methods in Java Swing?