java 简单计算器
Posted xiaoxiaodeyan
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 简单计算器相关的知识,希望对你有一定的参考价值。
package mdthob;//package com.cal;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Stack;
import javax.swing.*;
class MyException extends Exception
public MyException(String message)
super(message);
class SwingConsole
public static void run(final JFrame f,final int width,final int height)
SwingUtilities.invokeLater(() ->
f.setTitle(f.getClass().getSimpleName());
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setSize(width,height);
f.setVisible(true);
);
public class MyCalculator extends JFrame
/*
*
*/
private final JTextField textField; //输入文本框
private String input; //结果
public MyCalculator()
input = "";
//操作符
Container container = this.getContentPane();
JPanel panel = new JPanel();
textField = new JTextField(30);
textField.setEditable(false); //文本框禁止编辑
textField.setHorizontalAlignment(JTextField.LEFT);
//textField.setBounds(100, 100, 20, 20); //在容器布局为空情况下生效
textField.setPreferredSize(new Dimension(200,30));
container.add(textField, BorderLayout.NORTH);
String[] name= "7","8","9","+","4","5","6","-","1","2","3","*","0","C","=","/";
panel.setLayout(new GridLayout(4,4,1,1));
for (String s : name)
JButton button = new JButton(s);
button.addActionListener(new MyActionListener());
panel.add(button);
container.add(panel,BorderLayout.CENTER);
class MyActionListener implements ActionListener //内部类实现按钮响应
@Override
public void actionPerformed(ActionEvent e)
int cnt=0;
String actionCommand = e.getActionCommand(); //获取按钮上的字符串
switch (actionCommand)
case "+":
case "-":
case "*":
case "/":
input += " " + actionCommand + " ";
break;
case "C": //清除输入
input = "";
break;
case "=": //按下等号
try
input += "=" + calculate(input);
catch (MyException e1)
if (e1.getMessage().equals("Infinity"))
input += "=" + e1.getMessage();
else
input = e1.getMessage();
textField.setText(input);
input = "";
cnt = 1;
break;
default:
input += actionCommand; //按下数字
break;
if(cnt == 0)
textField.setText(input);
private String calculate(String input) throws MyException //计算函数
String[] comput = input.split(" ");
Stack<Double> stack = new Stack<>();
Double m = Double.parseDouble(comput[0]);
stack.push(m); //第一个操作数入栈
for(int i = 1; i < comput.length; i++)
if(i%2==1)
if(comput[i].equals("+"))
stack.push(Double.parseDouble(comput[i+1]));
if(comput[i].equals("-"))
stack.push(-Double.parseDouble(comput[i+1]));
if(comput[i].equals("*")) //将前一个数出栈做乘法再入栈
Double d = stack.peek(); //取栈顶元素
stack.pop();
stack.push(d*Double.parseDouble(comput[i+1]));
if(comput[i].equals("/")) //将前一个数出栈做乘法再入栈
double help = Double.parseDouble(comput[i+1]);
if(help == 0)
throw new MyException("Infinity"); //不会继续执行该函数
double d = stack.peek();
stack.pop();
stack.push(d/help);
double d = 0d;
while(!stack.isEmpty()) //求和
d += stack.peek();
stack.pop();
return String.valueOf(d);
public static void main(String[] args)
SwingConsole.run(new MyCalculator(), 250, 300);
以上是关于java 简单计算器的主要内容,如果未能解决你的问题,请参考以下文章