用java實現一個簡單的計算器

涼寒發表於2020-12-04

java簡單的計算器簡介

使用Java實現一個簡單的整數計算器,可是實現加減乘除操作,使用swing,awt模型開發。相比一些常見的計算器,本文設計的計算器可以實現多個操作符的長公式計算。
本文設計的計算器在被除數是0的時候會自動除以一,以此防止出現錯誤。

將會進行的改變

本文設計的計算器暫時只支援簡單的加減乘除操作,之後會新增浮點數的加減乘除及其他負載一些的操作。

使用的環境

jdk1.8
本文使用IDEA做的程式碼demo,但是程式碼可在所有java編輯器上執行,

問題

如有問題歡迎指出,

程式碼

package lxl.testdemo;


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

public class MiniJisuanqi extends JFrame {
    JPanel jPanelText;
    JTextField textField;
    JPanel jPanelButton;
    JButton jButton1;
    JButton jButton2;
    JButton jButton3;
    JButton jButtonAdd;

    JButton jButton4;
    JButton jButton5;
    JButton jButton6;
    JButton jButtonJian;

    JButton jButton7;
    JButton jButton8;
    JButton jButton9;
    JButton jButtonMul;

    JButton jButton0;
    JButton jButtonChu;
    JButton jButtonHu;
    JButton jButtonEqual;
    int sum=0;
    String opString="";
	// 數字鍵的監聽器
    public ActionListener buttonListener(String text){
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText(textField.getText()+text);
            }
        };
    }
    //清除鍵的監聽器
    public ActionListener clearButtonListener(){
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                textField.setText("");
            }
        };
    }
    //計算操作
    private void ops(){
        switch (opString) {
            case "+":
                if (textField.getText() != "") {
                    sum += Integer.parseInt(textField.getText());
                }
                else sum +=0;
                textField.setText(sum+"");
                opString="";
                break;
            case "-":
                if (textField.getText() != "") {
                    sum -= Integer.parseInt(textField.getText());
                }
                else sum -=0;
                textField.setText(sum+"");
                opString="";
                break;
            case "*":
                if (textField.getText() != "") {
                    sum *= Integer.parseInt(textField.getText());
                }
                else sum *=0;
                textField.setText(sum+"");
                opString="";
                break;
            case "/":
                if (textField.getText() != "") {
                    sum /= Integer.parseInt(textField.getText())!=0?Integer.parseInt(textField.getText()):1;
                }
                else sum /= 1;
                textField.setText(sum+"");
                opString="";
                break;
        }
    }
	//加減乘除及等於號的監聽器
    public ActionListener optButtonListener(String op){
        return new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (op!="="){
                    ops();
                    opString=op;
                    if (textField.getText() != "") {
                        sum = Integer.parseInt(textField.getText());
                        textField.setText("");
                    }
                    else sum=0;
                }
                else {
                    ops();
                }
            }
        };
    }
	//初始化介面操作
    public MiniJisuanqi(){
        jPanelText = new JPanel();
        textField = new JTextField(25);
        textField.setText("");
        textField.setEditable(true);
        jPanelText.add(textField);
        jPanelButton = new JPanel(new GridLayout(4,4,10,10));
        jButton1 = new JButton("1");
        jButton1.addActionListener(buttonListener(jButton1.getText()));
        jButton2 = new JButton("2");
        jButton2.addActionListener(buttonListener(jButton2.getText()));
        jButton3 = new JButton("3");
        jButton3.addActionListener(buttonListener(jButton3.getText()));
        jButtonAdd = new JButton("+");
        jButtonAdd.addActionListener(optButtonListener(jButtonAdd.getText()));

        jButton4 = new JButton("4");
        jButton4.addActionListener(buttonListener(jButton4.getText()));
        jButton5 = new JButton("5");
        jButton5.addActionListener(buttonListener(jButton5.getText()));
        jButton6 = new JButton("6");
        jButton6.addActionListener(buttonListener(jButton6.getText()));
        jButtonJian = new JButton("-");
        jButtonJian.addActionListener(optButtonListener(jButtonJian.getText()));

        jButton7 = new JButton("7");
        jButton7.addActionListener(buttonListener(jButton7.getText()));
        jButton8 = new JButton("8");
        jButton8.addActionListener(buttonListener(jButton8.getText()));
        jButton9 = new JButton("9");
        jButton9.addActionListener(buttonListener(jButton9.getText()));
        jButtonMul = new JButton("*");
        jButtonMul.addActionListener(optButtonListener(jButtonMul.getText()));

        jButton0 = new JButton("0");
        jButton0.addActionListener(buttonListener(jButton0.getText()));
        jButtonChu = new JButton("/");
        jButtonChu.addActionListener(optButtonListener(jButtonChu.getText()));
        jButtonHu = new JButton("清除");
        jButtonHu.addActionListener(clearButtonListener());
        jButtonEqual = new JButton("=");
        jButtonEqual.addActionListener(optButtonListener(jButtonEqual.getText()));
        jPanelButton.add(jButton1);jPanelButton.add(jButton2);jPanelButton.add(jButton3);jPanelButton.add(jButtonAdd);
        jPanelButton.add(jButton4);jPanelButton.add(jButton5);jPanelButton.add(jButton6);jPanelButton.add(jButtonJian);
        jPanelButton.add(jButton7);jPanelButton.add(jButton8);jPanelButton.add(jButton9);jPanelButton.add(jButtonMul);
        jPanelButton.add(jButton0);jPanelButton.add(jButtonEqual);jPanelButton.add(jButtonHu);jPanelButton.add(jButtonChu);
        getContentPane().add("North",jPanelText);
        getContentPane().add("Center",jPanelButton);
    }
    主函式
    public static void main(String[] args) {
        MiniJisuanqi miniJisuanqi = new MiniJisuanqi();
        miniJisuanqi.setTitle("簡單計算器");
        miniJisuanqi.setVisible(true);
        miniJisuanqi.pack();
        miniJisuanqi.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        miniJisuanqi.setResizable(false);
    }
}

以下是計算機初始化的樣式。
計算機圖片

相關文章