布局管理器区域的动态调整大小
在哪个 Swing 布局管理器中可以通过编程方式更改布局区域?以及如何以最低的成本做到这一点?
我必须从头开始创建功能类似于 JSplitPane 但具有三个面板的组件。其中一个时刻是单击分隔板上的 oneTouchExpandable 按钮后展开/折叠其中一个面板。但问题是我不知道如何实现这个崩溃动作。我尝试将面板宽度设置为 0,但包含此面板的布局区域在组件之后不会缩小。我尝试在所有布局管理器中执行此操作,但效果是相同的。
谢谢。
In which Swing layout manager it is possible to change layout areas programmatically? And how to do this with lowest cost?
I have to create component with functionality similar to JSplitPane but with three panels from scratch. One of the moments is to Expand/Collapse one of the panels after clicking oneTouchExpandable button on the divider. But the problem is that I don't know how to implement this collapse action. I tried just setting panels width to 0, but the layout area which contains this panel doesn't shrink after the component. I tried to do this in all Layout Managers, but effect is the same.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
在 GUI 可见后进行影响面板布局的更改时,您需要 revalidate() 面板,这实际上会调用面板上的布局。在您的情况下,简单地使组件不可见可能会更容易:
When making a change that affects the layout of a panel after the GUI is visible you need to revalidate() the panel which essentially invoke the layout on the panel. In your case it might be easier to simply make the component invisible:
所有布局管理器都会动态调整大小。但是,宽度和高度属性是布局的结果,并且将被覆盖。
您应该查看的属性是
preferredSize、minimumSize
和maximumSize
- 布局管理器基于这些属性进行计算,尽管确切的效果取决于布局管理器(例如 BorderLayout)如果可能的话,将为 NORTH、SOUTH、WEST 和 EAST 组件提供首选大小,并将其余部分分配给 CENTER 组件)。更改大小属性后,您必须在容器上调用
revalidate()
,然后您应该会看到更改。All layout managers resize dynamically. However, the width and height properties are the result of the layouting, and will be overwritten.
The properties you should look at are
preferredSize, minimumSize
, andmaximumSize
- the layout managers base their calculations on those properties, though the exact effect depends on the layout manager (e.g. BorderLayout will give the NORTH, SOUTH, WEST and EAST components their preferred size if possibe and assign the remainder to the CENTER component).Once you've changed the size properties, you have to call
revalidate()
on the container, then you should see the changes.我同意 revalidate()/preferredSize 答案,但只是想建议这一点:不要重新发明轮子!使用 JideSplitPane(JIDE 的免费“公共层”的一部分) - 它支持两个以上的分割。
I'm with the revalidate()/preferredSize answers but just wanted to suggest this: don't re-invent the wheel! Use the JideSplitPane (part of JIDE's free "Common Layer") - it supports more than two splits.
感谢大家的回答。最后我最终结合了几个答案的解决方案。
我的最终解决方案如下:
我使用 BorderLayout,设置 West、Center 和 East 面板,然后通过将 PreferredSize 设置为 West 和 East 面板来控制它们的大小。渲染方案如下:在布局组件时,BorderLayout 为东面板和西面板指定其 PreferredSize,并将其余空间分配给中心面板。因此,通过一些简单的计算,我可以轻松地操纵三个面板中每个面板的大小。
我还向西面板和东面板添加了分隔线(最初只是具有固定大小的 JPanel 组件)(计算时也考虑了它们的大小)。对于动态调整大小,我处理此分隔线上的拖动事件并重新计算面板大小。
刷新是通过以下代码片段完成的:
容器.setVisible(假);
容器.revalidate();
容器.repaint();
容器.setVisible(true);
我想将此代码放在某个地方以供其他人使用,但不知道具体在哪里执行此操作。所以如果你知道这样的地方,请在评论中指出我。
Thanks to all for the answers. Finally I ended up with combining solutions from several answers.
My final solution is following:
I use BorderLayout, set West, Center and East panels and then manipulate their sizes by setting PreferredSize to West and East panels. The scheme of rendering is following: while laying out the components BorderLayout gives East and West panels their PreferredSize and rest of the space to Center panel. So with a bit of easy calculations I can manipulate size of each of three panels painlessly.
I also added dividers(originally just JPanel components with fixed size) to West and East panels(their size is also considered while calculating). For dynamic resize I handle dragging events on this dividers and recalculate panel sizes.
Refreshing is done with following snippet:
container.setVisible(false);
container.revalidate();
container.repaint();
container.setVisible(true);
I'd like to put this code somewhere to be available for others, but don't know where exactly to do this. So if you know such place, please point me to it in the comments.