Java 在 JSplitPane 中调整 JPanel 的大小
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18021631/
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
Adjusting size of JPanels in JSplitPane
提问by Arun
Trying to create a JSplitPane with two JPanels. With the following code:
试图用两个 JPanel 创建一个 JSplitPane。使用以下代码:
JTable table = new JTable(qualifierModel);
table.setDefaultEditor(String.class, new QualifierCellEditor());
JPanel qualiferPanel = new JPanel();
JScrollPane jp = new JScrollPane(table);
qualiferPanel.add(new JLabel(Translator.getText("Select one qualifier for each section # from the table.")));
qualiferPanel.add(jp);
qualiferPanel.setVisible(true);
JToolBar btnBar = new JToolBar();
btnBar.setFloatable(false);
btnBar.add(Box.createHorizontalGlue());
btnBar.add(addItemButton);
btnBar.add(removeItemButton);
setLayout(new BorderLayout());
profilePanel.add(new JScrollPane(profileTable), BorderLayout.NORTH);
profilePanel.add(btnBar, BorderLayout.SOUTH);
JSplitPane spane = new JSplitPane(JSplitPane.VERTICAL_SPLIT);
spane.setTopComponent(profilePanel);
spane.setBottomComponent(qualiferPanel);
setLayout(new BorderLayout());
add(spane,BorderLayout.CENTER);
I have added two add buttons in the first JPanel which is not visible. How should I adjust the size of the first JPanel.
我在第一个不可见的 JPanel 中添加了两个添加按钮。我应该如何调整第一个 JPanel 的大小。
采纳答案by Lee Meador
You can adjust the position of the split pane divider by calling:
您可以通过调用来调整拆分窗格分隔线的位置:
spane.setDividerLocation(0.5);
Or you can rearrange the way it splits the space between the two parts with:
或者您可以重新排列它在两个部分之间分割空间的方式:
spane.setResizeWeight(1.0); // equal weights to top and bottom
You might want to figure out how to remove the blank space from the profile panel and that would help, too.
您可能想弄清楚如何从配置文件面板中删除空白区域,这也会有所帮助。
But I think the real problem might be the size of the frame itself. You didn't show that part of the code.
但我认为真正的问题可能是框架本身的大小。你没有显示那部分代码。
You might try enlarging the starting size of the JFrame and see how the layouts rearrange things.
您可以尝试扩大 JFrame 的起始大小并查看布局如何重新排列事物。
(On a side note: I usually fix up the buttons to be on the right side of their own little panel by using a flow layout with right justification.)
(旁注:我通常通过使用具有正确对齐的流布局将按钮固定在他们自己的小面板的右侧。)
回答by NiziL
Another way is to set the JPanel
's dimensions with setPreferredSizes(new Dimension(width, height))
and invoke resetToPreferredSizes()
on the JSplitPane
.
另一种方法是使用 设置JPanel
的维度setPreferredSizes(new Dimension(width, height))
并resetToPreferredSizes()
在 上调用JSplitPane
。
From the JSplitPane's javadoc
To resize the Components to their preferred sizes invoke resetToPreferredSizes.
When the user is resizing the Components the minimum size of the Components is used to determine the maximum/minimum position the Components can be set to. If the minimum size of the two components is greater than the size of the split pane the divider will not allow you to resize it. To alter the minimum size of a JComponent, see JComponent.setMinimumSize(java.awt.Dimension).
When the user resizes the split pane the new space is distributed between the two components based on the resizeWeight property. A value of 0, the default, indicates the right/bottom component gets all the space, where as a value of 1 indicates the left/top component gets all the space.
要将组件调整为它们的首选大小,请调用 resetToPreferredSizes。
当用户调整组件的大小时,组件的最小大小用于确定组件可以设置到的最大/最小位置。如果两个组件的最小大小大于拆分窗格的大小,则分隔符将不允许您调整其大小。要更改 JComponent 的最小大小,请参阅 JComponent.setMinimumSize(java.awt.Dimension)。
当用户调整拆分窗格的大小时,新空间会根据 resizeWeight 属性分布在两个组件之间。默认值为 0 表示右侧/底部组件获得所有空间,而值为 1 表示左侧/顶部组件获得所有空间。
Maybe you can use JSplitPane#setDividerLocation(aDouble)
too.