开发者

BoxLayout stretches component to fit parent panel

开发者 https://www.devze.com 2022-12-18 20:15 出处:网络
Hi I am using a BoxLayout to stack JPanels on top of each other (BoxLayout.Y_AXIS), for example if my parent JPanel is of height 500 pixels and I add two child panels to it both of height 100 pixels.

Hi I am using a BoxLayout to stack JPanels on top of each other (BoxLayout.Y_AXIS), for example if my parent JPanel is of height 500 pixels and I add two child panels to it both of height 100 pixels. The BoxLayout开发者_JAVA百科 stretches them so that together they occupy the the 500px space. Does anyone know how to disable this feature?


BoxLayout is one of the few layout managers that respects the minimum and maximum sizes of a component. So if you want to prevent a panel from stretching you can use:

panel.setMaximumSize( panel.getPreferredSize() );


Use GridBagLayout instead. You have much more control over your UI.

But if you want to use BoxLayout still, and don't want them to stretch, you can check out using invisible component fillers like rigid areas, glue and fillers.


The trick is, as the previous answer mentioned, to use the glue, fillers, and rigid areas in the box layout. Unlike that responder, though, I'd recommend sticking with BoxLayout - you can accomplish most simple UIs easier with Box than with the Grid Bag; and the extra power doesn't buy you much in your typical dialog box.

In the old idiom, these were things like Box.createHorizontalStrut(int x) and Box.createHorizontalGlue(); the idea is that you put a strut between your first and second components and then add a glue after the second one. ("strut" = "rigid area" nowadays).


This seems to work perfectly fine... using BoxLayout, as you wanted.

BoxLayout stretches component to fit parent panel

    this.setLayout(new FlowLayout()); // this being the JFrame

    JPanel panel = new JPanel();
    panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
    panel.setPreferredSize(new Dimension(500, 500));
    panel.setBackground(Color.orange);
    this.add(panel); // add the parent to the JFrame

    JPanel pnlChild1 = new JPanel();
    pnlChild1.setBackground(Color.cyan);
    pnlChild1.setMaximumSize(new Dimension(200, 100));

    JPanel pnlChild2 = new JPanel();
    pnlChild2.setBackground(Color.magenta);
    pnlChild2.setMaximumSize(new Dimension(200, 100));

    panel.add(pnlChild1);
    panel.add(pnlChild2);


Your panels are stretching because BoxLayout does not constrain each panel to its preferred size. You need to find layouts that do respect a component's preferred size, as BorderLayout's NORTH and SOUTH positions do.

Try this:

  1. Create a JPanel with a BorderLayout. Add your child component as NORTH in this JPanel.
  2. Create a second JPanel for the other child component, add it as NORTH of a BorderLayout
  3. Add the two JPanels to your BoxLayout.

Code:

JPanel panel1 = new JPanel(new BorderLayout());
panel1.add(component1, BorderLayout.NORTH);
JPanel panel2 = new JPanel(new BorderLayout());
panel2.add(component2, BorderLayout.NORTH);

JPanel boxPanel = new JPanel();
BoxLayout boxLayout = new BoxLayout(boxPanel, BoxLayout.Y_AXIS);
boxPanel.setLayout(boxLayout);
boxPanel.add(panel1);
boxPanel.add(panel2);
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号