自动调整摇摆元素的大小以适合容器的大小 [英] Auto resize swing elements to fit to the container's size
问题描述
不确定我需要什么.我有一个包含一些内部元素的容器(JPanel).我想知道是否有可能迫使内部元素适应容器的大小.我需要使它们完全可见,即调整其大小以适合面板的尺寸,并且不切掉内部元素的某些部分.
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.
不能滚动.
是否可以通过使用Layout或其他方式实现?
Is this possible by using a Layout or something?
重要说明:事实是,我无法访问内部元素,也无法访问其内部属性,因此我想说需要一个能够调整子元素大小以适合其大小的Layoutmanager.我测试了BorderLayout和GridBagLayout,但结果始终相同,内部元素被剪掉.
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.
推荐答案
正是出于这个原因, LayoutManagers 存在.所有 LayoutManagers
都直接用于简单容器,但 GridBagLayout
除外,后者可以直接处理大多数完整的GUI.
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.
对于大多数竞争性GUI,您可以选择以下选项:
For most compete GUI you have some choices as follows:
- 寻找第三部分布局,例如 MigLayout 或此处
- 使用 GridBagLayout
- 一种非常简单的方法是使用嵌套布局,其中有多个
JPanel
,每个都有子JPanels
,它们具有相同或不同的LayoutManager
- 或者自定义布局,应该很难.,但与使用
GridBagLayout
- 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 childJPanels
with same or differentLayoutManager
- Or custom layout, should be hard..., but same as using
GridBagLayout
这篇关于自动调整摇摆元素的大小以适合容器的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!