java簡單模仿win10計算器
更新時間:2020年02月22日 13:02:48 作者:waking-Java
這篇文章主要為大家詳細介紹了java簡單模仿win10計算器de,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了Java實現(xiàn)win10計算器的具體代碼,供大家參考,具體內容如下
這個小demo是我上學時的遠古代碼(嘻嘻嘻),今天整理代碼時看到的,看著以前的代碼,突然感覺這些是啥?看不懂了都,而且寫得也不規(guī)范。
運行一下,還是可以的,先截張圖
試了一下,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() {//構造函數(shù) 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;//新數(shù)字鍵輸入的開始 public static String result="";//保存中間結果的參數(shù) public static String lastcommand="=";//記錄上一個運算符 public static boolean firstDigit = true;//輸入第一個數(shù) 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{//數(shù)字鍵按鈕事件 @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) { // 輸入的第一個數(shù)字 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)) { // 輸入的是小數(shù)點,并且之前沒有小數(shù)點,則將小數(shù)點附在結果文本框的后面 ShowPanel.display2.setText(ShowPanel.display2.getText() + "."); } else //if(Double.parseDouble(ShowPanel.display2.getText())!=0) if (!e.getActionCommand().equals(".")) { // 如果輸入的不是小數(shù)點,則將數(shù)字附在結果文本框的后面 ShowPanel.display2.setText(ShowPanel.display2.getText() + e.getActionCommand()); } // 以后輸入的肯定不是第一個數(shù)字了 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;//新數(shù)字鍵輸入的開始 CalcultorPanel.result="";//保存中間結果的參數(shù) CalcultorPanel.lastcommand="=";//記錄上一個運算符 CalcultorPanel.firstDigit = true;//輸入第一個數(shù) 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;//新數(shù)字鍵的輸入 if(CalcultorPanel.lastcommand.equals("=")) //做一次運算 { CalcultorPanel.result=ShowPanel.display2.getText(); //獲取操作數(shù)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")) { // 倒數(shù)運算 double a1=Double.parseDouble(CalcultorPanel.result); if (a1 == 0.0) { // 操作不合法 ShowPanel.display2.setText("零沒有倒數(shù)"); } 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("除數(shù)不能為零"); } 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)); } } }
- 不要介意我把類寫在一起,因為那時我為什么寫在一起,我現(xiàn)在也不知道了呀!
- 有興趣的你可以研究研究,感謝您的觀看。
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
CompletableFuture并行處理List分批數(shù)據(jù)demo
這篇文章主要介紹了CompletableFuture并行處理List分批數(shù)據(jù)實現(xiàn)實例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-11-11java.math包下計算浮點數(shù)和整數(shù)的類的實例
這篇文章主要介紹了java.math包下計算浮點數(shù)和整數(shù)的類的實例代碼,本文通過使用BigDecimal進行浮點數(shù)比較給大家介紹的非常詳細,具有一定的參考借鑒價值,需要的朋友可以參考下2020-02-02使用webservice自定義注解處理參數(shù)加解密問題
這篇文章主要介紹了使用webservice自定義注解處理參數(shù)加解密問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-12-12