怎么给两个数加个判断??(在星号处)
小问题,麻烦各位了
******************************************************
//计算器
import java.awt.*;
import java.awt.event.*;
class J{ Frame f;
TextField tf1,tf2,tf3;
Button button1,button2;
Panel p,p2;
Checkbox cd1,cd2,cd3;
CheckboxGroup cg;
public J(){
f=new Frame("计算器");
f.setLayout(new FlowLayout());
tf1=new TextField("",6);
tf2=new TextField("",6);
tf3=new TextField("",20);
button1 = new Button("结果");
button2 = new Button("清除");
p.setLayout(new GridLayout(4,1));
cg=new CheckboxGroup();
Checkbox cd1=new Checkbox("+",cg,true);
Checkbox cd2=new Checkbox("-",cg,false);
Checkbox cd3=new Checkbox("*",cg,false);
Checkbox cd4=new Checkbox("/",cg,false);
p2=new Panel(new GridLayout(2,1));
//添加到面板
f.add(tf1);
p.add(cd1); p.add(cd2); p.add(cd3);p.add(cd4);
f.add(p);
f.add(tf2);
f.add(p2);
p2.add(button1);
p2.add(button2);
f.add(tf3);
button1.addActionListener(new MyListener1());
button2.addActionListener(new MyListener3());
f.addWindowListener(new MyListener2());
f.setVisible(true);
f.setSize(300,60);
// f.setBackground(Color.RED); //背景颜色
f.pack();
}
class MyListener1 implements ActionListener{
public void actionPerformed(ActionEvent e){
Double num1 = new Double(tf1.getText());
Double num2 = new Double(tf2.getText());
double n1 = num1.doubleValue();
double n2 = num2.doubleValue();
double n3 = 0;
//**************************************************************
// while(num1==null){
// tf3.setText("请正确输入");}
// //怎么给两个数加个判断?
//**************************************************************
if(cg.getSelectedCheckbox().getLabel() == "+"){n3 = n1+n2;}
if(cg.getSelectedCheckbox().getLabel() == "-"){n3 = n1-n2;}
if(cg.getSelectedCheckbox().getLabel() == "*"){n3 = n1*n2;}
if(cg.getSelectedCheckbox().getLabel() == "/"){n3 = n1/n2;}
Double num3 = new Double(n3);
tf3.setText(n1+cg.getSelectedCheckbox().getLabel()+n2+" = "+num3.toString());
tf3.select(0,0);}}
class MyListener2 extends WindowAdapter{
public void windowClosing(WindowEvent e){
System.exit(1);
}
}
class MyListener3 implements ActionListener{
public void actionPerformed(ActionEvent e){
tf3.setText("");
}}
}
public class Jsq{
public static void main(String args[]){
J jsq=new J();}
}//主方法