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 阅读(680) 评论(0)  编辑 收藏 引用 所属分类: java

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理