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

发布于 2024-07-23 05:35:07 字数 385 浏览 6 评论 0原文

我想要类似于 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

绮烟 2024-07-30 05:35:08

您可能最好简单地使用 CardLayout< /代码>

You may be better off simply using CardLayout.

梦开始←不甜 2024-07-30 05:35:08

我相信你必须手动完成。 显然它以前已经做过,但只是作为系统的一小部分,似乎不可用。

你的方法对我来说看起来不错。 我会像您布置的那样进行操作,并将所有逻辑包装在自定义 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.

虫児飞 2024-07-30 05:35:08

另一种选择是自定义 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.

吃→可爱长大的 2024-07-30 05:35:08

我认为这可以使用选项卡栏和卡片布局来实现,

  • 将选项卡栏和卡片布局添加到网格包布局中,以便它们
    自动调整
  • 大小选项卡栏的最大高度应该是选项卡的高度
  • 向选项卡栏添加侦听器,以便在单击某些选项卡时它
    将切换卡片布局以显示适当的内容,
  • 如果只有一个选项卡,则隐藏选项卡栏

,这应该可以完成这项工作。

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.

寄与心 2024-07-30 05:35:08

是的,有办法。 我花了四个小时在oracle网站上找到:
http://docs.oracle .com/javase/7/docs/api/javax/swing/JTabbedPane.html#setTabLayoutPolicy()

只需使用这个:

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

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:

//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);
梅窗月明清似水 2024-07-30 05:35:08

jTabbedPane1.removeTabAt(0); 似乎在 initComponents(); 之后工作

jTabbedPane1.removeTabAt(0); seems to work after initComponents();

变身佩奇 2024-07-30 05:35:07

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

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:

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;  
    }  
});  
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文