如何通过单击按钮来切换 jTabbedPane 中的选项卡?
我有两个 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]
}
}
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]
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果使选项卡式窗格可供 ButtonHandler 访问,则可以执行以下操作:
您可以通过使用 getter 方法将 jtp(最好使用更好的名称)设为私有属性,或者将其作为构造函数参数传递给 ButtonHandler 来实现此目的。
If you make the tabbed pane accessible to ButtonHandler you can do this:
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.
您应该使用方法
JTabbedPane.setSelectedIndex(int index)
以及所需选项卡的索引。You should use the method
JTabbedPane.setSelectedIndex(int index)
with the index of the tab you want.它非常简单:使用下面的代码:
无论您的 J Panel 名称是什么,请将其替换为上面的 JTabbedpane,例如您想选择第一个选项卡,只需将 0 放在括号中,如果您想选择第二个选项卡,则将 1在括号中,例如:我的 J 选项卡式窗格称为 jtabbedpanel,我想要第一个选项卡,那么该行将如下所示:
希望这有帮助!
its very simple: use the code below:
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:
hope this helps!!
就像添加您的操作侦听器必须与您的选项卡位于同一类中一样。
Just like to add that your action listener has to be in the same class as your tabs.
只是!和:
Just! With:
这是固定且完整的代码。
12年后。
只需运行 void main(String[] args)
here is fixed and full code.
after 12 years.
just run void main(String[] args)