欢迎来到天天文库
浏览记录
ID:38369997
大小:35.50 KB
页数:6页
时间:2019-06-11
《计算器的java代码》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、importjava.awt.BorderLayout;importjava.awt.Color;importjava.awt.FlowLayout;importjava.awt.Font;importjava.awt.GridLayout;importjava.awt.event.ActionEvent;importjava.awt.event.ActionListener;importjavax.swing.JButton;importjavax.swing.JFrame;importjavax.swing.JPanel;importjavax.swing.JTextField;pub
2、licclassCalculatorBimplementsActionListener{privateJFrameframe;privateJTextFieldfield;privateJButton[]allButtons;privateJButtonclearButton;//构造初始化成员变量publicCalculatorB(){frame=newJFrame("Calculatorv1.1");field=newJTextField(25);allButtons=newJButton[16];Stringstr="123+456-789*0.=/";for(inti=0;i<16
3、;i++){allButtons[i]=newJButton(str.substring(i,i+1));}clearButton=newJButton("CLEAR");//调用界面的初始化方法init();//调用设置文本框的字体和颜色方法setFontAndColor();addEventHandler();}publicvoidaddEventHandler(){for(inti=0;i4、}privateStringop=null;privateStringm=null;privateStringn=null;privatebooleancls=false;privatevoidsetResult(){if(field.getText().equals("")){cls=true;m=null;n=null;}elseif(m!=null){n=field.getText();if(op.equals("+")){field.setText(Double.parseDouble(m)+Double.parseDouble(n)+"");}elseif(op.equals("5、-")){field.setText(Double.parseDouble(m)-Double.parseDouble(n)+"");}elseif(op.equals("*")){field.setText(Double.parseDouble(m)*Double.parseDouble(n)+"");}elseif(op.equals("/")){field.setText(Double.parseDouble(m)/Double.parseDouble(n)+"");}cls=true;m=null;n=null;}}publicvoidactionPerformed(ActionE6、vente){Stringstr=e.getActionCommand();//得到被点的按钮名称if("0123456789.".indexOf(str)!=-1){if(cls){field.setText("");cls=false;}field.setText(field.getText()+str);}elseif("+-*/".indexOf(str)!=-1){if(field.getText().equals("")){op=str;}elseif(m==null){m=field.getText();op=str;field.setText("");}elseif(m!=7、null){setResult();m=field.getText();op=str;}}elseif(str.equals("=")){setResult();}elseif(str.equals("CLEAR")){field.setText("");m=null;n=null;}}publicvoidinit(){frame.setLayout(newBorderLayout());JPanelnorthPanne
4、}privateStringop=null;privateStringm=null;privateStringn=null;privatebooleancls=false;privatevoidsetResult(){if(field.getText().equals("")){cls=true;m=null;n=null;}elseif(m!=null){n=field.getText();if(op.equals("+")){field.setText(Double.parseDouble(m)+Double.parseDouble(n)+"");}elseif(op.equals("
5、-")){field.setText(Double.parseDouble(m)-Double.parseDouble(n)+"");}elseif(op.equals("*")){field.setText(Double.parseDouble(m)*Double.parseDouble(n)+"");}elseif(op.equals("/")){field.setText(Double.parseDouble(m)/Double.parseDouble(n)+"");}cls=true;m=null;n=null;}}publicvoidactionPerformed(ActionE
6、vente){Stringstr=e.getActionCommand();//得到被点的按钮名称if("0123456789.".indexOf(str)!=-1){if(cls){field.setText("");cls=false;}field.setText(field.getText()+str);}elseif("+-*/".indexOf(str)!=-1){if(field.getText().equals("")){op=str;}elseif(m==null){m=field.getText();op=str;field.setText("");}elseif(m!=
7、null){setResult();m=field.getText();op=str;}}elseif(str.equals("=")){setResult();}elseif(str.equals("CLEAR")){field.setText("");m=null;n=null;}}publicvoidinit(){frame.setLayout(newBorderLayout());JPanelnorthPanne
此文档下载收益归作者所有