java 如何在 JTabbedPane 中隐藏选项卡式面板?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13116020/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 11:33:11  来源:igfitidea点击:

How to hide tabbed panel in JTabbedPane?

javaswing

提问by Stephane Grenier

I have:

我有:

JTabbedPane jtabbedPane = new JTabbedPane();
jTabbedPane.addTab("Tab 1", panel1);
jTabbedPane.addTab("Tab 2", panel2);
jTabbedPane.addTab("Tab 3", panel3);

What I want to do is hide Tab 2 when a condition occurs (say the user isn't permitted to access that tabbed panel.

我想要做的是在出现条件时隐藏选项卡 2(例如不允许用户访问该选项卡式面板。

Yes I know you can do:

是的,我知道你可以这样做:

jtabbedPane.setEnabled(1, false); // disable Tab 2

which will gray it out, but I want to completely hide it so that the user doesn't even know it's even a possibility in the software. They shouldn't be even aware that it exists.

这会使它变灰,但我想完全隐藏它,以便用户甚至不知道它在软件中的可能性。他们甚至不应该意识到它的存在。

I do NOT want to do

我不想做

jtabbedPane.remove(1); // remove Tab 2

because I then have to remove/add on a regular basis.

因为我必须定期删除/添加。

采纳答案by Stephane Grenier

The only way is to remove it when you don't want to see it, and re-add it later when you do want it visible.

唯一的方法是在您不想看到它时将其删除,并在您希望它可见时重新添加它。

回答by Lucas Motta

This work in my project.

这在我的项目中工作。

this.TabbedPane.setEnabledAt(1, false);

回答by vels4j

I think this can be done only by custom component.

我认为这只能通过自定义组件来完成。

Here is an api for HideableTabbedPanetry that

这是HideableTabbedPane的 api试试看

回答by Tarun Gupta

Solution 1:-why don't you just start x at the value of 1, so it skips 0, instead of starting at 0 and checking for x>1...

解决方案 1:-你为什么不从 1 的值开始 x,所以它跳过 0,而不是从 0 开始并检查 x>1 ...

Solution 2:-[http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapi][1]

解决方案 2:-[http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapi][1]

[1]: http://docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapiuse that link.

[1]:http: //docs.oracle.com/javase/tutorial/uiswing/components/tabbedpane.html#tabapi使用该链接。

Solution 3:- You can do something like this, which just doesn't paint the tabArea

解决方案 3:- 你可以做这样的事情,只是不绘制 tabArea

import javax.swing.*; 
import java.awt.*; 
import java.awt.event.*; 
class Testing { 
public void buildGUI() { 
JTabbedPane tp = new JTabbedPane(); 
tp.addTab("A",getPanel("A")); 
tp.addTab("B",getPanel("B")); 
tp.addTab("C",getPanel("C")); 
tp.setUI(new javax.swing.plaf.metal.MetalTabbedPaneUI(){ 
protected void paintTabArea(Graphics g,int tabPlacement,int selectedIndex){} }); 
JFrame f = new JFrame(); 
f.getContentPane().add(tp); 
f.pack(); 
f.setLocationRelativeTo(null); 
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
f.setVisible(true);
 } 
public JPanel getPanel(String tabText)
 { 
JPanel p = ...