Java編的簡單計算機

來源:互聯網
上載者:User

import java.awt.*;<br />import java.awt.event.*;<br />import java.lang.*;<br />import javax.swing.*;<br />import java.util.*;<br />import java.math.*;<br />public class Cal{<br /> static Frame frm=new Frame("Calculator");//frame<br /> static JTextField txt=new JTextField("0");//text<br /> static JTextField time=new JTextField("00:00:00");//time<br /> //static Date hour=new Date(),min=new Date(),sec=new Date();<br /> static Button btn[]=new Button[30];//button<br /> static Dialog dlg=new Dialog(frm);//dlg<br /> static Thread thd=new Thread();<br /> static String btn_name[]={"MC","7","8","9","/","sqrt","MR","4","5","6","*","%",<br /> "MS","1","2","3","-","1/x","M+","0","+/-",".","+","=",<br /> "Backspace","CE","C","close","cancel"};//button_name Array_size=29<br /> static String op="";<br /> static double ans=0,x=0,y=0;//ans<br /> static StringBuffer str=new StringBuffer("");//Str_buffer to read number<br /> static Panel pn1=new Panel();//panel<br /> static Panel pn2=new Panel();</p><p> public Cal(){//建構函式<br /> //frame<br /> frm.setLayout(null);<br /> frm.setLocation(250,250);<br /> frm.setSize(350,300);<br /> frm.setBackground(Color.DARK_GRAY);<br /> frm.setResizable(false);<br /> frm.setVisible(true);<br /> frm.addWindowListener(new WinLis());<br /> //text<br /> txt.setForeground(Color.yellow);<br /> txt.setBackground(Color.gray);<br /> txt.setBounds(90,40,230,30);//輸出顯示<br /> txt.setHorizontalAlignment(JTextField.RIGHT);<br /> txt.setEditable(false);<br /> frm.add(txt);<br /> time.setForeground(Color.yellow);<br /> time.setBackground(Color.gray);<br /> time.setBounds(20,40,60,30);//時間顯示<br /> time.setHorizontalAlignment(JTextField.CENTER);<br /> time.setEditable(false);<br /> frm.add(time);</p><p> //panel&&button<br /> GridLayout grid1=new GridLayout(4,6);<br /> GridLayout grid2=new GridLayout(1,3);<br /> pn1.setLayout(grid1);<br /> pn2.setLayout(grid2);<br /> pn1.setBounds(20,120,300,150);<br /> pn2.setBounds(20,80,300,30);<br /> for(int i=0;i<24;i++){<br /> btn[i]=new Button(btn_name[i]);<br /> btn[i].setForeground(Color.red);<br /> btn[i].setBackground(Color.yellow);<br /> btn[i].addActionListener(new ActLis());<br /> pn1.add(btn[i]);<br /> }<br /> frm.add(pn1);<br /> for(int i=24;i<27;i++){<br /> btn[i]=new Button(btn_name[i]);<br /> btn[i].setForeground(Color.red);<br /> btn[i].setBackground(Color.blue);<br /> btn[i].addActionListener(new ActLis());<br /> pn2.add(btn[i]);<br /> }<br /> frm.add(pn2);<br /> //dialog &btn<br /> btn[27]=new Button(btn_name[27]);<br /> btn[28]=new Button(btn_name[28]);<br /> btn[27].setBackground(Color.blue);<br /> btn[28].setBackground(Color.blue);<br /> btn[27].setForeground(Color.yellow);<br /> btn[28].setForeground(Color.yellow);<br /> dlg.setTitle("確定要關閉");<br /> dlg.setSize(200,150);<br /> dlg.setLayout(new FlowLayout(FlowLayout.CENTER,5,30));<br /> dlg.setBackground(Color.white);<br /> dlg.add(btn[27]);<br /> dlg.add(btn[28]);<br /> }<br /> static class thread extends Thread{//時間顯示線程<br /> public thread(){<br /> thd=new Thread(this);<br /> thd.start();<br /> }<br /> public void run(){<br /> while(true){<br /> Integer a=new Date().getHours(),b=new Date().getMinutes(),c=new Date().getSeconds();<br /> String aa=new String(""),bb=new String(""),cc=new String("");<br /> if(a<10) aa="0"+a.toString(); else aa=a.toString();<br /> if(b<10) bb="0"+b.toString(); else bb=b.toString();<br /> if(c<10) cc="0"+c.toString(); else cc=c.toString();<br /> time.setText(aa+":"+bb+":"+cc);<br /> try{<br /> thd.sleep(100);<br /> }<br /> catch(InterruptedException ie){<br /> System.out.println(""+ie);<br /> }<br /> }<br /> }<br /> }<br /> public class WinLis extends WindowAdapter{//WinLis<br /> public void windowClosing(WindowEvent e1){<br /> //frm.dispose();<br /> //System.exit(0);<br /> dlg.setLocation(200,300);<br /> dlg.show(true);<br /> }<br /> }<br /> static class ActLis implements ActionListener{ //ActionListener<br /> public void actionPerformed(ActionEvent e2){<br /> Button bt=(Button)e2.getSource();<br /> if(bt==btn[27]){<br /> frm.dispose();<br /> System.exit(0);<br /> }<br /> else if(bt==btn[28]) dlg.hide();<br /> else if(bt==btn[24]){//"Backspace"鍵<br /> if(!txt.getText().trim().equals("0")){<br /> if(str.length()!=1){<br /> txt.setText(str.delete(str.length()-1,str.length()).toString());<br /> }<br /> else{<br /> txt.setText("0");<br /> str.setLength(0);//str長度清零<br /> }<br /> }<br /> y=Double.parseDouble(txt.getText().trim());<br /> }<br /> else if(bt==btn[25] || bt==btn[26]){txt.setText("0");str.setLength(0);}// "CE"or"C"<br /> //else if(bt==btn[0]){}//btn[0]"MC" btn[6]"MR" btn[12]"MS" btn[18]"M+"<br /> else if(bt==btn[4]) {// "/" 號<br /> op="/";<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[5]){// "sqrt"號<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> if(x<0) txt.setText("invalid");<br /> else txt.setText(""+Math.sqrt(x));<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[10]){// "*"號<br /> op="*";<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[11]) {// "%"號<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> txt.setText(""+x/100);<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[16]){// "-"號<br /> op="-";<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[17]){// "1/x"號<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> if(x==0) txt.setText("invalid");<br /> else txt.setText(""+1/x);<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[22]){// "+"號<br /> op="+";<br /> x=Double.parseDouble(txt.getText().trim());y=0;<br /> str.setLength(0);<br /> }<br /> else if(bt==btn[21]){// "." 號<br /> if(txt.getText().trim().indexOf('.')==-1) txt.setText(str.append(".").toString());<br /> else{<br /> txt.setText("invalid");<br /> str.setLength(0);<br /> x=0;y=0;<br /> }<br /> }<br /> else if(bt==btn[23]){// "="號<br /> if(y==0 && op=="/") txt.setText("invalid");<br /> else txt.setText(""+calculate());<br /> str.setLength(0);<br /> x=Double.parseDouble(txt.getText().trim());y=0;op="";<br /> }<br /> else{//按下數字鍵<br /> txt.setText(str.append(e2.getActionCommand()).toString());<br /> y=Double.parseDouble(txt.getText().trim());<br /> }<br /> }<br /> public double calculate(){<br /> if(op=="+") return x+y;<br /> else if(op=="-") return x-y;<br /> else if(op=="*") return x*y;<br /> else if(op=="") return y;<br /> else{<br /> //if(y==0) {txt.setText("invalid");<br /> return x/y;<br /> }<br /> }<br /> }</p><p> public static void main(String args[]){<br /> Cal calculator=new Cal();<br /> thread new_thd=new thread();<br /> }<br />}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.