Java 如何通过单击按钮切换 jTabbedPane 中的选项卡?

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

How to switch tabs in jTabbedPane by clicking a Button?

javaswingjtabbedpane

提问by tiendv

I have two JTabbedPanes, JTabbedPane1 & 2 How can I press button in JTabbedPane2 to show JTabbedPane1 ?

我有两个 JTabbedPane,JTabbedPane1 和 2 我如何按下 JTabbedPane2 中的按钮来显示 JTabbedPane1?

Here is the code for JTabbedPane:

这是 JTabbedPane 的代码:

public class TabbedPane extends JFrame {

    public TabbedPane() {


        setTitle("Tabbed Pane");  
        setSize(300,300); 

        JTabbedPane jtp = new JTabbedPane();

       getContentPane().add(jtp);

       JPanel1 jp1 = new JPanel1();//This will create the first tab

       JPanel jp2 = new JPanel2();//This will create the second tab

       //add panel .........

    //example usage
     public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }

}

here is class JPane1:

这是 JPane1 类:

...    JLabel label1 = new JLabel();
       label1.setText("This is Tab 1");
       jp1.add(label1);

and class Jpane2 with button on int

和类 Jpane2 与 int 上的按钮

JButton test = new JButton("Press"); jp2.add(test);

JButton test = new JButton("按下"); jp2.add(测试);

ButtonHandler phandler = new ButtonHandler();
test.addActionListener(phandler);
setVisible(true); 

} so problem is here in ActionListener of button on Jpanel2

所以问题出在 Jpanel2 上按钮的 ActionListener 中

class ButtonHandler implements ActionListener{
       public void actionPerformed(ActionEvent e){
              // what i do now ? to call  jpanel 1 show ![alt text][1]
       }
}

alt text

替代文字

采纳答案by Ventral

If you make the tabbed pane accessible to ButtonHandler you can do this:

如果您使 ButtonHandler 可以访问选项卡式窗格,则可以执行以下操作:

class ButtonHandler implements ActionListener{
       public void actionPerformed(ActionEvent e){
              jtp.setSelectedIndex(0);
       }
}

You can do this by making jtp (ideally with a better name) a private attribute with a getter method or it can be passed in as a constructor argument to ButtonHandler.

您可以通过使用 getter 方法使 jtp(最好使用更好的名称)成为私有属性来实现此目的,也可以将其作为构造函数参数传入 ButtonHandler。

回答by Guillaume

You should use the method JTabbedPane.setSelectedIndex(int index)with the index of the tab you want.

您应该使用JTabbedPane.setSelectedIndex(int index)带有所需选项卡索引的方法。

回答by JohnnyQ

Just like to add that your action listener has to be in the same class as your tabs.

就像添加您的动作侦听器必须与您的选项卡在同一个类中一样。

回答by sidd

its very simple: use the code below:

它非常简单:使用下面的代码:

JTabbedpane.setSelectedIndex(); 

what ever the name is of you J Panel replace it with the above JTabbedpane and for example you want to select the first tabs simply put 0 in bracket and if you want to select second tab then put 1 in bracket eg: my J tabbed pane is called jtabbedpanel and I want the first tab then the line will look as:

你的名字是什么 J Panel 用上面的 JTabbedpane 替换它,例如,你想选择第一个选项卡,只需将 0 放在括号中,如果你想选择第二个选项卡,然后将 1 放在括号中,例如:我的 J 选项卡窗格是称为 jtabbedpanel 并且我想要第一个选项卡,然后该行将如下所示:

jtabbedpanel.setSelectedIndex(0);

hope this helps!!

希望这可以帮助!!

回答by LuisArzate95

Just! With:

只是!和:

JTabbedPane.setSelectedComponnet(component);