• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>

            infinity

              C++博客 :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理 ::
              36 隨筆 :: 0 文章 :: 25 評論 :: 0 Trackbacks
            今天沒事,拿java做了一個簡單的計算器,帶時間顯示。以此用了用swing和awt的一些組件。初次寫
            這種東西寫起來還真費時間,還真是跟c++不一樣。不過還是很有趣的...還有好多東西沒有完善,以后
            再弄。代碼貼出來供大家bs....或許用java弄個掃雷也很有趣...


            import java.awt.*;
            import java.awt.event.*;
            import java.lang.*;
            import javax.swing.*;
            import java.util.*;
            import java.math.*;
            public class Cal{
                
            static Frame frm=new Frame("Calculator");//frame
                static JTextField txt=new JTextField("0");//text
                static JTextField time=new JTextField("00:00:00");//time
                
            //static Date hour=new Date(),min=new Date(),sec=new Date();
                static Button btn[]=new Button[30];//button
                static Dialog dlg=new Dialog(frm);//dlg
                static Thread thd=new Thread();
                
            static String btn_name[]={"MC","7","8","9","/","sqrt","MR","4","5","6","*","%",
                                          
            "MS","1","2","3","-","1/x","M+","0","+/-",".","+","=",
                                          
            "Backspace","CE","C","close","cancel"};//button_name Array_size=29
                static String op="";
                
            static double ans=0,x=0,y=0;//ans
                static StringBuffer str=new StringBuffer("");//Str_buffer to read number
                static Panel pn1=new Panel();//panel
                static Panel pn2=new Panel();

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

            posted on 2008-12-01 01:03 infinity 閱讀(699) 評論(0)  編輯 收藏 引用 所屬分類: java
            777久久精品一区二区三区无码 | 狠狠人妻久久久久久综合| 波多野结衣中文字幕久久| 久久99国产精品久久99果冻传媒| 精品久久综合1区2区3区激情| 四虎影视久久久免费| 久久婷婷五月综合97色一本一本| 久久青青草原国产精品免费| 久久久久久久久久免免费精品| 漂亮人妻被黑人久久精品| 99热热久久这里只有精品68| 久久久亚洲裙底偷窥综合| 久久99毛片免费观看不卡| 国产精品久久久久久久久久影院| 久久夜色精品国产亚洲| 久久99这里只有精品国产| 88久久精品无码一区二区毛片| 久久久久人妻一区二区三区| 久久天天躁狠狠躁夜夜av浪潮 | 伊人久久大香线蕉AV一区二区| 97精品久久天干天天天按摩| 久久夜色精品国产亚洲av| 99久久久精品免费观看国产| 久久福利资源国产精品999| 久久久国产精华液| 国产精品成人无码久久久久久| 久久综合狠狠综合久久综合88| 久久精品国产亚洲AV久| 人妻少妇精品久久| 欧美粉嫩小泬久久久久久久| 国产亚州精品女人久久久久久 | 久久久亚洲欧洲日产国码是AV| 国产毛片久久久久久国产毛片| 91精品国产91久久久久福利| 欧美噜噜久久久XXX| 一本一本久久aa综合精品| 久久亚洲精品国产亚洲老地址| 久久成人永久免费播放| 久久久久九国产精品| 一本久久a久久精品综合香蕉| 午夜精品久久影院蜜桃|