java 计算器
Posted 夏芷雨涵梦
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java 计算器相关的知识,希望对你有一定的参考价值。
题目:简易计算器
时间:2019.3.10
介绍:做一个简易的计算器,实现简单的加减乘除运算。本文代码在Eclipse上调试运行。
package calculate;
import javax.swing.*;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.GridLayout;
import java.awt.event.*;
public class cal extends JFrame implements ActionListener
JMenuBar menubar = new JMenuBar();
JMenu menu1, menu2, menu3;
JMenuItem item1, item2, item3, item4, item5, item6, item7, item8;
JTextField area = new JTextField("0");
JPanel panel = new JPanel();
String[] str = "CE", "C", "%", "sqrt", "7", "8", "9", "/", "4", "5", "6",
"*", "1", "2", "3", "-", "0", ".", "+", "=" ;
JButton button[] = new JButton[str.length];
double result = 0.0;
boolean first = true;//是否是整个表达式的第一个数字,或者是运算符后的第一个数字
String operator = "=";//当前运算符
cal()
init();
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
void init()
menu1 = new JMenu("查看");
menu2 = new JMenu("编辑");
menu3 = new JMenu("帮助");
item1 = new JMenuItem("标准型");
item2 = new JMenuItem("科学型");
item3 = new JMenuItem("程序员");
item4 = new JMenuItem("复制");
item5 = new JMenuItem("粘贴");
item6 = new JMenuItem("历史记录");
item7 = new JMenuItem("帮助文件");
item8 = new JMenuItem("关于计算器");
menu1.add(item1);
menu1.add(item2);
menu1.add(item3);
menu2.add(item4);
menu2.add(item5);
menu2.add(item6);
menu3.add(item7);
menu3.add(item8);
menubar.add(menu1);
menubar.add(menu2);
menubar.add(menu3);
setJMenuBar(menubar);
area.setHorizontalAlignment(JTextField.RIGHT);
area.setSize(200, 50);
area.setBackground(Color.white);
add(area, BorderLayout.NORTH);
panel.setLayout(new GridLayout(5, 4, 3, 3));
for (int i = 0; i < str.length; i++)
button[i] = new JButton(str[i]);
panel.add(button[i]);
button[i].setForeground(Color.blue);
button[i].addActionListener(this);
add(panel, BorderLayout.SOUTH);
@Override
public void actionPerformed(ActionEvent e)
// TODO 自动生成的方法存根
String lable = e.getActionCommand();
if ("0123456789.".indexOf(lable) >= 0)
handleNum(lable);
else if (lable.equals("CE"))
handleCe();
else if (lable.equals("C"))
handleC();
else
handle(lable);
private void handle(String lable)
if (operator == "/")
if (getNumberFromText() == 0.0)
area.setText("除数不能为0");
else
result = result / getNumberFromText();
else if (operator == "*")
result = result * getNumberFromText();
else if (operator == "-")
result = result - getNumberFromText();
else if (operator == "+")
result = result + getNumberFromText();
else if (operator == "=")
result = getNumberFromText();
else if (operator == "%")
result = result / 100;
else if (operator == "sqrt")
result = Math.sqrt(result);
area.setText(String.valueOf(result));
// TODO 自动生成的方法存根
first = true;
operator = lable;
private double getNumberFromText()
// TODO 自动生成的方法存根
double num = 0.0;
try
num = Double.valueOf(area.getText()).doubleValue();
catch (NumberFormatException e)
return num;
private void handleC()
// TODO 自动生成的方法存根
String text = area.getText();
int i = text.length();
if (i > 0)
text = text.substring(0, i - 1);
if (text.length() == 0)
area.setText("0");
first = true;
operator = "=";
else
area.setText(text);
private void handleNum(String key)
// TODO 自动生成的方法存根
if (first)
area.setText(key);
else if ((key.equals(".")) && (area.getText().indexOf(".") < 0))
area.setText(area.getText() + ".");
else if (!key.equals("."))
area.setText(area.getText() + key);
first = false;
private void handleCe()
// TODO 自动生成的方法存根
area.setText("0");
first = true;
operator = "=";
package calculate;
import javax.swing.JFrame;
public class main
public static void main(String[] args)
cal cal=new cal();
cal.setTitle("计算器");
cal.setVisible(true);
cal.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cal.setSize(400,300);
运算截图:
以上是关于java 计算器的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.NumberFormatException: Infinite or NaN原因之浮点类型除数为0结果探究