java实现简单计算器功能

网络编程 itxz 1个月前 (12-15) 23次浏览 0个评论

使用java来实现一个简单的计算器,供大家参考,具体内容如下
最近使用java制作了一个计算器,菜单栏里的功能没有实现,只是写一个版式,仅仅实现了计算功能 ,如果想要实现菜单栏功能可自行在该代码的基础上去实现。

话不多说直接上代码


package test;
 
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
 
 public class calculate extends JFrame implements ActionListener {
  private Container ct;
  private JButton[] jbs;
  private String[] str;
  private JTextField jTextField;
  //cal[0]用来存储第一次输入的数
  //cal[1]用来存储运算符(+,-,*,/,%)
  //cal[2]用来存储第二次输入的数
  //cal[3]用来存储结果
  //cal[4]用来存储MS键所存储的数
  //cal[5]用来获取要存入的值(用来给cal[4]赋值)
  private String[] cal = {"","","","","",""};
  //_count和_count1始终相反,如果_count为true则表示正在输入cal[0]反之表示cal[0]输入完成,该输入cal[2]
  //当点过等号后将cal[3]的值赋给cal[0]
  private boolean _count = true;
  private boolean _count1 = false;
 
  public calculate() {
   super("计算器");
   jbs = new JButton[28];
   ct = this.getContentPane();
   this.setBounds(100, 100, 315, 400);
   this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
   ct.setLayout(null);
   //菜单栏
   JMenuBar jMenuBar = new JMenuBar();
   JMenu jm = new JMenu("查看(V)");
   JMenu jm1 = new JMenu("编辑(E)");
   JMenu jm2 = new JMenu("帮助(H)");
   jMenuBar.add(jm);
   jMenuBar.add(jm1);
   jMenuBar.add(jm2);
   this.setJMenuBar(jMenuBar);
   //输出框
   jTextField = new JTextField("0");
   jTextField.setFont(new Font("宋体", Font.BOLD, 25));
   jTextField.setEditable(false);
   jTextField.setHorizontalAlignment(JTextField.RIGHT);
   jTextField.setBounds(5, 5, 285, 65);
   ct.add(jTextField);
   //按键    0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
   str = new String[]{"MC", "MR", "MS", "M+", "M-", "←", "CE", "C", "±", "√", "7", "8", "9", "/", "%", "4", "5", "6", "*", "1/x", "1", "2", "3", "-", "=", "0", ".", "+"};
   int k = 0;
   for (int i = 75; i <= 195; i = i + 40) {
    for (int init = 10; init <= 290; init = init + 57) {
     JButton jButton = new JButton(str[k]);
     jbs[k] = jButton;
     jButton.setBounds(init, i, 52, 35);
     ct.add(jButton);
     k++;
     System.out.println(k);
    }
   }
   for (int i = 235; i < 265; i = i + 40) {
    for (int j = 10; j <= 181; j = j + 57) {
     JButton jButton = new JButton(str[k]);
     jButton.setBounds(j, i, 52, 35);
     ct.add(jButton);
     jbs[k] = jButton;
     k++;
     System.out.println(k);
    }
   }
   JButton jButton = new JButton(str[k]);
   jbs[k] = jButton;
   k++;
   System.out.println(k);
   jButton.setBounds(240, 235, 52, 75);
   ct.add(jButton);
   JButton jButton2 = new JButton(str[k]);
   System.out.println(str[k]);
   jButton2.setBounds(10, 275, 109, 35);
   ct.add(jButton2);
   jbs[k] = jButton2;
   k++;
   for (int i = 275; i < 310; i = i + 40) {
    for (int j = 124; j <= 181; j = j + 57) {
     JButton jButton1 = new JButton(str[k]);
     jButton1.setBounds(j, i, 52, 35);
     ct.add(jButton1);
     jbs[k] = jButton1;
     k++;
    }
   }
   for (int i = 0; i < 28; i++) {
    jbs[i].addActionListener(this);
   }
   this.setVisible(true);
  }
 
  public static void main(String[] args) {
   new calculate();
  }
 
 
  @Override
  public void actionPerformed(ActionEvent actionEvent) {
   if (actionEvent.getActionCommand().equals(".")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("0")){
    method(actionEvent);
   } else if (actionEvent.getActionCommand().equals("1")){
    method(actionEvent);
   }else if(actionEvent.getActionCommand().equals("2")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("3")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("4")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("5")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("6")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("7")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("8")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("9")){
    method(actionEvent);
   }else if (actionEvent.getActionCommand().equals("±")){
    if(_count){
     if (cal[0].contains(".")){
      cal[0] = String.valueOf(Double.parseDouble(cal[0]) * (-1));
      jTextField.setText(cal[0]);
     }else {
      cal[0] = String.valueOf(Integer.parseInt(cal[0]) * (-1));
      jTextField.setText(cal[0]);
     }
    }else {
     if (cal[2].contains(".")){
      cal[2] = String.valueOf(Double.parseDouble(cal[2]) * (-1));
      jTextField.setText(cal[2]);
     }else {
      cal[2] = String.valueOf(Integer.parseInt(cal[2]) * (-1));
      jTextField.setText(cal[2]);
     }
    }
   }else if(actionEvent.getActionCommand().equals("√")){
    if(_count){
     cal[0] = String.valueOf(Math.pow(Double.parseDouble(cal[0]),0.5));
     jTextField.setText(cal[0]);
    }else {
     cal[2] = String.valueOf(Math.pow(Double.parseDouble(cal[2]),0.5));
     jTextField.setText(cal[2]);
    }
   }else if(actionEvent.getActionCommand().equals("1/x")){
    if(_count){
     cal[0] = String.valueOf(1/Double.parseDouble(cal[0]));
     jTextField.setText(cal[0]);
    }else {
     cal[2] = String.valueOf(1/Double.parseDouble(cal[2]));
     jTextField.setText(cal[2]);
    }
   }else if(actionEvent.getActionCommand().equals("←")){
    if(_count){
     cal[0] = cal[0].substring(0,cal[0].length()-1);
     if(cal[0].equals("")){
      jTextField.setText("0");
     }else {
      jTextField.setText(cal[0]);
     }
    }else {
     cal[2] = cal[2].substring(0,cal[2].length()-1);
     if(cal[2].equals("")){
      jTextField.setText("0");
     }else {
      jTextField.setText(cal[2]);
     }
    }
   }else if(actionEvent.getActionCommand().equals("CE")){
    if (_count){
     cal[0]="";
     jTextField.setText("0");
    }else {
     cal[2]="";
     jTextField.setText("0");
    }
   }else if(actionEvent.getActionCommand().equals("C")){
    _count = true;
    cal[0]="";
    cal[1]="";
    cal[2]="";
    cal[3]="";
    jTextField.setText("0");
   }else if (actionEvent.getActionCommand().equals("+")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
     }else {
      cal[0] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "+";
    _count=false;
    jTextField.setText(cal[1]);
   }else if (actionEvent.getActionCommand().equals("-")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
     }else {
      cal[0] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "-";
    _count=false;
    jTextField.setText(cal[1]);
   }else if (actionEvent.getActionCommand().equals("*")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
     }else {
      cal[0] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "*";
    _count=false;
    jTextField.setText(cal[1]);
   }else if (actionEvent.getActionCommand().equals("/")){
    if (!cal[0].equals("")&&!cal[2].equals("")){
     if (cal[0].contains(".") || cal[2].contains(".")){
      cal[0]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
     }else {
      cal[0] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
     }
     cal[2]="";
    }
    cal[1] = "/";
    _count=false;
    jTextField.setText(cal[1]);
   }else if (actionEvent.getActionCommand().equals("%")){
    cal[0]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
    cal[2]="";
    cal[1] = "%";
    _count=false;
    jTextField.setText(cal[1]);
   }else if (actionEvent.getActionCommand().equals("=")){
    if(cal[0].equals("")&&cal[2].equals("")){
     jTextField.setText("0");
    }else if(!cal[0].equals("")&&cal[2].equals("")){
     jTextField.setText(cal[0]);
    }else if(!cal[0].equals("")&&!cal[2].equals("")){
     if(cal[1].equals("+")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=String.valueOf(Double.parseDouble(cal[0]) + Double.parseDouble(cal[2]));
      }else {
       cal[3] = String.valueOf(Integer.parseInt(cal[0]) + Integer.parseInt(cal[2]));
      }
      jTextField.setText(cal[3]);
     }else if (cal[1].equals("-")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=String.valueOf(Double.parseDouble(cal[0]) - Double.parseDouble(cal[2]));
      }else {
       cal[3] = String.valueOf(Integer.parseInt(cal[0]) - Integer.parseInt(cal[2]));
      }
      jTextField.setText(cal[3]);
     }else if(cal[1].equals("*")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=String.valueOf(Double.parseDouble(cal[0]) * Double.parseDouble(cal[2]));
      }else {
       cal[3] = String.valueOf(Integer.parseInt(cal[0]) * Integer.parseInt(cal[2]));
      }
      jTextField.setText(cal[3]);
     }else if (cal[1].equals("/")){
      if (cal[0].contains(".") || cal[2].contains(".")){
       cal[3]=String.valueOf(Double.parseDouble(cal[0]) / Double.parseDouble(cal[2]));
      }else {
       cal[3] = String.valueOf(Integer.parseInt(cal[0]) / Integer.parseInt(cal[2]));
      }
      jTextField.setText(cal[3]);
     }else if (cal[1].equals("%")){
      cal[3]=String.valueOf(Integer.parseInt(cal[0]) % Integer.parseInt(cal[2]));
      jTextField.setText(cal[3]);
     }
    }
    cal[0]=cal[3];
    cal[2]="";
    _count=true;
    _count1 = true;
    cal[5]=cal[0];
   }else if(actionEvent.getActionCommand().equals("MC")){
    cal[4]="";
   }else if(actionEvent.getActionCommand().equals("MR")){
    if(_count){
     if(!cal[4].equals("")){
      cal[0]=cal[4];
      jTextField.setText(cal[0]);
     }
    }else {
     if(!cal[4].equals("")){
      cal[2]=cal[4];
      jTextField.setText(cal[2]);
     }
    }
   }else if(actionEvent.getActionCommand().equals("MS")){
    if(_count){
     cal[0]="";
    }
    cal[4]=cal[5];
    System.out.println("cal[5]"+cal[5]);
    System.out.println("ms:"+cal[4]);
   }else if(actionEvent.getActionCommand().equals("M+")){
    if(!cal[4].equals("")){
     if (cal[4].contains(".") || cal[5].contains(".")){
      cal[4]=String.valueOf(Double.parseDouble(cal[4]) + Double.parseDouble(cal[5]));
     }else {
      cal[4] = String.valueOf(Integer.parseInt(cal[4]) + Integer.parseInt(cal[5]));
     }
    }
   }else if(actionEvent.getActionCommand().equals("M-")){
    if(!cal[4].equals("")){
     if (cal[4].contains(".") || cal[5].contains(".")){
      cal[4]=String.valueOf(Double.parseDouble(cal[4]) - Double.parseDouble(cal[5]));
     }else {
      cal[4] = String.valueOf(Integer.parseInt(cal[4]) - Integer.parseInt(cal[5]));
     }
    }
   }
  }
  public void method(ActionEvent actionEvent){
   if(_count){
    if (_count1){
     cal[0]="";
     _count1=false;
    }
    cal[0] += actionEvent.getActionCommand();
    cal[5]=cal[0];
    System.out.println(cal[5]);
   }else {
    cal[2] += actionEvent.getActionCommand();
    cal[5]=cal[2];
   }
   jTextField.setText(actionEvent.getActionCommand());
  }
}

效果图如下:

java实现简单计算器功能


IT学者 , 版权所有丨如未注明 , 均为原创丨本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:java实现简单计算器功能
喜欢 (0)

您必须 登录 才能发表评论!