切换JTabbedPane后响应,使一个boolean型的值改变?
比如现在我有一个JTabbedPane,里面有两个选项卡,有一个boolean类型的数据,怎么在切换选项卡的时候,改变这个boolean类型数据的值?...
比如现在我有一个JTabbedPane,里面有两个选项卡,有一个boolean类型的数据,怎么在切换选项卡的时候,改变这个boolean类型数据的值?
展开
2个回答
展开全部
首先,定义一个JTabbedPane jt = new JTabbedPane();然后向jt中加入两三个选项卡,并且定义一个监听器jt.addChangeListener(this);最后,在public void stateChanged(ChangeEvent ce) 函数中,用if(jt.getSelectedIndex()==0)或者if(jt.getSelectedIndex()==1)来判断选中了哪一个选项卡,然后相应的改变boolean类型数据的值。
我给你个例子,只是这个例子有三个选项卡,不过你可以很快知道两个选项卡应该怎么做。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTabbedPaneDemo{
public static void main(String args[]){
new MyTabbedPane();
}
}
class MyTabbedPane extends JFrame implements ActionListener,ChangeListener{
JTabbedPane jt;
JTextField t1,t2,t3,t4,t;
JLabel l1,l2,l3,l4;
JComboBox cb;
JButton b1,b2,b3;
boolean flag=false;
JPanel jp1 = new JPanel(new FlowLayout());
JPanel jp2 = new JPanel(new FlowLayout());
JPanel jp3 = new JPanel(new FlowLayout());
double x1,x2,x3,y1,y2;
long z1,z2=1;
MyTabbedPane(){
super("使用标签面板容器");
jt = new JTabbedPane();
b1 = new JButton("计算");
t1 = new JTextField(5);
t2 = new JTextField(5);
cb = new JComboBox();
t = new JTextField(5);
cb.addItem("+");
cb.addItem("-");
cb.addItem("*");
cb.addItem("/");
jp1.add(t1);
jp1.add(cb);
jp1.add(t2);
jp1.add(b1);
jp1.add(t);
l1 = new JLabel("请输入半径");
l2 = new JLabel("计算结果");
b2 = new JButton("计算圆面积");
t3 = new JTextField(5);
jp2.add(l1);
jp2.add(t3);
jp2.add(b2);
jp2.add(l2);
l3 = new JLabel("请输入一个数");
l4 = new JLabel("计算结果");
b3 = new JButton("计算阶乘");
t4 = new JTextField(5);
jp3.add(l3);
jp3.add(t4);
jp3.add(b3);
jp3.add(l4);
jt.addTab("四则运算",jp1);
jt.addTab("计算圆面积",jp2);
jt.addTab("计算阶乘",jp3);
jt.addChangeListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
getContentPane().add(jt,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
x1=Double.valueOf(t1.getText());
x2=Double.valueOf(t2.getText());
switch(cb.getSelectedIndex())
{case 0:
{x3=x1+x2;
t.setText(String.valueOf(x3));
break;}
case 1:
{x3=x1-x2;
t.setText(String.valueOf(x3));
break;}
case 2:
{x3=x1*x2;
t.setText(String.valueOf(x3));
break;}
case 3:
{x3=x1/x2;
t.setText(String.valueOf(x3));
break;}
}
}
if(e.getSource()==b2){
y1=Double.valueOf(t3.getText());
y2=y1*y1*3.14;
l2.setText(String.valueOf(y2));
}
if(e.getSource()==b3){
z2=1;
z1=Long.valueOf(t4.getText());
for(int i=1;i<=z1;i++){
z2=z2*i;
}
l4.setText(String.valueOf(z2));
}
}
public void stateChanged(ChangeEvent ce) {
if(ce.getSource()==jt){
if(jt.getSelectedIndex()==0){
flag=true;
System.out.println(flag);
}else if(jt.getSelectedIndex()==1){
flag=false;
System.out.println(flag);
}else if(jt.getSelectedIndex()==2){
flag=true;
System.out.println(flag);
}
}
}
}
我给你个例子,只是这个例子有三个选项卡,不过你可以很快知道两个选项卡应该怎么做。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class JTabbedPaneDemo{
public static void main(String args[]){
new MyTabbedPane();
}
}
class MyTabbedPane extends JFrame implements ActionListener,ChangeListener{
JTabbedPane jt;
JTextField t1,t2,t3,t4,t;
JLabel l1,l2,l3,l4;
JComboBox cb;
JButton b1,b2,b3;
boolean flag=false;
JPanel jp1 = new JPanel(new FlowLayout());
JPanel jp2 = new JPanel(new FlowLayout());
JPanel jp3 = new JPanel(new FlowLayout());
double x1,x2,x3,y1,y2;
long z1,z2=1;
MyTabbedPane(){
super("使用标签面板容器");
jt = new JTabbedPane();
b1 = new JButton("计算");
t1 = new JTextField(5);
t2 = new JTextField(5);
cb = new JComboBox();
t = new JTextField(5);
cb.addItem("+");
cb.addItem("-");
cb.addItem("*");
cb.addItem("/");
jp1.add(t1);
jp1.add(cb);
jp1.add(t2);
jp1.add(b1);
jp1.add(t);
l1 = new JLabel("请输入半径");
l2 = new JLabel("计算结果");
b2 = new JButton("计算圆面积");
t3 = new JTextField(5);
jp2.add(l1);
jp2.add(t3);
jp2.add(b2);
jp2.add(l2);
l3 = new JLabel("请输入一个数");
l4 = new JLabel("计算结果");
b3 = new JButton("计算阶乘");
t4 = new JTextField(5);
jp3.add(l3);
jp3.add(t4);
jp3.add(b3);
jp3.add(l4);
jt.addTab("四则运算",jp1);
jt.addTab("计算圆面积",jp2);
jt.addTab("计算阶乘",jp3);
jt.addChangeListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
getContentPane().add(jt,BorderLayout.CENTER);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500,200);
setVisible(true);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==b1){
x1=Double.valueOf(t1.getText());
x2=Double.valueOf(t2.getText());
switch(cb.getSelectedIndex())
{case 0:
{x3=x1+x2;
t.setText(String.valueOf(x3));
break;}
case 1:
{x3=x1-x2;
t.setText(String.valueOf(x3));
break;}
case 2:
{x3=x1*x2;
t.setText(String.valueOf(x3));
break;}
case 3:
{x3=x1/x2;
t.setText(String.valueOf(x3));
break;}
}
}
if(e.getSource()==b2){
y1=Double.valueOf(t3.getText());
y2=y1*y1*3.14;
l2.setText(String.valueOf(y2));
}
if(e.getSource()==b3){
z2=1;
z1=Long.valueOf(t4.getText());
for(int i=1;i<=z1;i++){
z2=z2*i;
}
l4.setText(String.valueOf(z2));
}
}
public void stateChanged(ChangeEvent ce) {
if(ce.getSource()==jt){
if(jt.getSelectedIndex()==0){
flag=true;
System.out.println(flag);
}else if(jt.getSelectedIndex()==1){
flag=false;
System.out.println(flag);
}else if(jt.getSelectedIndex()==2){
flag=true;
System.out.println(flag);
}
}
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询