用java写计算器

//一个小型计算器,自己的一些思考还有借鉴了别人的一些代码然后写出了自己java的最后一个作业计算器
package gui.hbut.edu.cn.www;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

public class Calculator extends JFrame implements ActionListener {
	private JTextField display;                            //这个编辑框用来显示数据
	JPanel jPanel;
	JPanel numbersPanel;
	
	final static int ADD = 1;                              //对加,减,乘,除用常量进行存取,易于操作
	final static int SUB = 2;
	final static int MULT = 3;
	final static int DIV = 4;
	private double number1 = 0.0,number2 = 0.0,result = 0.0;
	private int operation = 0;
	private boolean isCalculating = false;                 //检测状态
	final private String [] buttonText = { "←", "CE", "C", "+", "7", "8", "9", "-", "4", 
			"5", "6", "*", "1", "2", "3", "/", "+/-", "0", ".", "=" };   //用来排版
	public Calculator(){
		jPanel = new JPanel();
		display = new JTextField("0");
		display.setPreferredSize(new Dimension(240, 35));    //编辑框格式
		display.setFont(new Font("", Font.PLAIN, 20));
		display.setHorizontalAlignment(JTextField.RIGHT);
		display.setEditable(false);
		jPanel.add(display, "North");
		
		numbersPanel = new JPanel();
		numbersPanel.setLayout(new GridLayout(5,4,12,25));
		
		for(int i = 0; i < buttonText.length; i++){
			JButton button = new JButton(buttonText[i]);
			numbersPanel.add(button);
			button.addActionListener(this);
		}
		jPanel.add(numbersPanel, "South");
		getContentPane().add(jPanel);
		setTitle("Calculator");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		pack();
		setBounds(100,100,250,320);
		setResizable(false);
		setVisible(true);
	}
	
	public void actionPerformed(ActionEvent e){
		String cmd = e.getActionCommand();             //获取鼠标点击操作符
		if(Character.isDigit(cmd.charAt(0)) || cmd.equals(".")){ 
			if(isCalculating){
				display.setText("0");
				isCalculating = false;
			}
			
			if(display.getText().equals("0")){  //屏幕为0
				if(cmd.equals("0"))
					return ;
				else if(cmd.equals("."))
					display.setText("0.");
				else
					display.setText(cmd);
			}else{                              //屏幕不为0
				if(!display.getText().contains("."))
					display.setText(display.getText() + cmd);
				else if(!cmd.equals(".")){
					display.setText(display.getText() + cmd);
				}
			}
			
		}else{
			if(cmd.equals("+") || cmd.equals("-") || cmd.equals("*") || cmd.equals("/")){
				number1 = Double.parseDouble(display.getText());
				display.setText("");
				if(cmd.equals("+"))
					operation = ADD;
				else if(cmd.equals("-"))
					operation = SUB;
				else if(cmd.equals("*"))
					operation = MULT;
				else
					operation = DIV;
			}
			else if(cmd.equals("C")){   //所有数进行清空
				number1 = 0;
				number2 = 0;
				result = 0;
				display.setText("0");
			}
			else if(cmd.equals("CE")){  //清除正在输入的数
				if(number2 == 0)
					number1 = 0;
				else
					number2 = 0;
			}
			else if(cmd.equals("←")){   //退格符
				if(display.getText().length() == 1)
					display.setText("0");
				else
					display.setText(display.getText().substring(0, display.getText().length()-1));
			}
			 else if (cmd.equals("+/-")) {  //改变屏幕输入数的正负
					if (display.getText().equals("0")) {
						return;
					}
					if (display.getText().contains("-")) {
						display.setText(display.getText().substring(1));
					} else {
						display.setText("-" + display.getText());
					}
			 }
			 else if(cmd.equals("="))
			 {
				 number2 = Double.parseDouble(display.getText());
				 switch (operation) {//按操作运算符号常量分支
					case 1:
						result = number1 + number2;
						break;
					case 2:
						result = number1 - number2;
						break;
					case 3:
						result = number1 * number2;
						break;
					case 4:
						result = number1 / number2;
						break;
					default:
						result = number2;
					}
				 	Number resultObject = result;
					if ((resultObject.doubleValue() - resultObject.intValue()) < 1e-10) {
						String resultString = resultObject.intValue() + "";
						display.setText(resultString);
					} 
					else 
						display.setText(result + "");
					result = 0.0;
					number1 = 0.0;
					number2 = 0.0;
			 }
			
		}
	}
	
	
	public static void main(String []args){
		Calculator calculator = new Calculator();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值