java which layout manager suitable for this task?

后端 未结 4 1045
星月不相逢
星月不相逢 2021-01-13 17:51

I have a JPanel parent with 3 JPanel children inside. They all currently make use of GridLayout and together represent a UML class. Problem is that when I add a new attribu

4条回答
  •  爱一瞬间的悲伤
    2021-01-13 18:25

    maybe this by using BorderLayout you can do that

    (example about basic stuff revalidate(); + repaint(); versus pack();)

    enter image description here enter image description here enter image description here

    from code

    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import javax.swing.border.LineBorder;
    
    public class AddComponentsAtRuntime {
    
        private JFrame f;
        private JPanel panel;
        private JCheckBox checkValidate, checkReValidate, checkRepaint, checkPack;
    
        public AddComponentsAtRuntime() {
    
            JButton b = new JButton();
            b.setBackground(Color.red);
            b.setBorder(new LineBorder(Color.black, 2));
            b.setPreferredSize(new Dimension(600, 10));
            panel = new JPanel(new GridLayout(0, 1));
            panel.add(b);
            JPanel panelnorth = new JPanel();
            panelnorth.setPreferredSize(panel.getPreferredSize());
            f = new JFrame();
            f.setLayout(new BorderLayout(5, 5));
            f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            f.add(panelnorth,BorderLayout.NORTH);
            f.add(panel,BorderLayout.CENTER);
            f.add(getCheckBoxPanel(),BorderLayout.SOUTH);
            f.setLocation(200, 200);
            f.pack();
            f.setVisible(true);
        }
    
        private JPanel getCheckBoxPanel() {
            checkValidate = new JCheckBox("validate");
            checkValidate.setSelected(false);
            checkReValidate = new JCheckBox("revalidate");
            checkReValidate.setSelected(false);
            checkRepaint = new JCheckBox("repaint");
            checkRepaint.setSelected(false);
            checkPack = new JCheckBox("pack");
            checkPack.setSelected(false);
            JButton addComp = new JButton("Add New One");
            addComp.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    JButton b = new JButton();
                    b.setBackground(Color.red);
                    b.setBorder(new LineBorder(Color.black, 2));
                    b.setPreferredSize(new Dimension(600, 10));
                    panel.add(b);
                    makeChange();
                    System.out.println(" Components Count after Adds :" + panel.getComponentCount());
                }
            });
            JButton removeComp = new JButton("Remove One");
            removeComp.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent e) {
                    int count = panel.getComponentCount();
                    if (count > 0) {
                        panel.remove(0);
                    }
                    makeChange();
                    System.out.println(" Components Count after Removes :" + panel.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()) {
                panel.validate();
            }
            if (checkReValidate.isSelected()) {
                panel.revalidate();
            }
            if (checkRepaint.isSelected()) {
                panel.repaint();
            }
            if (checkPack.isSelected()) {
                f.pack();
            }
        }
    
        public static void main(String[] args) {
            AddComponentsAtRuntime makingChanges = new AddComponentsAtRuntime();
        }
    }
    

提交回复
热议问题