Auto resize swing elements to fit to the container

2020-02-05 12:58发布

Not sure if what I need is possible. I have a container (JPanel) that contains some internal elements. I was wondering if it is possible to force internal elements to fit into the container's size. I need them to be fully visible i.e., resize to fit inside the Panel's size and not cut some parts of the internal elements.

Scrolling is not an option.

Is this possible by using a Layout or something?

EDIT: Important clarification: The thing is that I do not have access to the internal elements neither to their properties so I would say that a Layoutmanager capable of resizing child elements to fit to its size is needed. I tested BorderLayout and GridBagLayout but the result is always the same, the internal elements are cut out.

标签: java swing
5条回答
Root(大扎)
2楼-- · 2020-02-05 12:59

It's for exactly that reason that LayoutManagers exist. All the LayoutManagers work for simple containers directly, excluding GridBagLayout which is to able to handle most complete GUIs directly.

For most compete GUI you have some choices as follows:

  • Look for a 3rd part layout such as MigLayout or here
  • Use GridBagLayout
  • Very easy way is use nested layout, where there is more than one JPanel and each has child JPanels with same or different LayoutManager
  • Or custom layout, should be hard..., but same as using GridBagLayout
查看更多
劫难
3楼-- · 2020-02-05 13:07

You could set the JPanel layout to border layout, then add the single child to the center. If there are multiple children, this approach becomes less useful since components added to the the NORTH, SOUTH, EAST, and WEST will remain statically sized while the centre resizes to fill the remainder.

In short, this isn't an ideal solution. All layouting in Swing is made all the more complex by the fact that different components behave in different ways, so you really need to provide further details of the child components you wish to add to your panel, and any behaviour that has been overridden on those components.

The best way is to try a couple of simple examples to see what mileage you get and whether subtle redesign of your child component nesting could help.

查看更多
▲ chillily
4楼-- · 2020-02-05 13:07

I was using a JInternalFrame inside JDesktopPane. I wanted the internal_frame to auto resize as desktop pane is resized, so I had to implement the AncestorResized event for the internal frame where I placed the following code:

 this.setPreferredSize(this.getParent().getPreferredSize());
 this.pack();

and it worked for me :)

查看更多
孤傲高冷的网名
5楼-- · 2020-02-05 13:12

you can use a layout, like GridBagLayout, or BorderLayout depending on the situation. With proper weights it is possible.

查看更多
冷血范
6楼-- · 2020-02-05 13:15

this sounds to me like you should just peek an appropriate layout manager and use it. For example, look at BorderLayout - put your component in the CENTER and it will occupy all the area. Its up to each concrete layout manager to decide what will be the size of the components. Mark

查看更多
登录 后发表回答