Java實現一個簡單計算器
先來看看界面效果:
源碼如下:
package test1; import java.awt.Frame; import java.awt.TextField; import java.awt.Button; import java.awt.Panel; import java.awt.GridLayout; import java.awt.event.WindowAdapter; import java.awt.event.WindowEvent; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class calculate { TextField tfResult; Button btNum[],btOper[],btPoint,btEqual,btBack,btSqrt,btPingfang,btLifang; int num1,num2;//操作數 char oper;//操作符 boolean isNum;//代表數字標志位 public void init(){ Frame f; tfResult=new TextField(); btNum=new Button[10];//數組對象 for(int i=0;i<=9;i++){ btNum[i]=new Button(i+""); } btOper=new Button[4]; btOper[0]=new Button("+"); btOper[1]=new Button("-"); btOper[2]=new Button("*"); btOper[3]=new Button("/"); btPoint=new Button("."); btEqual=new Button("="); btBack=new Button("Backspace"); btSqrt=new Button("Sqrt"); btPingfang=new Button("2"); btLifang=new Button("3"); f=new Frame("小羊羔的計算器!"); f.setSize(300,300); //放置組件 f.add(tfResult,"North"); Panel panel=new Panel(); f.add(panel,"Center"); GridLayout gl=new GridLayout(5,4); panel.setLayout(gl); panel.add(btNum[1]); panel.add(btNum[2]); panel.add(btNum[3]); panel.add(btOper[0]); panel.add(btNum[4]); panel.add(btNum[5]); panel.add(btNum[6]); panel.add(btOper[1]); panel.add(btNum[7]); panel.add(btNum[8]); panel.add(btNum[9]); panel.add(btOper[2]); panel.add(btNum[0]); panel.add(btPoint); panel.add(btEqual); panel.add(btOper[3]); panel.add(btBack); panel.add(btSqrt); panel.add(btPingfang); panel.add(btLifang); f.setVisible(true); //註冊監聽 f.addWindowListener(new MyWinLis()); //數字按鈕註冊監聽 MyNumLis mnl=new MyNumLis(); for(int i=0;i<=9;i++) btNum[i].addActionListener(mnl); //操作符按鈕註冊監聽 MyOperLis mol=new MyOperLis(); for(int j=0;j<=3;j++) btOper[j].addActionListener(mol); //等號按鈕註冊監聽 MyEqualLis mel=new MyEqualLis(); btEqual.addActionListener(mel); //刪除按鈕註冊監聽 MyBackLis mbl=new MyBackLis(); btBack.addActionListener(mbl); //開根號按鈕註冊監聽 MySqrtLis msl=new MySqrtLis(); btSqrt.addActionListener(msl); //平方按鈕註冊監聽 MyPingfangLis mpl=new MyPingfangLis(); btPingfang.addActionListener(mpl); //百分號註冊監聽 MyLifangLis mll=new MyLifangLis(); btLifang.addActionListener(mll); } //定義監聽器類 class MyNumLis implements ActionListener{ public void actionPerformed(ActionEvent e){ String t=e.getActionCommand(); String s=tfResult.getText(); if(isNum==false) tfResult.setText(t); else tfResult.setText(s+t); isNum=true; } } class MySqrtLis implements ActionListener{ public void actionPerformed(ActionEvent e){ double t=Double.parseDouble(tfResult.getText()); tfResult.setText(Math.sqrt(t)+""); isNum=false; } } class MyPingfangLis implements ActionListener{ public void actionPerformed(ActionEvent e){ num1=Integer.parseInt(tfResult.getText()); tfResult.setText(num1*num1+""); } } class MyLifangLis implements ActionListener{ public void actionPerformed(ActionEvent e){ num1=Integer.parseInt(tfResult.getText()); tfResult.setText(num1*num1*num1+""); } } class MyOperLis implements ActionListener{ public void actionPerformed(ActionEvent e){ num1=Integer.parseInt(tfResult.getText());//轉化為INT oper=e.getActionCommand().charAt(0); isNum=false; } } class MyEqualLis implements ActionListener{ public void actionPerformed(ActionEvent e){ int result=0; isNum=false; num2=Integer.parseInt(tfResult.getText()); switch(oper){ case '+':result=num1+num2;break; case '-':result=num1-num2;break; case '*':result=num1*num2;break; case '/':result=num1/num2;break; } tfResult.setText(result+""); } } class MyBackLis implements ActionListener{ public void actionPerformed(ActionEvent e){ tfResult.setText(""); isNum=false; } } class MyWinLis extends WindowAdapter{ public void windowClosing(WindowEvent e){ e.getWindow().setVisible(false); e.getWindow().dispose(); System.exit(0); } } public static void main(String[] args) { // TODO Auto-generated method stub new calculate().init(); } }
11×5=?界面如下:
到此這篇關於Java實現一個簡單計算器的文章就介紹到這瞭,更多相關Java簡單計算器內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!