• <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++博客 :: 首頁 :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理 ::
              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(){//構造函數(shù)
                    
            //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{//按下數(shù)字鍵
                            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 閱讀(700) 評論(0)  編輯 收藏 引用 所屬分類: java
            中文字幕无码免费久久| 久久久久亚洲AV成人网人人网站| 久久线看观看精品香蕉国产| 天堂无码久久综合东京热| 国产成人久久精品二区三区| 久久精品国产亚洲麻豆| 乱亲女H秽乱长久久久| 久久综合亚洲色一区二区三区| 久久天天躁狠狠躁夜夜av浪潮| 久久婷婷五月综合97色直播| 久久影视综合亚洲| 狠狠色丁香婷婷久久综合五月| 狠狠色狠狠色综合久久| 亚洲国产精品高清久久久| 亚洲av日韩精品久久久久久a| 久久久久女人精品毛片| 久久综合狠狠综合久久激情 | 国产精品久久久久一区二区三区| 久久精品亚洲乱码伦伦中文| 一本久久综合亚洲鲁鲁五月天亚洲欧美一区二区 | 久久久久亚洲Av无码专| AAA级久久久精品无码片| 久久免费视频网站| 一本久久综合亚洲鲁鲁五月天| 麻豆亚洲AV永久无码精品久久| 久久综合综合久久97色| 亚洲成av人片不卡无码久久| 亚洲精品无码久久久久sm| 久久久综合九色合综国产| 亚洲精品国产综合久久一线| 国内精品久久久久影院日本| 久久亚洲精品无码观看不卡| 伊人久久大香线蕉亚洲| 精品久久综合1区2区3区激情| 久久亚洲AV成人无码软件| 亚洲精品国产成人99久久| 91麻豆国产精品91久久久| 国产精品成人久久久久久久 | 亚洲欧美日韩精品久久亚洲区 | 久久99热这里只有精品66| 青青草原综合久久大伊人精品|