Java计算器(Beta版)_int_64_新浪博客

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

 

@SuppressWarnings("serial")
public class AppletTest extends JApplet{
 private JButton b1 = new JButton("1");
 private JButton b2 = new JButton("2");
 private JButton b3 = new JButton("3");
 private JButton b4 = new JButton("4");
 private JButton b5 = new JButton("5");
 private JButton b6 = new JButton("6");
 private JButton b7 = new JButton("7");
 private JButton b8 = new JButton("8");
 private JButton b9 = new JButton("9");
 private JButton b0 = new JButton("0");
 private JButton b_add = new JButton("+");
 private JButton b_sub = new JButton("-");
 private JButton b_mult = new JButton("×");
 private JButton b_div = new JButton("÷");
 private JButton b_ce = new JButton("CE");
 private JButton b_equal = new JButton("=");
 private JTextField txt = new JTextField(15);
 private double number1,number2,temp,result;
 private boolean add,sub,mult,div;
 
 public void init(){
  number1 = number2 = temp =0.0;
  add = sub = mult = div = false;
  
  JPanel area1 = new JPanel();
  area1.setLayout(new GridLayout(4,3,2,2));
  area1.add(b7);
  area1.add(b8);
  area1.add(b9);
  area1.add(b4);
  area1.add(b5);
  area1.add(b6);
  area1.add(b1);
  area1.add(b2);
  area1.add(b3);
  area1.add(b0);
  area1.add(b_ce);
  area1.add(b_equal);
  
  JPanel area2 = new JPanel();
  area2.setLayout(new GridLayout(4,1,2,2));
  area2.add(b_add);
  area2.add(b_sub);
  area2.add(b_mult);
  area2.add(b_div);
  
  JPanel show = new JPanel();
  show.add(txt);
  Container cp = getContentPane();
  cp.add(BorderLayout.CENTER,area1);
  cp.add(BorderLayout.EAST,area2);
  cp.add(BorderLayout.NORTH,show);
  
  b1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+1;
    txt.setText(""+temp);
   }
  });
  b2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+2;
    txt.setText(""+temp);
   }
  });
  b3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+3;
    txt.setText(""+temp);
   }
  });
  b4.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+4;
    txt.setText(""+temp);
   }
  });
  b5.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+5;
    txt.setText(""+temp);
   }
  });
  b6.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+6;
    txt.setText(""+temp);
   }
  });
  b7.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+7;
    txt.setText(""+temp);
   }
  });
  b8.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+8;
    txt.setText(""+temp);
   }
  });
  b9.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+9;
    txt.setText(""+temp);
   }
  });
  b0.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    temp = temp*10+0;
    txt.setText(""+temp);
   }
  });
  b_add.addActionListener(new ActionListener() {
   public void actionPerformed(ActionEvent e) {
    number1 = temp;
    add = true;
    sub = false;
    mult = false;
    div = false;
    temp = 0;
   }
  });
  b_sub.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    number1 = temp;
    add = false;
    sub = true;
    mult = false;
    div = false;
    temp = 0;
   }
  });
  b_mult.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    number1 = temp;
    add = false;
    sub = false;
    mult = true;
    div = false;
    temp = 0;
   }
  });
  b_div.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    number1 = temp;
    add = false;
    sub = false;
    mult = false;
    div = true;
    temp = 0;
   }
  }); 
  b_equal.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    number2 = temp;
    if(add==true){
     result = number1 + number2;
     txt.setText(""+result);
    }
    if(sub==true){
     result = number1 - number2;
     txt.setText(""+result);
    }
    if(mult==true){
     result = number1 * number2;
     txt.setText(""+result);
    }
    if(div==true){
     if(number2==0){
      txt.setText("Error!");
     }
     else{txt.setText(""+result);
         result = number1 / number2;
         txt.setText(""+result);
     }
    }
    temp = result;
   }
  });
  b_ce.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e) {
    number1 = number2 = temp = 0;
    txt.setText(""+temp);
   }
  });
 }

}

编译平台:

Eclipse

Windows 7 Ultimate

 

Jawa直接运行结果:

 

浏览器运行结果:



缺点:1、没有小数点,无法进行浮点数运算。

      2、只限于四则运算,功能低下。

      3、对运算结果没有保存功能。

      4、界面丑陋。

注:以上缺点是与windows自带的计算器进行对比的结果。当然还有很多的不足,我会不断改进程序的结构以及思想,争取向真正拿得出来的软件靠拢。

已投稿到:
郑重声明:资讯 【Java计算器(Beta版)_int_64_新浪博客】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——