⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 calculator.java

📁 JAVA实现小型计算器程序。对于JAVA初学者有参考
💻 JAVA
字号:
/* Calculator.java
 * 
 * Create on Mar 24,2009
 * 
 * author : john
 * 
 */

import javax.swing.*;
import java.awt.*;
import java.applet.*;
import java.awt.event.*;
import java.math.*;
import java.util.*;

public class Calculator extends JFrame{

/* 
 * 计算器界面
 */	
	JTextField ResultField = new JTextField(23);
	JButton No0 = new JButton("0"); JButton No1 = new JButton("1"); JButton No2 = new JButton("2"); 
	JButton No3 = new JButton("3"); JButton No4 = new JButton("4"); JButton No5 = new JButton("5"); 
	JButton No6 = new JButton("6"); JButton No7 = new JButton("7"); JButton No8 = new JButton("8");
	JButton No9 = new JButton("9"); JButton Dot = new JButton(".");
	JButton Add = new JButton("+"); JButton Sub = new JButton("-"); JButton Mul = new JButton("×"); 
	JButton Div = new JButton("÷"); JButton Root = new JButton("√"); JButton C = new JButton("C"); 
	JButton CE = new JButton("CE"); JButton Opp = new JButton("+/-"); JButton Equal = new JButton("=");
	GridLayout NumLayout = new GridLayout(4,5,2,2);
	
	JPanel Cal_Panel = new JPanel(true);
	JPanel Cal_Num = new JPanel(true);
	
	
	class CalWindowListener implements WindowListener{
		public void windowClosing(WindowEvent w){
			System.out.println("exit MyCalculator...");
			System.exit(0);
		}
		public void windowClosed(WindowEvent w){
			System.out.println("exit Successfully!");
		}
		public void windowOpened(WindowEvent w){
			
		}
		public void windowActivated(WindowEvent w){
			
		}
		public void windowIconified(WindowEvent w){
			
		}
		public void windowDeactivated(WindowEvent w){
			
		}
		public void windowDeiconified(WindowEvent w){
			
		}
	}
	
	public Calculator(){
	super("Calculator");
	CalWindowListener CalWindow = new CalWindowListener();
	addWindowListener(CalWindow);
	
		
//整体布局	
	getContentPane().add(Cal_Panel);
//Num方阵布局
	Cal_Num.setLayout(NumLayout);
	
	Cal_Num.add(No7);Cal_Num.add(No8);Cal_Num.add(No9);Cal_Num.add(CE);Cal_Num.add(C);
	Cal_Num.add(No4);Cal_Num.add(No5);Cal_Num.add(No6);	Cal_Num.add(Mul);Cal_Num.add(Div);
	Cal_Num.add(No1);Cal_Num.add(No2);Cal_Num.add(No3);	Cal_Num.add(Add);Cal_Num.add(Sub);
	Cal_Num.add(No0);Cal_Num.add(Dot);Cal_Num.add(Opp);Cal_Num.add(Root);Cal_Num.add(Equal);
	
	No0.addActionListener(new BtnActionAdapter());No1.addActionListener(new BtnActionAdapter());
	No2.addActionListener(new BtnActionAdapter());No3.addActionListener(new BtnActionAdapter());
	No4.addActionListener(new BtnActionAdapter());No5.addActionListener(new BtnActionAdapter());
	No6.addActionListener(new BtnActionAdapter());No7.addActionListener(new BtnActionAdapter());
	No8.addActionListener(new BtnActionAdapter());No9.addActionListener(new BtnActionAdapter());
	Add.addActionListener(new BtnActionAdapter());Mul.addActionListener(new BtnActionAdapter());
	Div.addActionListener(new BtnActionAdapter());Sub.addActionListener(new BtnActionAdapter());
	Opp.addActionListener(new BtnActionAdapter());Root.addActionListener(new BtnActionAdapter());
	CE.addActionListener(new BtnActionAdapter());C.addActionListener(new BtnActionAdapter());
	Equal.addActionListener(new BtnActionAdapter());Dot.addActionListener(new BtnActionAdapter());
	
	
//设置颜色
	C.setForeground(Color.RED);
	
	Cal_Panel.add(ResultField);
	Cal_Panel.add(Cal_Num);
	
 
	ResultField.setBackground(Color.decode("81119991"));//个人认为这个颜色比较好。^_^?!
		
	ResultField.setHorizontalAlignment(JTextField.RIGHT);
	setSize(265,186);
	setVisible(true);
	//setUndecorated(true);
	
			
	}

	/*
	 * 计算器功能实现
	 */
	
	//链表结构:存放运算数,以实现优先级算术
	LinkedList<String> CalList = new LinkedList<String>();
	String CalNum ="";
	class BtnActionAdapter implements ActionListener{
			public void actionPerformed(ActionEvent e){
				if(e.getSource() == No0){
					CalNum += "0";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No1){
					CalNum += "1";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No2){
					CalNum += "2";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No3){
					CalNum += "3";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No4){
					CalNum += "4";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No5){
					CalNum += "5";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No6){
					CalNum += "6";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No7){
					CalNum += "7";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No8){
					CalNum += "8";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == No9){
					CalNum += "9";
					ResultField.setText(CalNum);
				}
				if(e.getSource() == Dot){
					if(CalNum != "")
						CalNum += ".";
					else
						CalNum +="0.";
					ResultField.setText(CalNum);
				}
				
				if(e.getSource() == Add){
					if(CalNum != "" )
					CalList.add(CalNum);
					CalList.add("+");
					CalNum = "";
					ResultField.setText("");
				}
				if(e.getSource() == Sub){
					if(CalNum != "" )
					CalList.add(CalNum);
					CalList.add("-");
					CalNum = "";
					ResultField.setText("");
				}
				if(e.getSource() == Mul){
					if(CalNum != "" )
					CalList.add(CalNum);
					CalList.add("*");
					CalNum = "";
					ResultField.setText("");
				}
				if(e.getSource() == Div){
					if(CalNum != "" )
					CalList.add(CalNum);
					CalList.add("%");
					CalNum = "";
					ResultField.setText("");
				}
				if(e.getSource() == Opp){			
					if(CalNum !=""){
						Integer Opp;
						Opp = (-1)*Integer.parseInt(CalNum);
						CalNum = Opp.toString(Opp.intValue());
						ResultField.setText(CalNum);
					}
					else if(CalNum == "" && !CalList.isEmpty()){
						if(CalList.size() == 1){
							Float Opp_;
							Opp_ = -1*Float.parseFloat(CalList.element());
							CalList.set(0,Opp_.toString(Opp_.floatValue()));
							ResultField.setText(CalList.element());
						}
					}
					
				}
				if(e.getSource() == Root){
					if(CalNum !=""){
						CalNum = (Root(CalNum)).toString(Root(CalNum).floatValue());
						ResultField.setText(CalNum);
					}
					else if(CalNum == "" && !CalList.isEmpty()){
						if(CalList.size() == 1){
							Float sqrt;
							sqrt = Root(CalList.element());
							CalList.set(0, sqrt.toString(sqrt.floatValue()));
							ResultField.setText(CalList.element());
						}
					}
				}
				
				if(e.getSource() == CE){
					CalNum = "";
					ResultField.setText("");
				}
				
				if(e.getSource() == C){
					CalNum = "";
					CalList.clear();
					ResultField.setText("");
				}
				
				if(e.getSource() == Equal){
					int MulIndex,DivIndex,AddIndex,SubIndex;
					Float temp;
					String Item;
					if(CalNum != "" && CalList.size() > 1)
					CalList.add(CalNum);					
					
					while(CalList.size() > 1){
						//乘法,除法优先级处理
						if(CalList.contains("*")&&CalList.contains("%")){
							MulIndex = CalList.indexOf("*");
							DivIndex = CalList.indexOf("%");
							if(MulIndex < DivIndex){
								temp =  Mul(Float.parseFloat(CalList.get(MulIndex-1)),Float.parseFloat(CalList.get(MulIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(MulIndex,Item);
								CalList.remove(MulIndex+1);
								CalList.remove(MulIndex-1);
						
								DivIndex -= 2;//remove()使Index发生了改变
								temp =  Div(Float.parseFloat(CalList.get(DivIndex-1)),Float.parseFloat(CalList.get(DivIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(DivIndex,Item);
								CalList.remove(DivIndex+1);
								CalList.remove(DivIndex-1);

							}
							else{
								temp =  Div(Float.parseFloat(CalList.get(DivIndex-1)),Float.parseFloat(CalList.get(DivIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(DivIndex,Item);
								CalList.remove(DivIndex+1);
								CalList.remove(DivIndex-1);
								
								MulIndex -= 2;
								temp =  Mul(Float.parseFloat(CalList.get(MulIndex-1)),Float.parseFloat(CalList.get(MulIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(MulIndex,Item);
								CalList.remove(MulIndex+1);
								CalList.remove(MulIndex-1);
							}
						}
						if(CalList.contains("*")&&!CalList.contains("%")){
							MulIndex = CalList.indexOf("*");
							temp =  Mul(Float.parseFloat(CalList.get(MulIndex-1)),Float.parseFloat(CalList.get(MulIndex+1)));
							Item = temp.toString(temp.floatValue());
							CalList.set(MulIndex,Item);
							CalList.remove(MulIndex+1);
							CalList.remove(MulIndex-1);
						}
						if(!CalList.contains("*")&&CalList.contains("%")){
							DivIndex = CalList.indexOf("%");
							temp =  Div(Float.parseFloat(CalList.get(DivIndex-1)),Float.parseFloat(CalList.get(DivIndex+1)));
							Item = temp.toString(temp.floatValue());
							CalList.set(DivIndex,Item);
							CalList.remove(DivIndex+1);
							CalList.remove(DivIndex-1);
						}


						//加减法处理
						if(CalList.contains("+")&&CalList.contains("-")){
							AddIndex = CalList.indexOf("+");
							SubIndex = CalList.indexOf("-");
							if(AddIndex < SubIndex){
								temp =  Add(Float.parseFloat(CalList.get(AddIndex-1)),Float.parseFloat(CalList.get(AddIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(AddIndex,Item);
								CalList.remove(AddIndex+1);
								CalList.remove(AddIndex-1);
						
								SubIndex -= 2;
								temp =  Sub(Float.parseFloat(CalList.get(SubIndex-1)),Float.parseFloat(CalList.get(SubIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(SubIndex,Item);
								CalList.remove(SubIndex+1);
								CalList.remove(SubIndex-1);
							}
							else{
								temp =  Sub(Float.parseFloat(CalList.get(SubIndex-1)),Float.parseFloat(CalList.get(SubIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(SubIndex,Item);
								CalList.remove(SubIndex+1);
								CalList.remove(SubIndex-1);
								
								SubIndex -= 2;
								temp =  Add(Float.parseFloat(CalList.get(AddIndex-1)),Float.parseFloat(CalList.get(AddIndex+1)));
								Item = temp.toString(temp.floatValue());
								CalList.set(AddIndex,Item);
								CalList.remove(AddIndex+1);
								CalList.remove(AddIndex-1);
							}
						}
						if(CalList.contains("+")&&!CalList.contains("-")){
							AddIndex = CalList.indexOf("+");
							temp =  Add(Float.parseFloat(CalList.get(AddIndex-1)),Float.parseFloat(CalList.get(AddIndex+1)));
							Item = temp.toString(temp.floatValue());
							CalList.set(AddIndex,Item);
							CalList.remove(AddIndex+1);
							CalList.remove(AddIndex-1);
						}
						if(!CalList.contains("+")&&CalList.contains("-")){
							SubIndex = CalList.indexOf("-");
							temp =  Sub(Float.parseFloat(CalList.get(SubIndex-1)),Float.parseFloat(CalList.get(SubIndex+1)));
							Item = temp.toString(temp.floatValue());
							CalList.set(SubIndex,Item);
							CalList.remove(SubIndex+1);
							CalList.remove(SubIndex-1);
						}
						
						ResultField.setText(CalList.element());
						CalNum = "";
						
						}

					}

				
				}
		}

	public Float Add(Float a,Float b){
		return a+b;
	}
	
	public Float Sub(Float a,Float b){
		return a-b;
	}
	
	public Float Mul(Float a,Float b){
		return a*b;
	}
	
	public Float Div(Float a,Float b){
		return a/b;
	}
	
	public Float Root(String a){
		Double sqrt = Math.sqrt(Double.parseDouble(a));
		return Float.parseFloat(sqrt.toString(sqrt.doubleValue()));
	}
	
	
	public static void main(String []args){
		new Calculator();
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -