JSplitPane:有没有办法显示/隐藏其中一个窗格?
我有一个带有两个组件 A 和 B 的 JSplitPane,但有时我希望能够隐藏 B,以便满足以下任一条件:
- 组件 A 和 B 在 JSplitPane 中可见
- 只有组件 A 在占用的空间中可见通过 JSplitPane
有办法做到这一点吗?
I have a JSplitPane with two components, A and B, but sometimes I want to be able to hide B, so that either of the following are true:
- components A and B are visible in the JSplitPane
- only component A is visible in the space occupied by the JSplitPane
Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
我发现 Hovercraft Full Of Eels 的版本存在问题,并制作了自己的版本。
Hovercraft Full Of Eels 的一个可以工作,但是如果您单击按钮,调整框架大小,然后再次单击该按钮,则会出现图形错误;也像阿莫尔所说,您可能希望在此过程中保留分流器位置。
I found problems with Hovercraft Full Of Eels' version and made my own.
Hovercraft Full Of Eels' one works, nevertheless graphical bug appears if you click a button, resize the frame, then click again the button ; also like amol said, you may want the splitter position to remain during the process.
哎呀,我会尝试一个解决方案......
Heck, I'll throw in an attempt at a solution...
假设 HORIZONTAL_SPLIT 拆分为两个组件(A 位于左侧,B 位于右侧)
以下是如何隐藏 A 并让 B 占据拆分窗格的所有空间
隐藏组件 B 并显示 A -
如果您有垂直拆分,请使用类似的方法和将
x
与y
切换,将width
与height
切换对于完整的解决方案,您必须监听调整大小事件(如果适用)和重新计算分隔线位置(这意味着您将在某处存储当前可见内容的状态)
Assume HORIZONTAL_SPLIT split with two components (A on left and B on right)
Here is how you would hide A and let B take up all the splitpane's space
To hide component B and show A -
If you have a vertical split, use similar approach and switch
x
withy
andwidth
withheight
For a complete solution, you will have to listen to resize events (if applicable) and recalculate the divider location (which means you will have store the state of what is currently visible somewhere)
如果您引用了组件 A 和 B,则可以使用 JSplitPane.remove() 方法
或 JComponent.setVisible(false) 组件A或B的方法。测试代码:
If you have references to the component A and B you could user JSplitPane.remove() method
or JComponent.setVisible(false) method of the component A or B.Test code:
我用它来切换框架底部的日志面板:
这会记住并恢复组件大小。
I use this to toggle a log panel at the bottom of a frame:
This remembers and restores the component size.
尝试ExpandableSplitPane#setOneSideHidden
try
ExpandableSplitPane#setOneSideHidden