如何防止Boxlayout / Box拉伸儿童组件? [英] How to prevent Boxlayout/Box from stretching children components?

查看:138
本文介绍了如何防止Boxlayout / Box拉伸儿童组件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如您在下面的可运行代码中所看到的,我尝试使用具有可扩展子框的Box。儿童盒可以改变它们的大小,这一切都很好。主要问题是大小始终相对于父级。但我希望它们具有特定的大小,以防万一没有地方使用JScrollPane。目前他们只收缩其他儿童盒。

As you can see in the runnable code below, i try to have a Box with expandable child-boxes. The children Boxes can change they size and this all works good. The main problem is that the size is always relative to the parent. But I want them to have a specific size and in case there is no place anymore use the JScrollPane. At the moment they shrink the other children-boxes only.

我尝试了Glue和Filler,但它没有用。胶水没有任何影响,填料具有副作用,始终保持在某些位置(即使ScrollPane正在运行)。拥有如此多的免费空间真是难看。

I tried Glue and Filler, but it didn't work. The glue just had no effect and the filler had the side effect to keep always some place at the (even when the ScrollPane is in action). That is pretty ugly to have there so much free space.

那么,你知道防止盒子伸展孩子的好方法吗?

So, do you know a good way to prevent the Boxes from stretching the children?

提前谢谢!

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.border.TitledBorder;


public class ExpandableMenueDemo {
    Box allBoxes; 
    ExpandableMenueDemo(){
        allBoxes = Box.createVerticalBox();

        TitledBorder title;
        title = BorderFactory.createTitledBorder("Filter");
        allBoxes.setBorder(title);

        for (int i = 0 ;i<3;i++){
            //generate collapsable components
            SubBox b = new SubBox("SubBox"+i);
            allBoxes.add(b.getSwingBox());

        }
        allBoxes.add(Box.createVerticalGlue());
    }

    public Container getMenue(){
        return allBoxes;
    }

    public static void main(String[] args) {
        JFrame frame = new JFrame();
        ExpandableMenueDemo m = new ExpandableMenueDemo();
        Box mainBox = Box.createHorizontalBox();

        mainBox.add(new JScrollPane(m.getMenue()));
        mainBox.add(new JTable(20,5));

        frame.setContentPane(mainBox);
        frame.pack();
        frame.setVisible(true);
    }


    class SubBox{
        Box box;
        Box header; 
        String name; 
        JButton cBtn;
        boolean isCollapsed = true;
        JLabel headerLine; 

        SubBox(String name) {
            this.name= name;
            box = Box.createVerticalBox();

            headerLine = new JLabel(name+" () :");

            header= Box.createHorizontalBox();
            cBtn = new JButton("v");
            cBtn.addActionListener(new ActionListener(){

                @Override
                public void actionPerformed(ActionEvent arg0) {
                    if (isCollapsed)show();
                    else collapse();
                }
            });
            collapse();

            header.add(cBtn);
            header.add(Box.createHorizontalStrut(10));
            header.add(headerLine);
            header.add(Box.createHorizontalGlue());

        }

        Box getSwingBox() {
            Box b = Box.createVerticalBox();    
            b.add(header);
            b.add(box);
            return b;
        }

        public void collapse(){
            System.out.println("collapse");
            box.removeAll();
            this.isCollapsed=true;
            cBtn.setText("v");
        }

        public void show(){
            System.out.println("show");
            box.removeAll();
            this.isCollapsed=false;
            cBtn.setText("^");

            for (int i = 0; i<3;i++) { 
               Box b = Box.createHorizontalBox();
               b.add(Box.createHorizontalStrut(20));
               b.add(new JCheckBox("checkBox "+i));
               b.add(Box.createHorizontalGlue());
               box.add(b);
            }
        }
    }
}


推荐答案

您可以考虑使用 SwingX中包含的JXTaskPane / -Container,而不是重新发明轮子。 。它的演示显示它在左侧的行动(作为taskPanes) - 那是选择演示的部分。

Instead of re-inventing the wheel, you might consider to use JXTaskPane/-Container contained in SwingX. Its demo shows it in action (as taskPanes) at the left - that's the part for choosing the demos.

这篇关于如何防止Boxlayout / Box拉伸儿童组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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