java infinity 处理_java 简单计算器

importjava.awt.*;importjava.awt.event.*;importjava.lang.*;importjavax.swing.*;importjava.util.*;importjava.math.*;publicclassCal{staticFrame frm=newFrame("Calculator");//framestaticJTextField txt=newJTextField("0");//textstaticJTextField time=newJTextField("00:00:00");//time//static Date hour=new Date(),min=new Date(),sec=new Date();staticButton btn[]=newButton[30];//buttonstaticDialog dlg=newDialog(frm);//dlgstaticThread thd=newThread();staticString 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=29staticString op="";staticdoubleans=0,x=0,y=0;//ansstaticStringBuffer str=newStringBuffer("");//Str_buffer to read numberstaticPanel pn1=newPanel();//panelstaticPanel pn2=newPanel();publicCal(){//构造函数//framefrm.setLayout(null);

frm.setLocation(250,250);

frm.setSize(350,300);

frm.setBackground(Color.DARK_GRAY);

frm.setResizable(false);

frm.setVisible(true);

frm.addWindowListener(newWinLis());//texttxt.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&&buttonGridLayout grid1=newGridLayout(4,6);

GridLayout grid2=newGridLayout(1,3);

pn1.setLayout(grid1);

pn2.setLayout(grid2);

pn1.setBounds(20,120,300,150);

pn2.setBounds(20,80,300,30);for(inti=0;i<24;i++){

btn[i]=newButton(btn_name[i]);

btn[i].setForeground(Color.red);

btn[i].setBackground(Color.yellow);

btn[i].addActionListener(newActLis());

pn1.add(btn[i]);

}

frm.add(pn1);for(inti=24;i<27;i++){

btn[i]=newButton(btn_name[i]);

btn[i].setForeground(Color.red);

btn[i].setBackground(Color.blue);

btn[i].addActionListener(newActLis());

pn2.add(btn[i]);

}

frm.add(pn2);//dialog &btnbtn[27]=newButton(btn_name[27]);

btn[28]=newButton(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(newFlowLayout(FlowLayout.CENTER,5,30));

dlg.setBackground(Color.white);

dlg.add(btn[27]);

dlg.add(btn[28]);

}staticclassthreadextendsThread{//时间显示线程publicthread(){

thd=newThread(this);

thd.start();

}publicvoidrun(){while(true){

Integer a=newDate().getHours(),b=newDate().getMinutes(),c=newDate().getSeconds();

String aa=newString(""),bb=newString(""),cc=newString("");if(a<10) aa="0"+a.toString();elseaa=a.toString();if(b<10) bb="0"+b.toString();elsebb=b.toString();if(c<10) cc="0"+c.toString();elsecc=c.toString();

time.setText(aa+":"+bb+":"+cc);try{

thd.sleep(100);

}catch(InterruptedException ie){

System.out.println(""+ie);

}

}

}

}publicclassWinLisextendsWindowAdapter{//WinLispublicvoidwindowClosing(WindowEvent e1){//frm.dispose();//System.exit(0);dlg.setLocation(200,300);

dlg.show(true);

}

}staticclassActLisimplementsActionListener{//ActionListenerpublicvoidactionPerformed(ActionEvent e2){

Button bt=(Button)e2.getSource();if(bt==btn[27]){

frm.dispose();

System.exit(0);

}elseif(bt==btn[28]) dlg.hide();elseif(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());

}elseif(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+"elseif(bt==btn[4]) {//"/" 号op="/";

x=Double.parseDouble(txt.getText().trim());y=0;

str.setLength(0);

}elseif(bt==btn[5]){//"sqrt"号x=Double.parseDouble(txt.getText().trim());y=0;if(x<0) txt.setText("invalid");elsetxt.setText(""+Math.sqrt(x));

str.setLength(0);

}elseif(bt==btn[10]){//"*"号op="*";

x=Double.parseDouble(txt.getText().trim());y=0;

str.setLength(0);

}elseif(bt==btn[11]) {//"%"号x=Double.parseDouble(txt.getText().trim());y=0;

txt.setText(""+x/100);

str.setLength(0);

}elseif(bt==btn[16]){//"-"号op="-";

x=Double.parseDouble(txt.getText().trim());y=0;

str.setLength(0);

}elseif(bt==btn[17]){//"1/x"号x=Double.parseDouble(txt.getText().trim());y=0;if(x==0) txt.setText("invalid");elsetxt.setText(""+1/x);

str.setLength(0);

}elseif(bt==btn[22]){//"+"号op="+";

x=Double.parseDouble(txt.getText().trim());y=0;

str.setLength(0);

}elseif(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;

}

}elseif(bt==btn[23]){//"="号if(y==0&&op=="/") txt.setText("invalid");elsetxt.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());

}

}publicdoublecalculate(){if(op=="+")returnx+y;elseif(op=="-")returnx-y;elseif(op=="*")returnx*y;elseif(op=="")returny;else{//if(y==0) {txt.setText("invalid");returnx/y;

}

}

}publicstaticvoidmain(String args[]){

Cal calculator=newCal();

thread new_thd=newthread();

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值