• <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 閱讀(711) 評論(0)  編輯 收藏 引用 所屬分類: java
            66精品综合久久久久久久| 久久亚洲AV成人出白浆无码国产| 色诱久久久久综合网ywww | 久久无码国产专区精品| 久久亚洲AV无码精品色午夜| 99久久精品国内| 亚洲欧美成人久久综合中文网| 久久午夜无码鲁丝片秋霞| 久久精品免费观看| 狠狠综合久久综合88亚洲 | 99久久综合狠狠综合久久| 怡红院日本一道日本久久 | 99久久精品免费看国产一区二区三区 | 色综合久久久久| 久久久久se色偷偷亚洲精品av| 国产一久久香蕉国产线看观看 | 青青热久久综合网伊人| 亚洲综合伊人久久大杳蕉| 久久高清一级毛片| 亚洲成av人片不卡无码久久| 国产精品久久久久久久久免费| 久久亚洲AV成人无码| 久久激情亚洲精品无码?V| 97精品国产91久久久久久| 亚洲人成精品久久久久| 久久久久久久女国产乱让韩| 久久久久亚洲AV综合波多野结衣| 99久久超碰中文字幕伊人| 人妻丰满AV无码久久不卡| 久久久久亚洲av成人网人人软件| 久久久久亚洲精品中文字幕| 伊人丁香狠狠色综合久久| 久久精品国产亚洲麻豆| 久久无码av三级| 久久精品国产亚洲Aⅴ蜜臀色欲| 一级做a爰片久久毛片16| 精品免费久久久久国产一区| 久久精品?ⅴ无码中文字幕| 久久99精品久久久久久野外| 久久er国产精品免费观看8| 久久久久99精品成人片三人毛片 |