使用BoxLayout动态扩展JPanel(在空布局上) [英] Dynamically growing JPanel with BoxLayout (on a null layout)

查看:166
本文介绍了使用BoxLayout动态扩展JPanel(在空布局上)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有垂直BoxLayout的JPanel,它位于一个具有空布局的JPanel之上.

I have a JPanel with a vertical BoxLayout on top of a JPanel with a null layout.

我希望带有BoxLayout的JPanel随着组件的添加而增长.

I would like the JPanel with the BoxLayout to grow as the components are being added.

查看此代码:

public static void main (String[] args) {
    JFrame f = new JFrame();
    f.setSize(500,500);
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    JPanel total = new JPanel();
    total.setLayout(null);
    total.setSize(f.getWidth(),f.getHeight());
    total.setBackground(Color.green);
    JPanel box = new JPanel();
    box.setLocation(100,200);
    box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
    box.add(new JButton("test"));
    box.add(new JLabel("hey"));
    total.add(box);
    f.add(total);
    f.setVisible(true);
}

您会注意到没有组件出现.

You will notice that no components show up.

如何使JPanel成为盒子",以便随着我添加更多组件(垂直添加)而使尺寸动态增加.

How can I make the JPanel "box" such that the size dynamically increases as I add more components (which are added vertically).

在先:我需要将框"的位置精确地设置为100,200,因此请不要建议我不要使用空布局.我必须使用空布局.空的总计"布局不会影响我的问题的答案,该问题集中在框"面板上.

IN ADVANCE: I need the position of "box" to be at exactly 100,200 so please do not suggest that I do not use null layout. I must use null layout. The null layout of "total" should not effect the answer to my question, which focuses on the "box" panel.

推荐答案

通过扔掉布局管理器,您突然对它的工作负责.我可能要添加的工作,这并不容易...

By throwing away the layout manager, you suddenly become responsible for it's job. A job I might add, which isn't easy ...

基本上,以您为例,您无法设置子组件的大小...

Basically, given you example, you've failed to set the size of the child component...

JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel total = new JPanel();
total.setLayout(null);
total.setSize(f.getWidth(), f.getHeight());
total.setBackground(Color.green);

JPanel box = new JPanel();
box.setLocation(100, 200);
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.add(new JButton("test"));
box.add(new JLabel("hey"));
box.setSize(100, 100);  // <-- Don't forget this..

total.add(box);
f.add(total);
f.setVisible(true);

我个人认为您是在自找麻烦,但是我会知道...

Personally, I think your asking for trouble, but what would I know...

一个更好的主意可能是使用EmptyBorder之类的东西来提供填充...

A better idea might be to use something like an EmptyBorder to providing padding...

JFrame f = new JFrame();
f.setSize(500, 500);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

JPanel total = new JPanel(new BorderLayout());
total.setSize(f.getWidth(), f.getHeight());
total.setBackground(Color.green);
total.setBorder(new EmptyBorder(100, 200, 100, 200));

JPanel box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
box.add(new JButton("test"));
box.add(new JLabel("hey"));

total.add(box);
f.add(total);
f.setVisible(true);

已更新为布局管理器"示例

现在,如果所有布局管理器都使您失望,则可以尝试编写自己的布局管理器.这具有null布局管理器所需的好处,以及集成到Swing的组件更改过程中的好处,而无需求助于ComponentListenersContainerListeners

Now, if all the layout managers are failing you, you could try writing your own. This has the benefits you need from the null layout manager and the benefits of intergrating into Swing's component changing process, without the need to resort to ComponentListeners and ContainerListeners

JPanel total = new JPanel();
total.setLayout(new SuperAwesomeBetterThenYoursLayout());

自定义布局管理器

public static class SuperAwesomeBetterThenYoursLayout implements LayoutManager {

    @Override
    public void addLayoutComponent(String name, Component comp) {
    }

    @Override
    public void removeLayoutComponent(Component comp) {
    }

    @Override
    public Dimension preferredLayoutSize(Container parent) {
        return new Dimension(100, 300);
    }

    @Override
    public Dimension minimumLayoutSize(Container parent) {
        return new Dimension(100, 300);
    }

    @Override
    public void layoutContainer(Container parent) {
        boolean laidOut = false;
        for (Component child : parent.getComponents()) {
            if (child.isVisible() && !laidOut) {
                child.setLocation(200, 100);
                child.setSize(child.getPreferredSize());
            } else {
                child.setSize(0, 0);
            }
        }
    }

}

这基本上代表了您无论如何都要做的工作,但实际上与Swing的设计方式......

This basically represents the work you are going have to do anyway, but does in away that works with how Swing was designed...

这篇关于使用BoxLayout动态扩展JPanel(在空布局上)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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