java which layout manager suitable for this task?

后端 未结 4 1042
星月不相逢
星月不相逢 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: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) ;
    

提交回复
热议问题