⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 自己用JAVA制作的计算器程序,对初学者是个不错的例子
💻 JAVA
字号:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.border.*;
public class Counter{
 Process process=new Process();
 JTextField t1=new JTextField();
 public Counter(){
  JFrame f=new JFrame();
 
  t1.setEditable(false);
  t1.setBackground(Color.white);
  JButton b1=new JButton(" 1 ");b1.setForeground(Color.blue);
  JButton b2=new JButton(" 2 ");b2.setForeground(Color.blue);
  JButton b3=new JButton(" 3 ");b3.setForeground(Color.blue);
  JButton b4=new JButton(" + ");b4.setForeground(Color.red);
  JButton b5=new JButton(" C ");b5.setForeground(Color.red);
  JButton b6=new JButton(" 4 ");b6.setForeground(Color.blue);
  JButton b7=new JButton(" 5 ");b7.setForeground(Color.blue);
  JButton b8=new JButton(" 6 ");b8.setForeground(Color.blue);
  JButton b9=new JButton(" - ");b9.setForeground(Color.red);
  JButton b10=new JButton(" B ");b10.setForeground(Color.red);
  JButton b11=new JButton(" 7 ");b11.setForeground(Color.blue);
  JButton b12=new JButton(" 8 ");b12.setForeground(Color.blue);
  JButton b13=new JButton(" 9 ");b13.setForeground(Color.blue);
  JButton b14=new JButton(" * ");b14.setForeground(Color.red);
  JButton b15=new JButton("1/x");b15.setForeground(Color.red);
  JButton b16=new JButton(" 0 ");b16.setForeground(Color.blue);
  JButton b17=new JButton("+/-");b17.setForeground(Color.red);
  JButton b18=new JButton(" . ");b18.setForeground(Color.red);
  JButton b19=new JButton(" / ");b19.setForeground(Color.red);
  JButton b20=new JButton(" = ");b20.setForeground(Color.red);
  
  Container c=f.getContentPane();
  c.setLayout(new BorderLayout());
  
  JPanel p=new JPanel(new GridLayout(4,1));
  JPanel p1=new JPanel(new FlowLayout());
  JPanel p2=new JPanel(new FlowLayout());
  JPanel p3=new JPanel(new FlowLayout());
  JPanel p4=new JPanel(new FlowLayout());
  
  c.add(t1,BorderLayout.NORTH);
  p1.add(b1);p1.add(b2);p1.add(b3);p1.add(b4);p1.add(b5);
  p2.add(b6);p2.add(b7);p2.add(b8);p2.add(b9);p2.add(b10);
  p3.add(b11);p3.add(b12);p3.add(b13);p3.add(b14);p3.add(b15);
  p4.add(b16);p4.add(b17);p4.add(b18);p4.add(b19);p4.add(b20);
  p.add(p1);
  p.add(p2);
  p.add(p3);
  p.add(p4);
  c.add(p,BorderLayout.CENTER);
  
  f.setTitle("计算器");
  f.setBounds(300,160,284,210);
  f.setVisible(true); 
  f.setResizable(false);
  
  //b1事件
  b1.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("1");
    t1.setText(process.out());
   } 
  });
  //b2事件
  b2.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("2");
    t1.setText(process.out());
   }
  });
  //b3事件
  b3.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("3");
    t1.setText(process.out());
   }
  });
  //b6事件
  b6.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("4");
    t1.setText(process.out());
   }
  });
  //b7事件
  b7.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("5");
    t1.setText(process.out());
   }
  });
  //b8事件
  b8.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("6");
    t1.setText(process.out());
   }
  });
  //b11事件
  b11.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("7");
    t1.setText(process.out());
   }
  });
  //b12事件
  b12.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("8");
    t1.setText(process.out());
   }
  });
  //b13事件
  b13.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("9");
    t1.setText(process.out());
   }
  });
  //b16事件
  b16.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input("0");
    t1.setText(process.out());
   }
  });
  //b18事件
  b18.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.input(".");
    t1.setText(process.out());
   }
  });
  //b5事件
  b5.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.clear();
    t1.setText("0");
   }
  });
  //b10事件
  b10.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.backSpace();
    t1.setText(process.out());
   }
  });
  //加号事件
  b4.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     process.sign(1);
   }
  });
  //减号事件
  b9.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     process.sign(2);
   }
  });
  //乘号事件
  b14.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
    process.sign(3);
   }
  });
  //除号事件
  b19.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     process.sign(4);
   }
  });
  //+/-号事件
  b17.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     process.setSign();
        t1.setText(process.out());
   }
  });
  //1/x号事件
  b15.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     process.sign(5);
   }
  });
  //等号事件
  b20.addActionListener(new ActionListener(){
   public void actionPerformed(ActionEvent e){
     t1.setText(process.sum());
   }
  }); 
 }
 
 public static void main(String args[]){
  Counter cou=new Counter();
 }
}

//Process类

class Process {
  double temp1, temp2;
  String tfsum1, tfsum2;
  int mark, mark1;
//构造函数

Process() { 
    clear();
  }

  void clear() {
    temp1 = temp2 = 0;
    mark = mark1 = 0;
    tfsum1 = tfsum2 = "0";
  }

//接收数据  

void input(String s) { 
    if (mark1 == -1) {
      clear();
      if (!s.equalsIgnoreCase(".")) {
        tfsum1 = s;
      }
      else {
        tfsum1 += s;
      }
    }
    else if (mark != 0) {
      if (tfsum2.equalsIgnoreCase("0") && !s.equalsIgnoreCase(".")) {
        tfsum2 = s;
      }
      else if (!s.equalsIgnoreCase(".") ||
               (tfsum2.indexOf(".") < 0 && s.equalsIgnoreCase("."))) {
        tfsum2 += s;
      }
      mark1 = 2;
    }
    else {
      if (tfsum1.equalsIgnoreCase("0") && !s.equalsIgnoreCase(".")) {
        tfsum1 = s;
      }
      else if (!s.equalsIgnoreCase(".") ||
               (tfsum1.indexOf(".") < 0 && s.equalsIgnoreCase("."))) {
        tfsum1 += s;
      }
      mark1 = 1;
    }
    temp1 = Double.parseDouble(tfsum1);
    temp2 = Double.parseDouble(tfsum2);
  }
//////////////符号//////////////
  void sign(int sign) { 
    if (mark1 != 0) {
      mark = sign;
    }
  }
//输出计算结果
  String sum() {
    mark1 = -1;
    switch (mark) {
      default:
        break;
      case 1:
        temp1 += temp2;
        break;
      case 2:
        temp1 -= temp2;
        break;
      case 3:
        temp1 *= temp2;
        break;
      case 4:
        if (temp2 != 0) {
          temp1 /= temp2;
        }
        else {
          return "除数不能为零";
        }
        break;
       case 5:
        if(temp1!=0)
        temp1=1/temp1;
        else return "零不能取倒数";
        break;
    }
    tfsum1 = Double.toString(temp1);
    tfsum2 = Double.toString(temp2);
    if (Double.isInfinite(temp1)) {
      return "CE";
    }
    else {
      return String.valueOf(temp1);
    }
  }

//输出当前操作数的字符串
  String out() {
    if (mark != 0 && mark1 >= 0) {
      return tfsum2;
    }
    else {
      return tfsum1;
    }
  }

  //实现退格的功能

  void backSpace() {
   if (mark1 == 1) {
      if (tfsum1.length() <= 1) {
        tfsum1 = "0";
      }
      else {
        tfsum1 = tfsum1.substring(0, tfsum1.length() - 1);
      }
      temp1 = Double.parseDouble(tfsum1);
    }
    if (mark1 == 2) {
      if (tfsum2.length() <= 1) {
        tfsum2 = "0";
      }
      else {
        tfsum2 = tfsum2.substring(0, tfsum2.length() - 1);
      }
      temp2 = Double.parseDouble(tfsum2);
    }
  }

 //取反

  void setSign() {
    switch (mark1) {
      case -1:
      case 1:
        if (tfsum1.startsWith("-")) {
          tfsum1 = tfsum1.substring(1, tfsum1.length());
        }
        else {
          tfsum1 = "-" + tfsum1;
        }
        break;
      case 2:
        if (tfsum2.startsWith("-")) {
          tfsum2 = tfsum2.substring(1, tfsum2.length());
        }
        else {
          tfsum2 = "-" + tfsum1;
        }
        break;
      default:
        break;
    }
    temp1 = Double.parseDouble(tfsum1);
    temp2 = Double.parseDouble(tfsum2);
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -