动态调整布局管理器区域的大小 [英] Dynamic resize of Layout Manager areas

查看:86
本文介绍了动态调整布局管理器区域的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以在哪个Swing布局管理器中以编程方式更改布局区域?以及如何以最低的成本做到这一点?

In which Swing layout manager it is possible to change layout areas programmatically? And how to do this with lowest cost?

我必须创建具有类似于JSplitPane的功能的组件,但必须从头开始创建三个面板.其中之一是单击分隔器上的oneTouchExpandable按钮后展开/折叠面板之一.但是问题是我不知道如何实施这种崩溃动作.我尝试仅将面板宽度设置为0,但是包含该面板的布局区域在组件之后不会缩小.我尝试在所有布局管理器中执行此操作,但是效果是相同的.

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 to all for the answers. Finally I ended up with combining solutions from several answers.

我的最终解决方案是: 我使用BorderLayout,设置West,Central和East面板,然后通过将PreferredSize设置为West和East面板来操纵它们的大小.渲染方案如下:在布局组件时,BorderLayout为East和West面板提供了PreferredSize以及剩余的空间给Center面板.因此,通过一些简单的计算,我可以轻松地控制三个面板中每个面板的大小.

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.

我还在West和East面板上添加了分隔线(最初只是固定大小的JPanel组件)(在计算时也会考虑它们的大小).对于动态调整大小,我将处理此分隔线上的拖动事件并重新计算面板大小.

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.

使用以下代码段进行刷新: container.setVisible(false); container.revalidate(); container.repaint(); container.setVisible(true);

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.

这篇关于动态调整布局管理器区域的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆