初学者+菜鸟,小小作品,只实现了最简单的加减乘除功能。。。望请各位牛人指导!代码如下:
试问:下面红色字体代码部分,能不能精简,或是其他改进一下啊?太繁琐了那样写
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class frame extends JFrame
implements ActionListener{
double first = 0,second=0,result=0;
int computer;
JTextField text,t;
JButton b1,b2,b3,b4,b5,b6,b7,b8,b9,b0;
JButton add,sub,cheng,chu;
JButton point,equal;
JButton clean;
frame(String stri)
{
super(stri);
text=new JTextField("",15);
b1=new JButton("1");b2=new JButton("2");b3=new JButton("3");
b4=new JButton("4");b5=new JButton("5");b6=new JButton("6");
b7=new JButton("7");b8=new JButton("8");b9=new JButton("9");
b0=new JButton("0");
add=new JButton("+");sub=new JButton("-");
cheng=new JButton("*");chu=new JButton("/");
point=new JButton(".");equal=new JButton("=");
clean=new JButton("C");
setLayout(new FlowLayout());
setBounds(200,200,200,220);
add(text);
add(b1);add(b2);add(b3);add(b4);add(b5);add(b6);add(b7);add(b8);
add(b9);add(b0);
add(point);add(equal);
add(add);add(sub);add(cheng);add(chu);
add(clean);
b1.addActionListener(this);b2.addActionListener(this);
b3.addActionListener(this);b4.addActionListener(this);
b5.addActionListener(this);b6.addActionListener(this);
b7.addActionListener(this);b8.addActionListener(this);
b9.addActionListener(this);b0.addActionListener(this);
add.addActionListener(this);sub.addActionListener(this);
cheng.addActionListener(this);chu.addActionListener(this);
point.addActionListener(this);equal.addActionListener(this);
clean.addActionListener(this);
setVisible(true);
setResizable(false);
validate();
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
String s;
if(e.getSource()==b0)text.setText(text.getText()+b0.getLabel().toString());
if(e.getSource()==b1)text.setText(text.getText()+b1.getLabel().toString());
if(e.getSource()==b2)text.setText(text.getText()+b2.getLabel().toString());
if(e.getSource()==b3)text.setText(text.getText()+b3.getLabel().toString());
if(e.getSource()==b4)text.setText(text.getText()+b4.getLabel().toString());
if(e.getSource()==b5)text.setText(text.getText()+b5.getLabel().toString());
if(e.getSource()==b6)text.setText(text.getText()+b6.getLabel().toString());
if(e.getSource()==b7)text.setText(text.getText()+b7.getLabel().toString());
if(e.getSource()==b8)text.setText(text.getText()+b8.getLabel().toString());
if(e.getSource()==b9)text.setText(text.getText()+b9.getLabel().toString());
if(e.getSource()==point)text.setText(text.getText()+point.getLabel().toString());
if(e.getSource()==add||e.getSource()==sub||e.getSource()==cheng||e.getSource()==chu){
//first=Integer.parseInt(text.getText());
first=Double.parseDouble(text.getText());
text.setText("");
if(e.getSource()==add)computer=1;
if(e.getSource()==sub)computer=2;
if(e.getSource()==cheng)computer=3;
if(e.getSource()==chu)computer=4;
}
if(e.getSource()==equal){
second=Double.parseDouble(text.getText());
text.setText("");
switch(computer){
case 1:result=first+second;break;
case 2:result=first-second;break;
case 3:result=first*second;break;
case 4:result=first/second;break;
default:text.setText("Please input the right number");
}
s=String.valueOf(result);
text.setText(s);
}
if(e.getSource()==clean){
text.setText("");
first = 0;second=0;result=0;
}
}
}