如果只有一个选项卡存在,有没有办法隐藏 JTabbedPane 的选项卡栏?
我想要类似于 Firefox 的行为,其中可用选项卡列表仅在至少存在两个选项卡时才显示。
我还没有找到类似的东西。
我最好的想法是手动更改布局:
- 如果有一个组件,只需将其添加到周围的面板
- (如果添加了组件),从周围的面板中删除该组件,添加一个 JTabbedPane 并添加前一个和新的该窗格的组件。
- 如果删除了某个组件并且窗格中只剩下一个组件,请删除该窗格并添加所包含的组件。
虽然这可能会起作用,但感觉像是一种黑客或解决方法......
还有更好的主意吗?
理想情况下,解决方案应该同时适用于 Java 1.5 和 1.6...但我也对仅适用于 1.6 的解决方案感到高兴。
I want a behavior similar to e.g. Firefox where the list of available tabs does only show up if at least two tabs exist.
I wasn't able to find anything like that, yet.
The best idea I had was changing the layout manually:
- in case of one component, just add that to the surrounding panel
- if a component is added, remove the component from the surrounding panel, add a JTabbedPane instead and add both the previous and the new component to that pane.
- if a component is removed and only one component is left in the pane, remove the pane and add the contained component instead.
While this would probably work it feels like a hack or workaround...
Any better idea?
A solution should ideally work in both Java 1.5 and 1.6... but I'd be happy about a 1.6-only solution, too.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您可能最好简单地使用
CardLayout< /代码>
。
You may be better off simply using
CardLayout
.我相信你必须手动完成。 显然它以前已经做过,但只是作为系统的一小部分,似乎不可用。
你的方法对我来说看起来不错。 我会像您布置的那样进行操作,并将所有逻辑包装在自定义
JComponent
中,这样就不会感觉那么hackish。I believe you'll have to do it manually. Apparently it has been done before, but only as a small bit of a system which seems to not be available.
Your approach looks good to me. I would do it just like you laid it out, and wrap all that logic in a custom
JComponent
so it will feel less hackish.另一种选择是自定义 JTabbedPane 使用的 L&F 委托(BasicTabbedPaneUI 或 WindowsTabbedPaneUI,具体取决于您关心的平台)。 这将允许您在仅显示单个选项卡的情况下自定义选项卡式窗格的行为。
这是另一种做事的方式,但我想说这是一项艰巨的任务,按照迈克尔所说的去做会让你以更少的努力到达你想去的地方。 我只是想将此作为答案发布,以防您不知道此选项。
Another option would be to customize the L&F delegate (either BasicTabbedPaneUI or WindowsTabbedPaneUI depending on the platforms you care about) used by the JTabbedPane. This would allow you to customize the behavior of the tabbed pane in the case where only a single tab was being shown.
This is another way of doing things however I would say it's quite an undertaking and doing what Michael said will get you where you want to go with a lot less effort. I just wanted to post this as an answer in case you weren't aware of this option.
我认为这可以使用选项卡栏和卡片布局来实现,
自动调整
将切换卡片布局以显示适当的内容,
,这应该可以完成这项工作。
I think this can be achieved using tab bar and a card layout,
re-size automatically
will switch the card layout to show appropriate content
and this should do the job.
是的,有办法。 我花了四个小时在oracle网站上找到:
http://docs.oracle .com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()
只需使用这个:
Yes, there is a way. Took me four hours to find at the oracle website:
http://docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()
Simply use this:
jTabbedPane1.removeTabAt(0);
似乎在initComponents();
之后工作jTabbedPane1.removeTabAt(0);
seems to work afterinitComponents();
您可以重写计算选项卡按钮区域高度的 UI 方法,当只有一个选项卡时强制高度为
0
:You can override the UI method that calculates the height for the tab button area, forcing the height to
0
when there's only one tab: