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

发布于 2024-10-02 02:58:14 字数 1333 浏览 2 评论 0原文

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

这是 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();
    }

}

这里是类 JPane1:

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

和类 Jpane2,其中按钮 int

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

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

} 所以问题出在 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

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

Here is the code for 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:

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

and class Jpane2 with button on int

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

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

}
so problem is here in ActionListener of button on Jpanel2

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

alt text

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

青朷 2024-10-09 02:58:14

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

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

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

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

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.

情泪▽动烟 2024-10-09 02:58:14

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

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

两个我 2024-10-09 02:58:14

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

JTabbedpane.setSelectedIndex(); 

无论您的 J Panel 名称是什么,请将其替换为上面的 JTabbedpane,例如您想选择第一个选项卡,只需将 0 放在括号中,如果您想选择第二个选项卡,则将 1在括号中,例如:我的 J 选项卡式窗格称为 jtabbedpanel,我想要第一个选项卡,那么该行将如下所示:

jtabbedpanel.setSelectedIndex(0);

希望这有帮助!

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:

jtabbedpanel.setSelectedIndex(0);

hope this helps!!

临风闻羌笛 2024-10-09 02:58:14

就像添加您的操作侦听器必须与您的选项卡位于同一类中一样。

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

君勿笑 2024-10-09 02:58:14

只是!和:

JTabbedPane.setSelectedComponnet(component);

Just! With:

JTabbedPane.setSelectedComponnet(component);
乖不如嘢 2024-10-09 02:58:14

这是固定且完整的代码。
12年后。

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import javax.swing.JPanel;

public class TabbedPane extends JFrame {
    public TabbedPane() {
        JTabbedPane jtp = new JTabbedPane(); 
        JPanel jp1 = new JPanel();//This will create the first tab
        JPanel jp2 = new JPanel();//This will create the second tab
        setTitle("Tabbed Pane");  
        setSize(400,300); 
        setLocationRelativeTo(null);
        
        JLabel label1 = new JLabel();
        label1.setText("This is Tab 1"); 
        jp1.add(label1);
        //jp2.add(new JLabel(new ImageIcon("C:/users/user/downloads/jav/s1/pic1.jpg")));        
        getContentPane().add(jtp);
        
        JButton nextButton = new JButton("Next"); jp1.add(nextButton);
        JButton prevButton = new JButton("Previous"); jp2.add(prevButton);
        //prev
        class ButtonHandlerPrev implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i-1);
            }
        }
        class ButtonHandlerNext implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i+1);
            }
        }
        jtp.addTab("first", jp1);
        jtp.addTab("second", jp2);
        ButtonHandlerPrev prevHandler = new ButtonHandlerPrev();
        ButtonHandlerNext nextHandler = new ButtonHandlerNext();
        prevButton.addActionListener(prevHandler);
        nextButton.addActionListener(nextHandler);
        setVisible(true);
    }
        //example usage
    public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }
}

只需运行 void main(String[] args)

here is fixed and full code.
after 12 years.

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;  
import javax.swing.JPanel;

public class TabbedPane extends JFrame {
    public TabbedPane() {
        JTabbedPane jtp = new JTabbedPane(); 
        JPanel jp1 = new JPanel();//This will create the first tab
        JPanel jp2 = new JPanel();//This will create the second tab
        setTitle("Tabbed Pane");  
        setSize(400,300); 
        setLocationRelativeTo(null);
        
        JLabel label1 = new JLabel();
        label1.setText("This is Tab 1"); 
        jp1.add(label1);
        //jp2.add(new JLabel(new ImageIcon("C:/users/user/downloads/jav/s1/pic1.jpg")));        
        getContentPane().add(jtp);
        
        JButton nextButton = new JButton("Next"); jp1.add(nextButton);
        JButton prevButton = new JButton("Previous"); jp2.add(prevButton);
        //prev
        class ButtonHandlerPrev implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i-1);
            }
        }
        class ButtonHandlerNext implements ActionListener{
            public void actionPerformed(ActionEvent e){
                int i = jtp.getSelectedIndex();
                jtp.setSelectedIndex(i+1);
            }
        }
        jtp.addTab("first", jp1);
        jtp.addTab("second", jp2);
        ButtonHandlerPrev prevHandler = new ButtonHandlerPrev();
        ButtonHandlerNext nextHandler = new ButtonHandlerNext();
        prevButton.addActionListener(prevHandler);
        nextButton.addActionListener(nextHandler);
        setVisible(true);
    }
        //example usage
    public static void main (String []args){
        TabbedPane tab = new TabbedPane();
    }
}

just run void main(String[] args)

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