簡單模仿win10計算器(demo)

計算器

  • 這個小demo是我上學時的遠古代碼(嘻嘻嘻),今天整理代碼時看到的,看着以前的代碼,突然感覺這些是啥?看不懂了都,而且寫得也不規範。
  • 運行一下,還是可以的,先截張圖在這裏插入圖片描述
  • 試了一下,bug還是有的,但是可以基本的運算,有興趣的可以試一下
  • 代碼就貼在這裏,感慨!!!
package com.waking.call;


import java.awt.BorderLayout;
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.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;






/**
 * 計算器小案例
 * @author waking
 *
 */
public class CalculatorExample {

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		CalculatorFrame calculatorFrame=new CalculatorFrame();
	}
}
class CalculatorFrame extends JFrame{
	public CalculatorFrame() {//構造函數
		setTitle("計算器");
		setSize(340,500);
		CalcultorPanel calcultorPanel=new CalcultorPanel();
		getContentPane().add(calcultorPanel);//給當前Frame添加面板
		setLocationRelativeTo(null);//居中
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}
}
class CalcultorPanel extends JPanel{//主面板
	public static boolean start=true;//新數字鍵輸入的開始
	public static String result="";//保存中間結果的參數
	public static String lastcommand="=";//記錄上一個運算符
	public static boolean firstDigit = true;//輸入第一個數
	public CalcultorPanel() {		
		setLayout(new BorderLayout());
		ShowPanel showPanel=new ShowPanel();
		BTNPanel btnPanel=new BTNPanel();
		add(showPanel, BorderLayout.NORTH);
		add(btnPanel,BorderLayout.CENTER);
	}	
}
class ShowPanel extends JPanel{//結果面板
		public static JLabel display1;
		public static JLabel display2;
	public ShowPanel() {
		display1=new JLabel("",SwingConstants.RIGHT);
		display2=new JLabel("0",SwingConstants.RIGHT);
		display1.setFont(new Font("黑體",Font.BOLD,30));
		display2.setFont(new Font("黑體",Font.BOLD,40));
		display1.setPreferredSize(new Dimension(300, 80));
		display2.setPreferredSize(new Dimension(300, 50));
		setLayout(new BorderLayout());
		add(display1,BorderLayout.NORTH);
		add(display2,BorderLayout.CENTER);
	}
} 
class BTNPanel extends JPanel{//按鈕面板
	public BTNPanel() {
		setLayout(new GridLayout(6, 4));
		InsertAciton insertAciton=new InsertAciton();
		CommandAction commandAction=new CommandAction();
		addButton("%", commandAction);
		addButton("√", commandAction);
		addButton("pow", commandAction);
		addButton("1/x", commandAction);
		addButton("CE", commandAction);
		addButton("C", commandAction);
		addButton("<<", commandAction);
		addButton("/",commandAction);
		addButton("7",insertAciton);
		addButton("8",insertAciton);
		addButton("9",insertAciton);
		addButton("*",commandAction);
		addButton("4",insertAciton);
		addButton("5",insertAciton);
		addButton("6",insertAciton);
		addButton("-",commandAction);
		addButton("1",insertAciton);
		addButton("2",insertAciton);
		addButton("3",insertAciton);
		addButton("+",commandAction);
		addButton("+/-",commandAction);
		addButton("0",insertAciton);
		addButton(".",insertAciton);
		addButton("=",commandAction);
		
	}
	void addButton(String label,ActionListener actionListener) {//添加按鈕
		JButton button=new JButton(label);
		button.setFont(new Font("黑體",Font.BOLD,15));
		button.addActionListener(actionListener);
		add(button);
	}
}
class InsertAciton implements ActionListener{//數字鍵按鈕事件
	@Override
	
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
		if(CalcultorPanel.start==true) {
			//清空文本
			ShowPanel.display1.setText("");
			ShowPanel.display2.setText("");
			CalcultorPanel.start=false;
		}
		 if ("0123456789.".indexOf(e.getActionCommand()) >= 0) {
		 if (CalcultorPanel.firstDigit) {
	            // 輸入的第一個數字
			if(e.getActionCommand().equals(".")) {//如果第一個是“.”
				ShowPanel.display2.setText("0.");
				 }else if(e.getActionCommand().equals("0")) {//如果第一個是“0”
					 ShowPanel.display2.setText("0");
					 CalcultorPanel.firstDigit=true;
					 return;
				 }
				 else	 ShowPanel.display2.setText(e.getActionCommand());
		 }
		 else if ((e.getActionCommand().equals(".")) && (ShowPanel.display2.getText().indexOf(".") < 0)) {
            // 輸入的是小數點,並且之前沒有小數點,則將小數點附在結果文本框的後面
			 ShowPanel.display2.setText(ShowPanel.display2.getText() + ".");
        } 
        else //if(Double.parseDouble(ShowPanel.display2.getText())!=0)	
        if (!e.getActionCommand().equals(".")) {
            // 如果輸入的不是小數點,則將數字附在結果文本框的後面
        	ShowPanel.display2.setText(ShowPanel.display2.getText() +  e.getActionCommand());
        }
       // 以後輸入的肯定不是第一個數字了
		 CalcultorPanel.firstDigit = false;
	}
	}
	
}
class CommandAction implements ActionListener{//功能事件

	public void actionPerformed(ActionEvent e)  {
		// TODO Auto-generated method stub
		
		String string = e.getActionCommand();
		if(string.equals("CE")) {
			ShowPanel.display1.setText("");
			ShowPanel.display2.setText("0");
        }else if(string.equals("C")) {
        	CalcultorPanel.start=true;//新數字鍵輸入的開始
        	CalcultorPanel.result="";//保存中間結果的參數
        	CalcultorPanel.lastcommand="=";//記錄上一個運算符
        	CalcultorPanel.firstDigit = true;//輸入第一個數
        	ShowPanel.display1.setText("");
        	ShowPanel.display2.setText("0");
        }else if(string.equals("<<")) {//退格
        	ShowPanel.display1.setText("");
        	String text=ShowPanel.display2.getText();
	        	int i = text.length();
	            if (i > 0) {
	                // 退格,將文本最後一個字符去掉
	                text = text.substring(0, i - 1);
	                if (text.length() == 0) {
	                    // 如果文本沒有了內容,則初始化計算器的各種值
	                	ShowPanel.display2.setText("0");
	                	CalcultorPanel.firstDigit = true;
	                	CalcultorPanel.lastcommand = "=";
	                } else {
	                    // 顯示新的文本
	                	ShowPanel.display2.setText(text);
	                }
	            }
	        }	else if(string.equals("+/-")) {
	        	String a1=ShowPanel.display2.getText();
	        	ShowPanel.display1.setText("");
	        	if(a1.indexOf("-")==-1) {
	        		if(Double.parseDouble(a1)!=0)
	        		ShowPanel.display2.setText("-"+ShowPanel.display2.getText());
	        		}
	        	else {
	        		ZeroText(Double.parseDouble(a1)*-1);
	        		}
				CalcultorPanel.result=a1;
	        }else{
	        	CalcultorPanel.start=true;//新數字鍵的輸入
	        	if(CalcultorPanel.lastcommand.equals("=")) //做一次運算
		{   
			CalcultorPanel.result=ShowPanel.display2.getText();	//獲取操作數1
		}
		else {
			if (CalcultorPanel.lastcommand.equals("%")) {
	            // 百分號運算,除以100
				double a1=Double.parseDouble(CalcultorPanel.result);
				a1= a1 / 100;
				if (CalcultorPanel.result=="0.") //去除0.問題
					ShowPanel.display1.setText(0+"/100"+"=");
				else ShowPanel.display1.setText(CalcultorPanel.result+"/100=");
				ZeroText(a1);
				CalcultorPanel.result=a1+"";
	        }else if (CalcultorPanel.lastcommand.equals("√")) {
	            // 平方根運算
	        	double a1=Double.parseDouble(CalcultorPanel.result);
	            a1 = Math.sqrt(a1);
	            if(CalcultorPanel.result=="0.")//去除0.問題
	            	ShowPanel.display1.setText("√("+0+")"+"=");
	            else ShowPanel.display1.setText("√"+"("+CalcultorPanel.result+")=");
	            ZeroText(a1);
				CalcultorPanel.result=a1+"";
	        }else if(CalcultorPanel.lastcommand.equals("pow")) {
	        	double a1=Double.parseDouble(CalcultorPanel.result);
	        	double a2=Double.parseDouble(ShowPanel.display2.getText());
	        	double s=Math.pow(a1,a2);
	        	if (CalcultorPanel.result=="0.") {//去除0.問題
					if(ShowPanel.display2.getText()=="0.")
						ShowPanel.display1.setText("pow("+0+","+0+")"+"=");
					else ShowPanel.display1.setText("pow("+0+","+ShowPanel.display2.getText()+")"+"=");
				}else if(ShowPanel.display2.getText()=="0.") {
					ShowPanel.display1.setText("pow("+CalcultorPanel.result+","+0+")"+"=");
				}else 
	        	ShowPanel.display1.setText("pow"+"("+CalcultorPanel.result+","+ShowPanel.display2.getText()+")=");
	        	ZeroText(s);
				CalcultorPanel.result=a1+"";
	        }else if (CalcultorPanel.lastcommand.equals("1/x")) {
	            // 倒數運算
	        	double a1=Double.parseDouble(CalcultorPanel.result);
	            if (a1 == 0.0) {
	                // 操作不合法
	            	ShowPanel.display2.setText("零沒有倒數");
	            } else {
	            	a1 = 1 / a1;
	            	ShowPanel.display1.setText("1"+"/"+CalcultorPanel.result+"=");
	            	ZeroText(a1);
					CalcultorPanel.result=a1+"";
	            }
	        } 
	else if(CalcultorPanel.lastcommand.equals("+")) {
				double a1=Double.parseDouble(CalcultorPanel.result);
				double a2=Double.parseDouble(ShowPanel.display2.getText());
				double s=a1+a2;
				if (CalcultorPanel.result=="0.") {//去除0.問題
					if(ShowPanel.display2.getText()=="0.")
						ShowPanel.display1.setText(0+"+"+0+"=");
					else ShowPanel.display1.setText(0+"+"+ShowPanel.display2.getText()+"=");
				}else if(ShowPanel.display2.getText()=="0.") {
					ShowPanel.display1.setText(CalcultorPanel.result+"+"+0+"=");
				}else 
				ShowPanel.display1.setText(CalcultorPanel.result+"+"+ShowPanel.display2.getText()+"=");
				ZeroText(s);
				CalcultorPanel.result=s+"";				
			}
			else if(CalcultorPanel.lastcommand.equals("-")) {
				double a1=Double.parseDouble(CalcultorPanel.result);
				double a2=Double.parseDouble(ShowPanel.display2.getText());
				double s=a1-a2;
				if (CalcultorPanel.result=="0.") {//去除0.問題
					if(ShowPanel.display2.getText()=="0.")
						ShowPanel.display1.setText(0+"-"+0+"=");
					else ShowPanel.display1.setText(0+"-"+ShowPanel.display2.getText()+"=");
				}else if(ShowPanel.display2.getText()=="0.") {
					ShowPanel.display1.setText(CalcultorPanel.result+"-"+0+"=");
				}else 
				ShowPanel.display1.setText(CalcultorPanel.result+"-"+ShowPanel.display2.getText()+"=");
				ZeroText(s);
				CalcultorPanel.result=s+"";				
			}else if(CalcultorPanel.lastcommand.equals("*")) {
				double a1=Double.parseDouble(CalcultorPanel.result);
				double a2=Double.parseDouble(ShowPanel.display2.getText());
				double s=a1*a2;
				if (CalcultorPanel.result=="0.") {//去除0.問題
					if(ShowPanel.display2.getText()=="0.")
						ShowPanel.display1.setText(0+"*"+0+"=");
					else ShowPanel.display1.setText(0+"*"+ShowPanel.display2.getText()+"=");
				}else if(ShowPanel.display2.getText()=="0.") {
					ShowPanel.display1.setText(CalcultorPanel.result+"*"+0+"=");
				}else 
				ShowPanel.display1.setText(CalcultorPanel.result+"*"+ShowPanel.display2.getText()+"=");
				ZeroText(s);
				CalcultorPanel.result=s+"";				
			}else if (CalcultorPanel.lastcommand.equals("/")) {
	            // 除法運算
	            // 如果當前結果文本框中的值等於0
				double a1=Double.parseDouble(CalcultorPanel.result);
				double a2=Double.parseDouble(ShowPanel.display2.getText());
	            if (a2 == 0.0) {
	                // 操作不合法
	            	ShowPanel.display2.setText("除數不能爲零");
	            } else {
	            	double s=a1/a2;
	            	if(CalcultorPanel.result=="0.")//去除0.問題
	            		ShowPanel.display1.setText(0+"/"+ShowPanel.display2.getText()+"=");
	            	else ShowPanel.display1.setText(CalcultorPanel.result+"/"+ShowPanel.display2.getText()+"=");
					ZeroText(s);
					CalcultorPanel.result=s+"";		
	            }
	        } 
		} CalcultorPanel.lastcommand=e.getActionCommand();//獲取當前的運算符
		CalcultorPanel.firstDigit = true;
	}
}		
	public void ZeroText(double s) { 
		long t1;
		double t2;
		t1 = (long) s;
		t2 = s-t1;
		if (t2 == 0) {
			ShowPanel.display2.setText(String.valueOf(t1));
		} else {
			ShowPanel.display2.setText(String.valueOf(s));
		}			       
  }		
} 
  • 不要介意我把類寫在一起,因爲那時我爲什麼寫在一起,我現在也不知道了呀!
  • 有興趣的你可以研究研究,感謝您的觀看。
發佈了19 篇原創文章 · 獲贊 19 · 訪問量 5631
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章