gpt4 book ai didi

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

转载 作者:IT老高 更新时间:2023-10-28 20:56:57 25 4
gpt4 key购买 nike

我有两个 JTabbedPane,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 类

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

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

最佳答案

如果您让 ButtonHandler 可以访问选项卡式 Pane ,您可以这样做:

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

您可以通过使用 getter 方法将 jtp(最好使用更好的名称)作为私有(private)属性来做到这一点,或者可以将其作为构造函数参数传递给 ButtonHandler。

关于java - 如何通过单击按钮切换 jTabbedPane 中的选项卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4157293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com