本人的java小小作品--计算器

初学者+菜鸟,小小作品,只实现了最简单的加减乘除功能。。。望请各位牛人指导!代码如下:

试问:下面红色字体代码部分,能不能精简,或是其他改进一下啊?太繁琐了那样写

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;
        }
 }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值