java which layout manager suitable for this task?

后端 未结 4 1040
星月不相逢
星月不相逢 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:11

    You can use a GridBagLayout for this. I think what worked for me was

    • add a fourth sub-panel to the top panel
    • set the weighty for the top three panels to 0
    • set the weighty for the bottom (new) panel to 1
    • depending on what technique you are using to render the text in the sub-panels, you may need to set the preferred size of sub-panels explicitly.
    • when you add new items to the sub-panels, you'll need to invalidate and re-layout the whole thing.
    0 讨论(0)
  • 2021-01-13 18:13

    I suggest you to use BoxLayout. Here's a tutorial. Playing around with glues and rigid area you can obtain almost all desired layout. In your case the code should be somthing like this:

    JPanel container = new JPanel();
    container .setLayout(new BoxLayout(container , BoxLayout.Y_AXIS));
    
    JPanel childTop = new JPanel();
    JPanel childCenter = new JPanel();
    JPanel childBottom = new JPanel();
    
    
    childTop.setMaximumSize(...);
    childBottom.setMaximumSize(...);
    
    container.add(childTop);
    container.add(Box.createVerticalGlue());
    container.add(childCenter);
    container.add(Box.createVerticalGlue());
    container.add(childBottom);
    

    When you need to insert a new child, remember to insert it into the right position: between one of the glues and childCenter. For example:

    container.add(newChild, 2) ;
    
    0 讨论(0)
  • 2021-01-13 18:19

    Unfortunately I am not 100% sure I understood your layout. It will be better if you can attach picture.

    But if Grid Layout grows not as you want you can either use GridBagLayout that can do almost everything but a little bit complicated or use a combination of Border and other layouts.

    Probably several border layout can help you. In this layout center is growing fast while north, south, west and east are growing slower. Try to use them.

    Check BoxLayout too.

    If you are going to create a lot of complicated dialog boxes check out MigLayout. Although some learning curve exists this layout can really help you to save hours and days.

    Since you updated your question now I can definitely say that you can define BorderLayout, with North, South and Center.

    Center will contain Grid layout with 2 rows. Each row will contain Border layeout again. The upper border layout will contain other pannel in the south. The lower border layout willcontaint border layout in the North.

    Each one of that layouts will contain yet another border layouy with label in the west and text field in the center.

    0 讨论(0)
  • 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();
        }
    }
    
    0 讨论(0)
提交回复
热议问题