为什么需要使用BoxLayout调用setLayout? [英] Why do you need to invoke setLayout with BoxLayout?

查看:169
本文介绍了为什么需要使用BoxLayout调用setLayout?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数布局管理器都有无参数构造函数(也就是说,你可以用 new FlowLayout()创建一个FlowLayout,一个带有的GridLayout> new GridLayout (),一个GridBagLayout,带有 new GridBagLayout(),等等。但是, BoxLayout 要求您传递它将要管理的容器以及应该布置组件的轴。

Most layout managers have no-argument constructors (that is, you can create a FlowLayout with new FlowLayout (), a GridLayout with new GridLayout (), a GridBagLayout with new GridBagLayout (), etc.). However, BoxLayout requires that you pass both the container that it will be managing and the axis along which the components should be laid out.

我的问题是:既然你已经告诉布局经理要布局哪个组件,为什么还要写

My question is: since you're already telling the layout manager which component to lay out, why do you need to write

BoxLayout bl = new BoxLayout(myPanel, BoxLayout.Y_AXIS);
myPanel.setLayout(bl);

而不仅仅是第一行?

我快速浏览了 BoxLayout 源代码,看到我使用的构造函数(第178-185行)没有调用 target.setLayout(this)或任何类似的东西。看起来添加它真的很简单。有没有理由为什么它不包含在Swing库中?

I took a quick look at the BoxLayout source code and saw that the constructor I use (lines 178-185) doesn't make a call to target.setLayout(this) or anything of the sort. It seems like it would be really simple to just add that. Is there a reason why it's not included in the Swing library?

如果重要,我正在使用


java version 1.7.0

java version 1.7.0

Java(TM)SE运行时环境(版本1.7.0-b147)

Java(TM) SE Runtime Environment (build 1.7.0-b147)

在Win7Pro上。

谢谢!

SSCCE:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class BoxLayoutSSCCE extends JFrame {

    // Change this to see what I mean
    public static final boolean CALL_SET_LAYOUT = true;

    public BoxLayoutSSCCE () {
        super("Box Layout SSCCE");
        JPanel panel = new JPanel();
        BoxLayout bl = new BoxLayout(panel, BoxLayout.Y_AXIS);
        if (CALL_SET_LAYOUT) {
            panel.setLayout(bl);
        }
        panel.add(new JButton("Button 1"));
        panel.add(new JButton("Button 2"));
    }

    public static void main (String[] args) {
        BoxLayoutSSCCE blsscce = new BoxLayoutSSCCE();
        blsscce.pack();
        blsscce.setVisible(true);
    }
}


推荐答案

容器必须存在才能传递到 BoxLayout 。通常一个人写这样的东西:

The Container must exist before it can be passed to BoxLayout. Typically one writes something like this:

JPanel myPanel = new JPanel();
BoxLayout bl = new BoxLayout(myPanel, BoxLayout.Y_AXIS);
myPanel.setLayout(bl);

结合最后两行是很诱人的,但最不惊讶的原则表明布局的构造函数应该是否则会改变容器的状态。

It's tempting to combine the last two lines, but the principle of least astonishment suggests that the layout's constructor should not otherwise alter the container's state.

通常, javax.swing.Box 提供使用 BoxLayout 的轻量级容器 object作为其布局管理器。

Convenienly, javax.swing.Box provides "A lightweight container that uses a BoxLayout object as its layout manager."

public class Box extends JComponent implements ... {

    public Box(int axis) {
        super();
        super.setLayout(new BoxLayout(this, axis));
    }
}

现在只需一行:

Box myBox = new Box(BoxLayout.Y_AXIS);

这篇关于为什么需要使用BoxLayout调用setLayout?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆