简体   繁体   English

如果只有一个选项卡,有没有办法隐藏JTabbedPane的标签栏?

[英]Is there a way to hide the tab bar of JTabbedPane if only one tab exists?

I want a behavior similar to eg 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. 如果添加了一个组件,则从周围的面板中删除该组件,添加一个JTabbedPane,并将前一个组件和新组件添加到该窗格中。
  • 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. 理想情况下,解决方案应该适用于Java 1.5和1.6 ......但我也很高兴只有1.6的解决方案。

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: 您可以覆盖计算选项卡按钮区域高度的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;  
    }  
});  

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. 我会像你所说的那样做,并将所有逻辑包装在一个自定义的JComponent这样它就会感觉不那么hackish了。

简单地使用CardLayout可能会更好。

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);

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. 另一种选择是自定义JTabbedPane使用的L&F委托(BasicTabbedPaneUI或WindowsTabbedPaneUI,具体取决于您关注的平台)。 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, 我认为这可以使用标签栏和卡片布局来实现,

  • 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. 这应该做的工作。

jTabbedPane1.removeTabAt(0); seems to work after initComponents(); 似乎在initComponents();之后工作initComponents();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM