还剩40页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
tf.setTextFloat.toStringy;enable=false;break;case3:y=x/Float.parseFloat this.tf.getText;tf.setTextFloat.toStringy;enable=false;break;public voidkeyPressedKeyEvent e{//TODO Auto-generated methodstubif e.getKeyChar==,+{x=Float.parseFloat tf.getText;code=0;this.tf.setText〃〃;if e.getKeyChar二二-{x=Float.parseFloat tf.getText;code=l;this.tf.setText〃〃;}if e.getKeyChar==,*{x=Float.parseFloat tf.getText;code=2;this.tf.setText〃〃;}if e.getKeyChar=/{x=Float.parseFloat tf.getText;code=3;this.tf.setText〃〃;if e.getKeyChar二二1||e.getKeyChar二二2||e.getKeyChar二二3||e.g-10-etKeyChar==,O|e.getKeyChar==,||e.getKeyChar==,5||e getKeyChar二二64・||e getKeyChar二二.’・|e.getKeyChar二二7||e.getKeyChar二二8,||e.getKeyChar=9{System,out.printin〃hai〃;ifenable{iffirst{System,out.printin〃hehe〃;str=Character toStringe.getKeyChar;tf.setTextstr;first=false;}else{str=Character toStringe.getKeyChar;tf.setText tf.getText+str;}}else{str=Character toStringe.getKeyChar;tf.setTextstr;enable=true;if e.getKeyCode==KeyEvent.VK_ENTER{switchcode{case0:y=x+Float.parseFloat this.tf.getText;tf.setText Float.toStringy;enable=false;break;case1:y=x-Float.parseFloat this.tf.getText;tf.setText Float.toStringy;enable=false;break;case2:y=x*Float parseFloat this.tf.getText;・tf.setTextFloat.toStringy;enable=false;break;case3:y=x/Float.parseFloat this.tf.getText;tf.setTextFloat.toStringy;enable=false;break;public voidkeyReleasedKeyEvent argO{//TODO Auto-generated methodstub public void keyTypedKeyEventargO{//TODO Auto-generated methodstubvfx vL*vlx vl*vlx xlxsix six six xlx xfx xlx vfx vjxvizXTX Xrs✓Tx XTX XjX xjxXjX✓Tx✓jx#TS XTXXIX XjX#TS XjXpackage上机;import java,applet.import java.awt.*;import java.awt.event.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.*;import javax.swing,event.*;public classcalculator extendsJAppletContainer contentPane;JButton btnOnew JButtonJButton btnlnew JButtonJButton btn2newJButton JButton btn3newJButton JButton btn4newJButton JButton btn5newJButton JButton btn6newJButton JButtonbtn7newJButton JButtonbtn8newJButton JButtonbtn9newJButton〃特殊按钮btnClear=new JButton;btnEqual=new〃清除JButtonJButton;btnPlus=new JButton;〃等于JButtonbtnMinus=new JButton;btnMultiply=〃+操作符〃JButtonnew JButton;btnDivide=new-操作符JButtonJButton;〃*操作符〃JButton除操作符JButtonJTextField texResult=new JTextFieldO;boolean flag=false;String operandl;String operand2;double result;String action;public voidinit{contentPane=getContentPane;-13-contentPane.setLayoutnull;setSizenew Dimension250,250;btnO.setBoundsnew Rectangle50,230,45,45;btnO.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btnO.setText〃0〃;btnO.addActionListenernew btnOthis;〃清除操作符btnClear.setBoundsnew Rectangle100,230,45,45;btnClear.setFontnew java.awt.FontTahoma”,Font.PLAIN,14;btnClear.setText〃C〃;btnClear.addActionListener newbtnClearthis;〃二操作符btnEqual.setBoundsnew Rectangle150,230,45,45;btnEqual.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btnEqual.setText〃=〃;btnEqual.addActionListenernew btnEqualthis;〃+操作符btnPlus.setBoundsnew Rectangle200,230,45,45;btnPlus.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btnPlus.setText〃+〃;btnPlus.addActionListenernew btnPlusthis;btnl.setBoundsnew Rectangle50,180,45,45;btnl.setFont newjava.awt.Font〃Tahoma〃,Font.PLAIN,14;btnl.setText〃1〃;btnl.addActionListenernew btnlthis;btn
2.setBoundsnew Rectangle100,180,45,45;btn
2.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btn
2.setText〃2〃;btn
2.addActionListenernew btn2this;btn
3.setBoundsnew Rectangle150,180,45,45;btn
3.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btn
3.setText〃3〃;btn
3.addActionListenernew btn3this;〃-操作符btnMinus.setBoundsnew Rectangle200,180,45,45;btnMinus.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,16;btnMinus.setText〃-〃;btnMinus.addActionListenernew btnMinusthis;btn
4.setBounds newRectangle50,130,45,45;btn4setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;・btn
4.setText〃4〃;btn
4.addActionListenernew btn4this;btn
5.setBounds newRectangle100,130,45,45;btn
5.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btn
5.setText z,5,z;btn
5.addActionListenernew btn5this;btn
6.setBoundsnew Rectangle150,130,45,45;btn
6.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btn
6.setText〃6〃;btn
6.addActionListenernew btn6this;//*操作符btnMultiply.setBoundsnew Rectangle200,130,45,45;btnMultiply.setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;btnMultiply.setText〃*〃;btnMultiply.addActionListenernew btnMultiplythis;btn
7.setBoundsnew Rectangle50,80,45,45;btn
7.setFont newjava.awt.Font〃Tahoma〃,Font.PLAIN,14;btn
7.setText〃7〃;btn
7.addActionListenernew btn7this;btn
8.setBounds newRectangle100,80,45,45;btn8setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;・btn
8.setText〃8〃;btn
8.addActionListenernew btn8this;btn
9.setBoundsnew Rectangle150,80,45,45;btn9setFontnew java.awt.Font〃Tahoma〃,Font.PLAIN,14;・btn
9.setText〃9;btn
9.addActionListenernew btn9this;〃除操作符btnDivide.setBoundsnew Rectangle200,80,45,45;btnDivide.setFontnew java.awt.FontTahoma”,Font.PLAIN,14;btnDivide.setText,z//z;btnDivide.addActionListenernew btnDividethis;texResult.setText〃〃;texResult.setBoundsnew Rectangle40,35,215,35;texResult.setFontnew java.awt.FontTahoma”,Font.PLAIN,14;texResult.setHorizontalAlignmentJTextField.RIGHT;contentPane.addbtnO;contentPane.addbtnl;contentPane.addbtn2;contentPane.addbtn3;contentPane.addbtn4;contentPane.addbtn5;contentPane.addbtn6;contentPane.add btn7;contentPane.addbtn8;contentPane.addbtn9;contentPane.addbtnClear;contentPane.addbtnEqual;contentPane.addbtnPlus;contentPane.addbtnMinus;contentPane.addbtnMultiply;contentPane.addbtnDivide;contentPane.addtexResult;〃的事件监听器public voidbtnO_actionPerformedActionEvent e{ifflag{texResult.setText btnO.getActionCommand;flag=false;else{texResult.setText texResult.getText+btnO.getActionCommand;class btnOimplements ActionListener{private calculator adaptee;btnO calculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnOactionPerformede;〃1的事件监听器public voidbtnl_actionPerformedActionEvent e{ifflag{texResult.setTextbtnl.getActionCommand;flag=false;else{texResult.setText texResult.getText+btnl.getActionCommand;class btnlimplements ActionListener{private calculator adaptee;btnl calculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnl_actionPerformede;〃2的事件监听器public voidbtn2_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
2.getActionCommand;flag=false;else{texResult.setText texResult.getText+btn
2.getActionCommand;class btn2implements ActionListener{private calculator adaptee;btn2calculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn2_actionPerformede;.〃3的事件监听器public voidbtn3_actionPerformedActionEvent e{ifflag{texResult.setText btn
3.getActionCommand;flag=false;else{texResult.setText texResult.getText+btn
3.getActionCommand;class btn3implements ActionListener{-19-private StringlastCommand;private booleanstart;public CalculatorPanelsetLayoutnew BorderLayout;result=0;lastCommand=//=/z;start=true;〃添加显示标签栏display=new JLabel〃0〃;add display,BorderLayout.NORTH;ActionListener insert=new InsertAction;ActionListener command=new CommandAction;〃在4x4网格中添加数字和运算符按钮panel=new JPanel;panel.setLayoutnew GridLayout4,4;addButton〃7〃,insert;addButton〃8〃,insert;addButton〃9〃,insert;addButton〃/〃,command;addButton〃4〃,insert;addButton〃5〃,insert;addButton〃6〃,insert;addButton〃*〃,command;addButton〃1〃,insert;addButton〃2〃,insert;addButton〃3〃,insert;addButton〃-〃,command;addButton〃0〃,insert;addButton〃.〃,insert;addButton〃=〃,command;addButton〃+〃,command;add panel,BorderLayout.CENTER;private calculator adaptee;btn3calculator adaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn3_actionPerformede;〃4的事件监听器public voidbtn4_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
4.getActionCommand;flag=false;else{texResult.setText texResult.getText+btn
4.getActionCommand;class btn4implements ActionListener{private calculator adaptee;btn4calculator adaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn4_actionPerformede;〃5的事件监听器public voidbtn5_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
5.getActionCommand;flag=false;}else{texResult.setTexttexResult.getText+btn
5.getActionCommand;class btn5implements ActionListener{private calculatoradaptee;btn5calculatoradaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn5_actionPerformede;〃6的事件监听器public voidbtn6_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
6.getActionCommand;flag=false;}else{texResult.setTexttexResult.getText+btn
6.getActionCommand;-21-class btn6implements ActionListenerprivate calculatoradaptee;btn6calculatoradaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn6_actionPerformede;〃7的事件监听器public voidbtn7_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
7.getActionCommand;flag=false;else{texResult.setTexttexResult.getText+btn
7.getActionCommand;class btn7implements ActionListenerprivate calculatoradaptee;btn7calculatoradaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{-22-adaptee.btn7_actionPerformed e;〃8的事件监听器public voidbtn8_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
8.getActionCommand;flag=false;else{texResult.setTexttexResult.getText+btn
8.getActionCommand;class btn8implements ActionListener{private calculatoradaptee;btn8calculatoradaptee{this,adaptee=adaptee;}public voidactionPerformedActionEvent e{adaptee.btn8_actionPerformede;〃9的事件监听器public voidbtn9_actionPerformedActionEvent e{ifflag{texResult.setTextbtn
9.getActionCommand;flag=false;else{texResult.setText texResult.getText+btn
9.getActionCommand;class btn9implements ActionListener{private calculatoradaptee;btn9calculatoradaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btn9_actionPerformede;〃清除操作public voidbtnClear_actionPerformedActionEvent e{texResult.setText〃〃;}class btnClearimplements ActionListener{private calculatoradaptee;btnClearcalculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnClear_actionPerformede;〃+操作事件监听器public voidbtnPlus_actionPerformedActionEvent e{operandl=texResult.getTextO;action=〃plus〃;flag=true;class btnPlusimplements ActionListener{private calculatoradaptee;btnPluscalculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnPlus_actionPerformede;〃-操作事件监听器public voidbtnMinus_actionPerformedActionEvent e{operandl=texResult.getTextO;..〃・〃action=minus;flag=true;class btnMinusimplements ActionListenerprivate calculatoradaptee;btnMinuscalculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnMinus_actionPerformede;〃*操作事件监听器public voidbtnMultiply_actionPerformedActionEvent e{operandl=texResult.getTextO;action=multiply”;flag=true;class btnMultiplyimplements ActionListener{privatecalculatoradaptee;btnMultiplycalculator adaptee{this.adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnMultiply_actionPerformed e;〃除操作事件监听器public voidbtnDivide_actionPerformedActionEvent e{operandl=texResult.getTextO;action=divide”;flag=true;class btnDivideimplements ActionListener{privatecalculatoradaptee;btnDividecalculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnDivide_actionPerformede;〃二操作事件监听器public voidbtnEqual_actionPerformedActionEvent e{double digiti;double digit2;operand2=texResult.getTextO;ifIflag{if action,equals〃plus〃{digiti=Double.parseDouble operandl;digit2=Double.parseDoubleoperand2;result=digiti+digit2;texResult.setText〃〃+result;flag=true;else ifaction,equalsminus{digiti=Double.parseDoubleoperandl;digit2=Double.parseDoubleoperand2;result=digiti-digit2;texResult.setText/z//+result;flag=true;}else ifaction,equalsmultiply{-27-digiti=Double.parseDoubleoperandl;digit2=Double.parseDoubleoperand2;result=digiti*digit2;texResult.setText/zz/+result;flag=true;else ifaction,equalsdivide{digiti=Double.parseDoubleoperandl;digit2=Double.parseDoubleoperand2;result=digiti/digit2;texResult.setText///z+result;flag=true;class btnEqualimplements ActionListener{privatecalculatoradaptee;btnEqualcalculator adaptee{this,adaptee=adaptee;public voidactionPerformedActionEvent e{adaptee.btnEqual_actionPerformede;xlx✓rs XTX XTX xTx XTX xTxxTx rxxTxXTXxjx xTxXTH XTX✓TS XJXXTX XTX XTX XTX XTX✓TX XTXXJX xjxXTX✓TXpackage上机;import java.awt.*;import java.awt.event.public classCounters extendsFrame-28-Panel panel;TextField tf;Buttonb[],add,sub,mul,div,cle,equ,dot,two,sqr,tsex;Boolean isDot,operated,equaled;double storedNumber;char lastOperator;public Counters{//isDot=false;operated=false;equaled=false;storedNumber=0;lastOperator=,’;panel=new Panel;tf二new TextField;b=new Button
[10];forint i=0;i10;i++{b[i]=new Button〃〃+i;}add=new Button〃+〃;sub=new Button〃一〃;mul=new Button〃*〃;div=new Button〃/〃;cle=new Button〃Cle〃;equ=new Button〃二〃;//dot=new Button〃.〃;//two二new Button〃N2〃;//sqr=new Button/z J〃;//tsex=new Button〃N16〃;this,add tf,BorderLayout.NORTH;this,addpanel,BorderLayout.CENTER;panel.setLayoutnew GridLayout4,5,10,10;panel,addb
[1];private voidaddButtonString label,ActionListener listener{JButton button=new JButtonlabel;button.addActionListenerlistener;panel,addbutton;}private classInsertAction implements ActionListener{public voidactionPerformedActionEvent event{String input=event.getActionCommand;if startdisplay.setText〃〃;start=false;display.setText display.getText+input;}}private classCommandAction implements ActionListener{public voidactionPerformedActionEvent evt{String command=evt.getActionCommand;if startlastCommand=command;elsecalculate Double.parseDoubledisplay.getText;lastCommand=command;start=true;panel,add b
[2];panel.addb
[3];panel,add add;panel,addb
[4];panel,addb
[5];panel,addb
[6];panel,addsub;panel,addb
[7];panel.addb
[8];panel,add b
[9];panel,addmul;panel,addb
[0];panel,adddot;panel,addsqr;panel,add div;panel,addtwo;panel,addtsex;panel,addcle;panel,addequ;this.setTitle〃计算器〃;this.setSize250,350;this.setVisible true;for inti=0;i10;i++{b[i].addActionListenernew Monitor;add.addActionListenernew Monitor;sub.addActionListenernew Monitor;mul.addActionListenernew Monitor;div.addActionListenernew Monitor;dot.addActionListenernew Monitor;sqr.addActionListenernew Monitor;equ.addActionListenernew Monitor;cle.addActionListenernew Monitor;this.addWindowListenernew WindowAdapter{public voidwindowClosingWindowEvent e{System,exit0;};two.addActionListenernew ActionListener{public voidactionPerformedActionEvent e{int temp=Integer,parselnttf.getText;tf.setText〃〃+Integer.toBinaryStringtemp;};tsex.addActionListenernew ActionListener{public voidactionPerformedActionEvent e{int temp=Integer.parselnttf.getText;tf.setText Z///+Integer.toHexString temp;};class Monitorimplements ActionListenerpublic voidactionPerformedActionEvent e{Buttonbtn=Buttone.getSource;ifbtn==cle{tf.setText〃0〃;isDot=false;storedNumber=0;lastOperator=,’;else if btn==equ{operate=;equaled=true;else if btn==add{operate+;equaled=false;else ifbtn==sub{operate,;equaled=false;else ifbtn==mul{operate*;equaled=false;else ifbtn==div{operate/;equaled=false;}else ifbtn==sqr{operates;operate=;equaled=true;else{if equaled storedNumber=0;forint i=0;i10;i++ifbtn==b[i]{if tf.getText.equals〃0〃tf.setText〃〃+i;else if!operatedtf.setText tf.getText+i;else{tf.setText〃〃+i;operated=false;ifbtn==dot!isDot{tf.setText tf.getText+〃.〃;isDot=true;public voidoperate charoperator{double currentNumber=Double.valueOf tf.getText.doubleValue;iflastOperator==storedNumber=currentNumber;else iflastOperator==+storedNumber+=currentNumber;else iflastOperator==,storedNumber-=currentNumber;else iflastOperator==*storedNumber*=currentNumber;else iflastOperator==/storedNumber/=currentNumber;else iflastOperator==sstoredNumber=Math,sqrt currentNumber;else iflastOperator===equaledstoredNumber=currentNumber;tf.setText〃〃+storedNumber;operated=true;lastOperator=operator;}public staticvoid mainStringargs[]{new Counters;xlz vtxsix sixsixsixsix slzsix vtxvizXjX✓Tx XIX#7%XiX XTX✓TX✓TX#*Tx-33-package上机;import java.awt.import java.awt.event.public classjisuanqi extendsWindowAdapter Panel pl=new Panel;Panel p2=new Panel;Panelp3=new Panel;private Button[]b=new Button
[10];private Button[]c=new Button
[5];〃「〃〃4〃,〃L〃6,〃2〃,private Stringss[]={〃7〃,〃8〃,〃9〃,5,〃3〃,〃0〃};TextField txt;・.n.・r~|「〃।〃〃〃〃.〃〃/〃〃〃〕private StringsssLJ=tstatic doublea;static Strings,str;〃定义变量创建对像public staticvoid mainStringargs[]{new jisuanqi.frame;public voidframe{Frame fm=new Frame〃简单计算器〃;for inti=0;i=9;i++{b[i]=new Buttonss[i];forint i=0;i=4;i++c[i]=new Buttonsss[i];for inti=0;i=9;i++{p
2.addb[i];forint i=0;i=4;i++p
3.addc[i];}〃创建按钮并添加到P2//b
[16].setBackgroundColor,yellow;txt=new TextField15;txt.setEditablefalse;for inti=0;i=9;i++{b[i].addActionListener newbuttonlistener;//添力口监听器forint i=0;i〈二4;i++{b[i].addActionListenernew buttonlistener;//b
[16].addActionListenernew close;fm.addWindowListenerthis;//fm.setBackgroundColor,red;pl.setLayoutnew BorderLayout;pl.add txt,North;p
2.setLayoutnew GridLayout3,3;//p
3.setLayoutnewBorderLayout;p
3.setLayoutnew GridLayout1,5;//p
3.add b
[16];fm.add pl,〃North〃;fm.add p2,“Center;fm.add p3,〃South〃;fm.pack;fm.setVisibletrue;〃都是些窗中设置添加相关组件和监听器public voidwindowClosingWindowEvent e{System,exit0;〃退出系统class buttonlistenerimplementsActionListener{〃编写监听器事件通过按键得出给果public voidactionPerformedActionEvente{Buttonbtn=Button e.getSource;ifbtn.getLabel==〃=〃{jisuan;str=String.valueOfa;txt.setTextstr;s=;〃〃}else ifbtn.getLabel==〃+〃jisuanO;txt.setText〃〃;s=+;〃,〃}else ifbtn.getLabel==jisuanO;〃—〃txt.setText〃〃;〃〃s=-;}else ifbtn.getLabel==jisuanO;〃/〃txt.setText〃〃;s=/;〃/〃}else ifbtn.getLabel==jisuanO;〃*〃{txt.setText〃〃;s=*;〃.〃}else{txt.setText txt.getText+btn.getLabel;public voidjisuanO{〃编写具体计算方法==〃+〃if sa+=Double.parseDoubletxt.getText;else ifs==〃-〃a-=Double.parseDoubletxt.getText;elseif s==〃*〃a*=Double.parseDoubletxt.getText;〃/〃else ifsa/=Double.parseDoubletxt.getText;elsea=Double.parseDouble txt.getText;-36-class closeimplementsActionListener{〃退出public voidactionPerformedActionEvente{System,exit0;h娱|}婉b嘛Cz潴皮8斯害€第?D溶聆熟.瞰-・〈v,;H邪腐捐藁\P1报僚箭〉\骷v iE看脑法J1掴班是弓臣iT喀b靛m菖怀相j德炭鱼障\;久被他z;[药鳗搀x|l X罅H驴菇[wV粢长#EfDf票格W〜查Ppublic voidcalculatedouble xif lastCommand.equals〃+〃result+=x;elseiflastCommand.equals〃-〃result-二x;elseiflastCommand.equals〃*〃result*=x;elseiflastCommand.equals〃/〃result/=x;elseiflastCommand.equals〃二〃result=x;display.setText〃〃+result;vl*xlxvfx vlx sixxt*vlx xlxxfx xjxvfxvlxxtz xt*vtx vjxxlxxfxvtx vtxXlX#*T^XTX XrsXrs XjXXIX XTX#*TX#*T^XIX XT^#TS XjX#*TX✓TXXjX XjXXjXXTX XTXXjX#TS XTS#*TXXTX#*r^#T^XTXXTXXjX#T*XjX XjXZTSXT%✓Tx ZTX#TXXT%XT%package上机;import java.awt.BorderLayout;import java.awt.Button;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.awt.event.KeyEvent;import java.awt.event.KeyListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;-4-import javax.swing.JFrame;import javax.swing.JOptionPane;import javax.swing.JPanel;import javax.swing.JTextField;import javax.swing.WindowConstants;public classCaculator extendsJFrame implementsActionListener,KeyListener{/****/private staticfinal longserialVersionUID=5204982079673572494L;private JTextFieldtf=new JTextField;private floatx=0;private floaty=0;private intcode=0;private booleanenable;private booleanfirst;private Stringstr=〃〃;public Caculator{Container ct=this.getContentPane;this.setDefaultCloseOperationWindowConstants.DO_NOTHING_ON_CLOSE;tf.setHorizontalAlignmentJTextField.RIGHT;//tf.setText〃0〃;enable=true;first=true;ct.addtf,BorderLayout.NORTH;JPanel panel=new JPanel;panel.setLayoutnew GridLayout4,4;this.addWindowListenernew WindowAdapter{public voidwindowClosingWindowEvente{ifJOptionPane.YES_OPTION==JOptionPane.showConfirmDialogCaculator.th is,〃确定要关闭程序吗?〃,〃提示,JOptionPane.OK_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE{e.getWindow.setVisible false;e.getWindow.dispose;System,exit0;};Buttonbtn=null;btn=new Button〃1〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃2〃;panel,addbtn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃3〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃+〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃4〃;panel,addbtn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃5〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃6〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃一〃;panel,addbtn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃7〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃8〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃9〃;panel,addbtn;btn.addActionListenerthis;btn.addKeyListenerthis;btn=new Button〃*〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃0〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃.〃;panel,addbtn;btn.addActionListenerthis;btn.addKeyListenerthis;btn=new Button〃/〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;btn=new Button〃=〃;panel,add btn;btn.addActionListener this;btn.addKeyListenerthis;this,add panel,BorderLayout.CENTER;/***@param args*/public staticvoid mainString[]args{//TODO Auto-generated methodstubCaculator mainframe=new Caculator;mainframe.setTitle z/testing Caculator;mainframe.setSize400,400;mainframe.setVisibletrue;}publicvoidactionPerformedActionEvente//TODO Auto-generated methodstub ife.getActionCommand==/z+zz{x=Float.parseFloat tf.getText;code=0;this.tf.setText〃〃;}if e.getActionCommand==z/-/z{x=Float.parseFloat tf.getText;code=l;this.tf.setText z,/z;}if e.getActionCommand二二〃*〃{x=Float.parseFloat tf.getText;code=2;this.tf.setText〃〃;if e.getActionCommand==////,{x=Float.parseFloat tf.getText;code=3;this.tf.setText/z//;}if e.getActionCommand!=〃+〃e.getActionCommand!二〃一〃e getActionCommand!=〃*〃e.getActionCommand!=〃/〃e getActionCommand・・!二〃二〃{ifenable{iffirst{System,out.printin〃haha〃;tf.setText e.getActionCommand;first=false;else tf.setText tf.getText+e.getActionCommand;}else{tf.setText e.getActionCommand;enable=true;if e.getActionCommand==/z=z/{switch code{case0:y=x+Float.parseFloat this.tf.getText;tf.setTextFloat.toStringy;enable=false;break;case1:y=x-Float.parseFloat this.tf.getText;tf.setTextFloat.toStringy;enable=false;break;case2:y=x*Float.parseFloatthis.tf.getText;-9-。
个人认证
优秀文档
获得点赞 0