具有固定大小的动态GridLayout

时间:2014-11-07 08:44:42

标签: java swing user-interface layout-manager grid-layout

需要指导..

想要实现类似下图的图像,其中子面板保持相同的尺寸,但包含多达4个组件。我意识到我可以通过更改下面的gridlayout中的列数来实现这一点,但是为了保持子面板的大小相同,我必须更改边框大小,这是我不介意做的事情但是看起来有点麻烦我想知道是否有一个聪明的方法来解决这个问题。我提供的代码基于提供给我的示例代码here

enter image description here

import java.awt.*;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DynamicGridLayout {

    private JPanel ui = null;

    DynamicGridLayout() {
        initUI();
    }

    public final void initUI() {
        if (ui!=null) return;
        ui = new JPanel(new GridBagLayout());
        ui.setBorder(new TitledBorder("Parent Panel"));

        JPanel controls = new JPanel(new GridLayout(2,0,10,10));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));

        for (int ii=1; ii<5; ii++) {
            addLabel(controls, "String " + ii);
        }
    } 

    public JComponent getUI() {
        return ui;
    }

    private void addLabel(JPanel panel, String text) {
        JPanel controls1 = new JPanel(new GridLayout(3,0,3,3));
        controls1.setBackground(Color.green);
        controls1.setBorder(new EmptyBorder(75,75,75,75));
        panel.add(controls1);
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {

            @Override
            public void run() {
                JFrame f = new JFrame("Three Button/Text Field Combo");
                f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                f.setLocationByPlatform(true);
                DynamicGridLayout dgl = new DynamicGridLayout();
                f.setContentPane(dgl.getUI());
                f.setSize(1050, 720);
                f.setMinimumSize(f.getSize());
                f.setVisible(true);
            }
        };
        SwingUtilities.invokeLater(r);
    }
}

1 个答案:

答案 0 :(得分:9)

  • 只是为了我的享受,有趣,

  • 注意必须在LayoutManager从GridLayout切换回BorderLayout(不需要的输出,见图5)后通知(最小的孩子需要覆盖min / max / preferredsize)父JPanel。

enter image description here

enter image description here

enter image description here

enter image description here

enter image description here

来自

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;

public class DynamicGridLayout {

    private JFrame f = new JFrame("Three Button/Text Field Combo");
    private JPanel ui = new JPanel(new GridBagLayout()) {
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(400, 300);
        }

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

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(800, 600);
        }
    };
    private JPanel controls = new JPanel() {
        @Override
        public Dimension getMinimumSize() {
            return new Dimension(400, 300);
        }

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

        @Override
        public Dimension getMaximumSize() {
            return new Dimension(1050, 720);
        }
    };
    private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;

    DynamicGridLayout() {
        initUI();
    }

    public final void initUI() {
        ui.setBorder(new TitledBorder("Parent Panel"));
        ui.add(controls);
        controls.setBackground(Color.RED);
        controls.setBorder(new TitledBorder("Child Panel"));
        f.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        f.setLocationByPlatform(true);
        f.add(ui);
        f.add(getCheckBoxPanel(), "South");
        f.setMinimumSize(ui.getPreferredSize());
        f.setVisible(true);
    }

    public JComponent getUI() {
        return ui;
    }

    private void addLabel() {
        JPanel controls1 = new JPanel(new GridLayout(3, 0, 3, 3));
        controls1.setBackground(Color.green);
        controls1.setBorder(new EmptyBorder(75, 75, 75, 75));
        controls.add(controls1);
    }

    private JPanel getCheckBoxPanel() {
        checkValidate = new JCheckBox("validate");
        checkValidate.setSelected(false);
        checkReValidate = new JCheckBox("revalidate");
        checkReValidate.setSelected(true);
        checkRepaint = new JCheckBox("repaint");
        checkRepaint.setSelected(true);
        checkPack = new JCheckBox("pack");
        checkPack.setSelected(false);
        JButton addComp = new JButton("Add New One");
        addComp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (controls.getComponentCount() < 1) {
                    controls.setLayout(new BorderLayout());
                    addLabel();
                    makeChange();
                } else if (controls.getComponentCount() == 1) {
                    controls.setLayout(new GridLayout(0, 2, 10, 10));
                    addLabel();
                    makeChange();
                } else {
                    controls.setLayout(new GridLayout(2, 0, 10, 10));
                    addLabel();
                    makeChange();
                }
                System.out.println(" Components Count after Adds :" + controls.getComponentCount());
            }
        });
        JButton removeComp = new JButton("Remove One");
        removeComp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int count = controls.getComponentCount();
                if (count > 0) {
                    if (controls.getComponentCount() == 2) {
                        controls.setLayout(new BorderLayout());
                        controls.remove(0);
                    } else if (controls.getComponentCount() == 3) {
                        controls.setLayout(new GridLayout(0, 2, 10, 10));
                        controls.remove(0);
                    } else {
                        controls.remove(0);
                    }

                }
                makeChange();
                System.out.println(" Components Count after Removes :" + controls.getComponentCount());
            }
        });
        JPanel panel2 = new JPanel();
        panel2.add(checkValidate);
        panel2.add(checkReValidate);
        panel2.add(checkRepaint);
        panel2.add(checkPack);
        panel2.add(addComp);
        panel2.add(removeComp);
        return panel2;
    }

    private void makeChange() {
        if (checkValidate.isSelected()) {
            ui.validate();
        }
        if (checkReValidate.isSelected()) {
            ui.revalidate();
        }
        if (checkRepaint.isSelected()) {
            ui.repaint();
        }
        if (checkPack.isSelected()) {
            f.pack();
        }
    }

    public static void main(String[] args) {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                DynamicGridLayout dgl = new DynamicGridLayout();
            }
        };
        SwingUtilities.invokeLater(r);
    }
}