Java BoxLayout 拉伸组件以适应父面板
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2194998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
BoxLayout stretches component to fit parent panel
提问by Aly
Hi I am using a BoxLayout
to stack JPanel
s 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
stretches them so that together they occupy the the 500px space. Does anyone know how to disable this feature?
嗨,我正在使用 aBoxLayout
将JPanel
s堆叠在一起 ( BoxLayout.Y_AXIS
),例如,如果我的父JPanel
级高度为 500 像素,并且我向其添加了两个高度为 100 像素的子面板。在BoxLayout
绵延他们,让他们一起占据了500px的空间。有谁知道如何禁用此功能?
采纳答案by Ascalonian
Use GridBagLayoutinstead. You have much more control over your UI.
请改用GridBagLayout。您可以更好地控制 UI。
But if you want to use BoxLayout still, and don't want them to stretch, you can check out using invisible component fillerslike rigid areas, glue and fillers.
但是,如果您仍然想使用 BoxLayout,并且不希望它们被拉伸,您可以使用不可见的组件填充物(如刚性区域、胶水和填充物)进行检查。
回答by David Koelle
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.
您的面板正在拉伸,因为 BoxLayout 不会将每个面板限制为其首选大小。您需要找到符合组件首选大小的布局,就像 BorderLayout 的 NORTH 和 SOUTH 位置所做的那样。
Try this:
尝试这个:
- Create a JPanel with a BorderLayout. Add your child component as NORTH in this JPanel.
- Create a second JPanel for the other child component, add it as NORTH of a BorderLayout
- Add the two JPanels to your BoxLayout.
- 创建一个带有 BorderLayout 的 JPanel。在此 JPanel 中将您的子组件添加为 NORTH。
- 为另一个子组件创建第二个 JPanel,将其添加为 BorderLayout 的 NORTH
- 将两个 JPanel 添加到您的 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);
回答by M1EK
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.
正如前面的答案所提到的,诀窍是在盒子布局中使用胶水、填充物和刚性区域。不过,与那个响应者不同,我建议坚持使用 BoxLayout - 使用 Box 比使用 Grid Bag 可以更轻松地完成大多数简单的 UI;在典型的对话框中,额外的功能不会给您带来太多好处。
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).
在旧的习语中,这些是像 Box.createHorizontalStrut(int x) 和 Box.createHorizontalGlue() 之类的东西;这个想法是在第一个和第二个组件之间放置一个支柱,然后在第二个组件之后添加胶水。(现在“支柱”=“刚性区域”)。
回答by camickr
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:
BoxLayout 是少数尊重组件最小和最大尺寸的布局管理器之一。因此,如果您想防止面板拉伸,您可以使用:
panel.setMaximumSize( panel.getPreferredSize() );
回答by Armandt
This seems to work perfectly fine... using BoxLayout, as you wanted.
这似乎工作得很好……如您所愿,使用 BoxLayout。
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);