|
马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
先谈谈我对java的一些认识。我选择java,是因为他语法简单,功能强大,从web,到桌面,到嵌入式,无所不能。但当我进一步了解了java后,感叹,java原来也有许多缺点。
importjava.awt.*;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjava.awt.event.WindowAdapter;importjava.awt.event.WindowEvent;
importjavax.swing.JTextField;importjava.awt.Color;
publicclasscreateFrame{Framef;JTextFieldtext,text2;Buttonb1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,b14,b15,b16,b17,b18,b19,b20,b21,b22,b23,b24,b25,b26,b27;doubleop1,op2,op2x,op3;//运算的三个数:op1,op2是运算数,op3是运算了局charop=0,opx=0,opp=0;//运算操纵符booleanb=false;//我按过0键了,下次该按.了booleanc=false;//按过运算符了,下次按数字时该清屏显现booleanfirstdeng=true;//第一次做=
doublemjia=0;
publicstaticvoidmain(String[]args){createFramecf=newcreateFrame();cf.createWindow();}publicvoidcreateWindow(){f=newFrame("calculator");f.setBackground(Color.LIGHT_GRAY);//增添封闭事务的监听器this.shutwindow(f);
text=newJTextField();text.setHorizontalAlignment(JTextField.RIGHT);text.setSize(250,22);text.setLocation(5,25);text.setEditable(false);text.setBackground(Color.WHITE);text.setText("0.");text2=newJTextField();text2.setHorizontalAlignment(JTextField.CENTER);text2.setSize(37,22);text2.setLocation(5,50);text2.setEditable(false);text2.setText("");text2.setBackground(Color.LIGHT_GRAY);b1=newButton("BackSpace");b1.setSize(70,22);b1.setLocation(52,50);b2=newButton("CE");b2.setSize(60,22);b2.setLocation(128,50);b3=newButton("C");b3.setSize(60,22);b3.setLocation(194,50);b4=newButton("MC");b4.setSize(37,22);b4.setLocation(5,75);b5=newButton("7");b5.setSize(37,22);b5.setLocation(50,75);b6=newButton("8");b6.setSize(37,22);b6.setLocation(93,75);b7=newButton("9");b7.setSize(37,22);b7.setLocation(136,75);b8=newButton("/");b8.setSize(37,22);b8.setLocation(179,75);b9=newButton("sqrt");b9.setSize(37,22);b9.setLocation(218,75);b10=newButton("MR");b10.setSize(37,22);b10.setLocation(5,100);b11=newButton("4");b11.setSize(37,22);b11.setLocation(50,100);b12=newButton("5");b12.setSize(37,22);b12.setLocation(93,100);b13=newButton("6");b13.setSize(37,22);b13.setLocation(136,100);b14=newButton("*");b14.setSize(37,22);b14.setLocation(179,100);b15=newButton("%");b15.setSize(37,22);b15.setLocation(218,100);b16=newButton("MS");b16.setSize(37,22);b16.setLocation(5,125);b17=newButton("1");b17.setSize(37,22);b17.setLocation(50,125);b18=newButton("2");b18.setSize(37,22);b18.setLocation(93,125);b19=newButton("3");b19.setSize(37,22);b19.setLocation(136,125);b20=newButton("-");b20.setSize(37,22);b20.setLocation(179,125);b21=newButton("1/x");b21.setSize(37,22);b21.setLocation(218,125);b22=newButton("M+");b22.setSize(37,22);b22.setLocation(5,150);b23=newButton("0");b23.setSize(37,22);b23.setLocation(50,150);b24=newButton("+/-");b24.setSize(37,22);b24.setLocation(93,150);b25=newButton(".");b25.setSize(37,22);b25.setLocation(136,150);b26=newButton("+");b26.setSize(37,22);b26.setLocation(179,150);b27=newButton("=");b27.setSize(37,22);b27.setLocation(218,150);f.setLayout(null);
f.add(text);f.add(text2);b1.addActionListener(newButtonHandler());b2.addActionListener(newButtonHandler());b3.addActionListener(newButtonHandler());b4.addActionListener(newButtonHandler());b5.addActionListener(newButtonHandler());b6.addActionListener(newButtonHandler());b7.addActionListener(newButtonHandler());b8.addActionListener(newButtonHandler());b9.addActionListener(newButtonHandler());b10.addActionListener(newButtonHandler());b11.addActionListener(newButtonHandler());b12.addActionListener(newButtonHandler());b13.addActionListener(newButtonHandler());b14.addActionListener(newButtonHandler());b15.addActionListener(newButtonHandler());b16.addActionListener(newButtonHandler());b17.addActionListener(newButtonHandler());b18.addActionListener(newButtonHandler());b19.addActionListener(newButtonHandler());b20.addActionListener(newButtonHandler());b21.addActionListener(newButtonHandler());b22.addActionListener(newButtonHandler());b23.addActionListener(newButtonHandler());b24.addActionListener(newButtonHandler());b25.addActionListener(newButtonHandler());b26.addActionListener(newButtonHandler());b27.addActionListener(newButtonHandler());
f.add(b1);f.add(b2);f.add(b3);f.add(b4);f.add(b5);f.add(b6);f.add(b7);f.add(b8);f.add(b9);f.add(b10);f.add(b11);f.add(b12);f.add(b13);f.add(b14);f.add(b15);f.add(b16);f.add(b17);f.add(b18);f.add(b19);f.add(b20);f.add(b21);f.add(b22);f.add(b23);f.add(b24);f.add(b25);f.add(b26);f.add(b27);f.setResizable(false);f.setSize(260,180);//窗口居中显现this.centershow(260,180,f);f.setVisible(true);}publicvoidcentershow(intx,inty,Framef){//窗口居中显现Toolkittk=Toolkit.getDefaultToolkit();Dimensiond=tk.getScreenSize();intscreenHeight=d.height;intscreenWidth=d.width;f.setLocation((screenWidth-x)/2,(screenHeight-y)/2);}publicvoidshutwindow(Framef){//Addalistenerforthecloseeventf.addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEventevt){//加入程序System.exit(0);}});}publicdoublegetCurrentNum()//失掉以后文本框内数字{return1;}//四则运算publicdoublecalculateNow(doubleop1,charop,doubleop2){doubleop1b=0.0;switch(op){case+:op1b=op1+op2;break;case-:op1b=op1-op2;break;case*:op1b=op1*op2;break;case/:if(op2==0.0)op1b=op1;elseop1b=op1/op2;break;case%:if(op2==0.0)op1b=op1;elseop1b=op1%op2;break;case0:op1b=op2;break;}returnop1b;}classButtonHandlerimplementsActionListener{publicvoidactionPerformed(ActionEvente){System.out.println("Buttonslableis:"+e.getActionCommand()+"||"+createFrame.this.op1+createFrame.this.op+createFrame.this.op2+"="+createFrame.this.op3);Stringcommand=e.getActionCommand();StringcurrentText=text.getText();if(currentText.equals("函数输出有效.")||currentText.equals("除数不克不及为零.")){currentText="0.";text.setText(currentText);}if(command=="BackSpace"){if(currentText.length()>1)currentText=currentText.substring(0,currentText.length()-1);elseif(currentText.length()<=1){currentText="0.";}text.setText(currentText);}if(command=="CE"){text.setText("0.");op2=0.0;//return;}if(command=="C"){text.setText("0.");op3=0.0;op1=0.0;op2=0.0;op2x=0.0;op=0;opx=0;opp=0;firstdeng=true;b=false;c=false;}if(command=="0"){if(text.getText().equals("0.")){text.setText("0.");b=true;}elsetext.setText(currentText+"0");}if(command=="1"){if(c){c=false;text.setText("1");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("1");elsetext.setText(currentText+"1");}if(command=="2"){if(c){text.setText("2");c=false;return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("2");elsetext.setText(currentText+"2");//return;}if(command=="3"){if(c){c=false;text.setText("3");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("3");elsetext.setText(currentText+"3");//return;}if(command=="4"){if(c){c=false;text.setText("4");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("4");elsetext.setText(currentText+"4");//return;}if(command=="5"){if(c){c=false;text.setText("5");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("5");elsetext.setText(currentText+"5");//return;}if(command=="6"){if(c){c=false;text.setText("6");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("6");elsetext.setText(currentText+"6");//return;}if(command=="7"){if(c){c=false;text.setText("7");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("7");elsetext.setText(currentText+"7");//return;}if(command=="8"){if(c){c=false;text.setText("8");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("8");elsetext.setText(currentText+"8");//return;}if(command=="9"){if(c){c=false;text.setText("9");return;}if((text.getText().equals("0.")||text.getText().equals("0"))&!b)text.setText("9");elsetext.setText(currentText+"9");}if(command=="/"){op2=Double.valueOf(text.getText()).doubleValue();if(opp!==){op1=calculateNow(op1,op,op2);opp=/;}op=/;opx=/;text.setText(String.valueOf(op1));c=true;//b=false;firstdeng=true;
}if(command=="*"){op2=Double.valueOf(text.getText()).doubleValue();if(opp!==){op1=calculateNow(op1,op,op2);opp=*;}op=*;opx=*;text.setText(String.valueOf(op1));c=true;firstdeng=true;}if(command=="-"){op2=Double.valueOf(text.getText()).doubleValue();if(opp!==){op1=calculateNow(op1,op,op2);opp=-;}op=-;opx=-;text.setText(String.valueOf(op1));c=true;firstdeng=true;}if(command=="+"){op2=Double.valueOf(text.getText()).doubleValue();if(opp!==){op1=calculateNow(op1,op,op2);opp=+;}op=+;opx=+;text.setText(String.valueOf(op1));c=true;firstdeng=true;}if(command=="."){if(text.getText().indexOf(.)>1)return;elseif(text.getText().indexOf(.)==-1){text.setText(currentText+".");b=true;}elseif(text.getText().indexOf(.)==1)b=true;}
if(command=="+/-"){if(Double.valueOf(text.getText()).doubleValue()!=0){doubled=-Double.valueOf(text.getText()).doubleValue();text.setText(String.valueOf(d));op2=d;op2x=d;}}if(command=="%"){op2=Double.valueOf(text.getText()).doubleValue();if(op1!=0&op2!=0){doubletemp=op1*op2;temp=temp/100;text.setText(String.valueOf(temp));}}if(command=="sqrt"){doublein=Double.valueOf(text.getText()).doubleValue();if(in<0)text.setText("函数输出有效.");else{doublesd=Math.sqrt(in);text.setText(String.valueOf(sd));op2=sd;op2x=sd;}
}if(command=="1/x"){doublein=Double.valueOf(text.getText()).doubleValue();if(in==0)text.setText("除数不克不及为零.");else{doublesd=1/in;text.setText(String.valueOf(sd));op2=sd;op2x=sd;}}if(command=="="){op2=Double.valueOf(text.getText()).doubleValue();System.out.println("=========="+op2);if(firstdeng){op2x=op2;firstdeng=false;}op1=calculateNow(op1,op,op2x);opp==;text.setText(String.valueOf(op1));}if(command=="MC"){mjia=0;text2.setText("");}if(command=="MR"){text.setText(String.valueOf(mjia));}if(command=="MS"){mjia=Double.valueOf(text.getText()).doubleValue();text2.setText("M");}if(command=="M+"){mjia=mjia+Double.valueOf(text.getText()).doubleValue();}}
}}
再说说缺点:首先java功能强大的背后是其复杂性,就拿web来说,当今流行的框架有很多,什么struts,spring,jQuery等等,而这无疑增加了java的复杂性。 |
|