Giving a JPanel a percentage-based width

后端 未结 3 512
猫巷女王i
猫巷女王i 2021-01-14 08:27

What\'s the easiest way to make a JPanel that takes up a fixed percentage of its parent container, by width?

Its width should update when that of its pa

3条回答
  •  天涯浪人
    2021-01-14 08:43

    You could use the Relative Layout. This layout was designed for this purpose since none of the layouts from the JDK support this directly (and easily).

    As a simple example you could have 3 panels at 25%, 25% and 50% of the width:

    RelativeLayout rl = new RelativeLayout(RelativeLayout.X_AXIS);
    rl.setFill( true );
    JPanel panel = new JPanel( rl );
    panel.add(red, new Float(25);
    panel.add(green, new Float(25));
    panel.add(blue, new Float(50));
    

提交回复
热议问题