• <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
            久久天天躁狠狠躁夜夜2020一 | 久久婷婷五月综合色高清| 欧美激情一区二区久久久| 亚洲精品无码久久久久去q | 亚洲国产成人久久综合一区77| 国产精品久久新婚兰兰| 久久久久成人精品无码中文字幕| 久久精品国产69国产精品亚洲| 久久久久亚洲精品中文字幕| 久久99热只有频精品8| 久久婷婷五月综合成人D啪| 老色鬼久久亚洲AV综合| 久久婷婷五月综合成人D啪| 久久精品www人人爽人人| 午夜精品久久久久久影视777| 久久精品无码专区免费青青| 亚洲精品国精品久久99热| 久久天堂电影网| 久久久久亚洲精品无码蜜桃| 久久久精品国产| 精品久久久久久国产三级| 99国产欧美精品久久久蜜芽| 2021最新久久久视精品爱| 国内精品久久久久久久涩爱 | 777米奇久久最新地址| 久久福利资源国产精品999| 国产亚洲成人久久| 伊人久久精品线影院| 国产精品99精品久久免费| 久久精品人人做人人爽97| 一本色道久久综合亚洲精品| 久久人做人爽一区二区三区| 思思久久99热免费精品6| 久久乐国产精品亚洲综合| 国产精品欧美久久久久无广告| 久久亚洲国产午夜精品理论片 | 无码日韩人妻精品久久蜜桃| 久久天天躁夜夜躁狠狠躁2022| 亚洲精品无码久久久久AV麻豆| 精品久久久久久无码国产| 国产精品熟女福利久久AV|