java 如果只有一个选项卡,有没有办法隐藏 JTabbedPane 的选项卡栏?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/942500/
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
Is there a way to hide the tab bar of JTabbedPane if only one tab exists?
提问by Huxi
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.
我想要一种类似于 Firefox 的行为,其中可用选项卡列表仅在至少存在两个选项卡时才会显示。
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.
- 如果只有一个组件,只需将其添加到周围的面板中
- 如果添加了组件,请从周围面板中删除该组件,改为添加 JTabbedPane,并将前一个组件和新组件都添加到该窗格中。
- 如果删除了一个组件并且窗格中只剩下一个组件,请删除该窗格并添加包含的组件。
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.
理想情况下,解决方案应该在 Java 1.5 和 1.6 中都有效......但我也很高兴只有 1.6 的解决方案。
采纳答案by Michael Myers
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 JComponentso it will feel less hackish.
你的方法对我来说很好。我会像你布置的那样做,并将所有这些逻辑包装在一个自定义中,JComponent这样它就会感觉不那么黑客了。
回答by Brad Mace
You can override the UI method that calculates the height for the tab button area, forcing the height to 0when there's only one tab:
您可以覆盖计算选项卡按钮区域高度的 UI 方法,在0只有一个选项卡时强制设置高度:
tabbed_pane.setUI(new BasicTabbedPaneUI() {
@Override
protected int calculateTabAreaHeight(int tab_placement, int run_count, int max_tab_height) {
if (tabbed_pane.getTabCount() > 1)
return super.calculateTabAreaHeight(tab_placement, run_count, max_tab_height);
else
return 0;
}
});
回答by Eugene Ryzhikov
You may be better off simply using CardLayout.
您最好只使用CardLayout.
回答by Joshua
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.
另一种选择是自定义 JTabbedPane 使用的 L&F 委托(BasicTabbedPaneUI 或 WindowsTabbedPaneUI,具体取决于您关心的平台)。这将允许您在仅显示一个选项卡的情况下自定义选项卡式窗格的行为。
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.
这是另一种做事的方式,但是我会说这是一项艰巨的任务,按照迈克尔所说的去做会让你以更少的努力去你想去的地方。我只是想将此作为答案发布,以防您不知道此选项。
回答by Low Flying Pelican
I think this can be achieved using tab bar and a card layout,
我认为这可以使用标签栏和卡片布局来实现,
- add the tab bar and card layout to a grid bag layout so that they re-size automatically
- the max height of the tab bar should be the height of the tab
- add a listener to tab bar so that when certain tabs are clicked it will switch the card layout to show appropriate content
- hide the tab bar if it has only one tab
- 将标签栏和卡片布局添加到网格包布局中,以便它们自动调整大小
- 标签栏的最大高度应该是标签的高度
- 向标签栏添加一个监听器,这样当点击某些标签时,它会切换卡片布局以显示适当的内容
- 如果只有一个选项卡,则隐藏选项卡栏
and this should do the job.
这应该可以完成工作。
回答by Rafa?
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()
是的,有办法。花了我四个小时在oracle网站上找到:http: //docs.oracle.com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()
Simply use this:
只需使用这个:
//declare
private JTabbedPane editor = new JTabbedPane ();
//construct like this:
editor.setTabLayoutPolicy(JTabbedPane.SCROLL_TAB_LAYOUT);
//just add components and see how it goes.
editor.addTab("", newPanel);
回答by Luchang Li
jTabbedPane1.removeTabAt(0);seems to work after initComponents();
jTabbedPane1.removeTabAt(0);似乎在工作之后initComponents();

