importjava.awt.*;importjava.awt.event.*;publicclassComputerimplementsActionListener{Framef;Panelp;TextFieldtf;Buttonbutton[];inti;doubled1;doubled2;doublesum;Stringopr;bo...
import java.awt.*;
import java.awt.event.*;
public class Computer implements ActionListener{
Frame f;
Panel p;
TextField tf;
Button button[];
int i;
double d1;
double d2;
double sum;
String opr;
boolean flag;
public Computer(){
f=new Frame("computer");
p=new Panel();
p.setLayout(new GridLayout(5,4,3,3));
f.add(p,"Center");
tf=new TextField(20);
tf.setEditable(false);
f.add(tf,"North");
button=new Button[20];
String s[]={"退格","CE","C","/",
"7","8","9","*",
"4","5","6","-",
"1","2","3","+",
"0","+/-",".","="};
for(i=0;i<20;i++){
button[i]=new Button(s[i]);
p.add(button[i]);
button[i].addActionListener(this);
}
f.setSize(300,300);
f.setBackground(Color.blue);
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(1);
}
});
}
public void actionPerformed(ActionEvent e){
String d=e.getActionCommand();
String dd=tf.getText();
if(d=="0"||d == "1"|| d == "2"|| d == "3"|| d== "4"|| d == "5"|| d == "6"
|| d == "7"|| d == "8"|| d == "9"|| d== "."){
if(flag==false)//input not complete
{
dd=dd+d;
tf.setText(dd);
}
else{
tf.setText(d);
flag=false;//为第二次输入数字做准备
}
}
else if(d == "+" || d == "-"|| d == "*"|| d == "/"){
if((!dd.equals(""))||(!dd.equals("0"))){
d1=Double.parseDouble(dd);
opr=d;
flag=true;//first input complete
}
}
else if(d== "="){
d2=Double.parseDouble(dd);
if(opr == "+") sum = d1 + d2;
if(opr == "-") sum = d1 - d2;
if(opr == "*") sum = d1 * d2;
if(opr == "/") sum = d1 / d2;
tf.setText(sum + "");
}
else if (d=="退格"){
dd=dd.substring(0,dd.length()-1);
tf.setText(dd);
}
else if(d=="+/-"){
double t=Double.parseDouble(dd)*(-1);
tf.setText(t+"");
}
else if(d=="CE"){
}
else if(d=="C"){
tf.setText("0.");
}
}
public static void main(String[]args){
Computer computer =new Computer();
}
}
注:CE为清空,C为清零。
请高手们看看,在我的这种设计下,CE和C该怎么实现它们的功能?
展开