还剩16页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
计算器布局显示功能举例
①13+20=33
②-7*14二-98:先输入7,再单击-/+将其变成负数,然后依次单击义号、14及=
③7!二5040@sin30°二
0.5:先输入30,然后按下〃按钮转换成角度值,再按下sin即可显示结果
⑤sqrt4=2:利用鼠标点击4后再点击sqrt按键即可显示结果
⑥2*冗=
6.2832源程序如下import java.awt.BorderLayout;import java.awt.Color;import java.awt.Dimension;import java.awt.Font;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.text.DecimalFormat;import javax.swing.BorderFactory;import javax.swing.ButtonGroup;import javax.swing.JButton;else{tf.setText〃负数不能开平方根〃;/*log求常用对数*/else ifstr.equals/zlog/z{double i=Double.parseDouble{Xi.getText;if i0{tf.setText String.valueOf^i.format Math.Jogi;}else{tf.setText〃负数不能求对数;/*%求百分比*/else ifstr.equals〃%〃{tf.setText df.format Double.parseDouble^i.getText100;/*1/x求倒数*/else ifstr.equals〃l/x〃{if Double.parseDouble[Xi.getText==0{tf.setText〃除数不能为零〃;}else{tf.setTextdf.format1Double.parseDouble{Xf.getText;/*sin求正弦函数*/else ifstr.equals/zsinz/{double i=Double.parseDouble[Xi.getText;tf.setText String.valueOf^i.format Math,sini;/*cos求余弦函数*/else ifstr.equals/zcosz/{double i=Double.parseDouble^i.getText;tf.setText String.valueOf9f.format Math,cosi;/*tan求正切函数*/else ifstr.equals/ztanz,{double i=Double.parseDouble[Xi.getText;tf.setText String.valueOfdf.format Math,tani;/*n!求阶乘*/else ifstr.equals〃n!〃{double i=Double.parseDouble[Xi.getText;if i%2=0||i%2=l〃判断为整数放进行阶乘操作int j=inti;〃强制类型转换int result=l;forint k=l;k=j;k++result*=k;tf.setText String.valueOf{yes\i\.t;elsetf.setText〃无法进行阶乘〃;/*x「2求平方*/else ifstr.equals〃x2〃{double i=Double.parseDouble^i.getText;tf.setText String.valueOfBf.format i*i;/*x3求立方*/else ifstr.equals/zx3/z{double i=Double.parseDouble[Xf.getText;tf.setText String.valueOf^t.format i*i*i;/*〃角度转换*/*将角度值转换成弧度值,方便三角函数的计算else ifstr.equals,z〃{double i=Double.parseDouble[Xf.getText;tf.setText String.3/〃eOFi/180*Math Pf;・else{if flag{IfResult=false;ifIfResult{oper=str;}else{getResult Double.parseDouble[Xi.getText;oper=str;IfResult=true;*清除按钮的事件监听class Clearimplements ActionListener{public void actionPerformedActionEvent e{*用ActionEvent对象的getActionCommand方法*取得及引发事件对象相关的字符串String str=e.getActionCommand;ifstr=〃C〃{tf.setText〃0〃;IfResult=true;result=0;}else ifstr=二〃一/+〃{double i=0-Double.parseDouble[Xi.getText.trim;tf.setText df.format i;}else ifstr==Backspace{if Double.parseDouble[Xi.getText0{if tf.getText.length1{tf.setText tf.getText.substring0,tf.getText.length-1;〃使用退格删除最后一位字符}else{tf.setText〃0〃;IfResult=true;}else{if tf.getText.length2{tf.setText tf.getText.substring0,tf.getText.length-1;}else tf.setText0;IfResult=true;}else ifstr二二〃CE〃{tf.setText〃0〃;IfResult=true;*数字输入的事件监听class Numimplements ActionListener{public voidactionPerformedActionEvent e{String str=e.getActionCommandO;ifIfResult{tf.setText〃〃;IfResult=false;ifstr二二〃n〃tf.setText String.ra/〃eOFMath PT;・else ifstr=〃e〃tf.setText String.valueOf^ath.;else{tf.setText tf.getText,trim+str;if tf.getText.equals0〃{tf.setText〃0〃;IfResult=true;flag=true;*小数点的事件监听class Dotimplements ActionListener{public voidactionPerformedActionEvent e{IfResult=false;if tf.getText.trim.indexOf〃.“二二-1{tf.setText tf.getText+〃.〃;*main方法public staticvoid mainString[]args{new Calucator.setVisibletrue;import javax.swing.JCheckBoxMenuItem;import javax.swing.JFrame;import javax.swing.JMenu;import javax.swing.JMenuBar;import javax.swing.JMenuItem;import javax.swing.JPanel;import javax.swing.JRadioButtonMenuItem;import javax.swing.JTextField;public classCalucator extendsJFrame{private JTextFieldtf;private JPanelpanel1,pane12,panel3,panel4;private JMenuBarmyBar;private JMenumenul,menu2,menu3;private JMenuItemeditlteml,editltem2,helpl,help2,help3;private JRadioButtonMenuItemseelteml,seeltem2;〃单选框private JCheckBoxMenuItemseeltem3;//复选框private ButtonGroupbgb;private Stringback;private booleanIfResult=true,flag=false;private Stringoper=〃=〃;private doubleresult=0;private NumnumActionListener;private DecimalFormatdf;public Calucator{super〃科学计算器〃;〃设置标题栏df=new DecimalFormat〃#.####〃;〃保留四位小数this.setLayoutnew BorderLayout10,5;pane11=new JPanelnew GridLayout1,3,10,10;panel2=new JPanelnewGridLayout5,6,5,5;〃5行6列panel3=new JPanelnewGridLayout5,1,5,5;panel4=new JPanelnewBorderLayout5,5;*菜单栏myBar=new JMenuBar;menul=new JMenu〃编辑E〃;menu2=new JMenu〃查看V〃;menu3=new JMenu〃帮助H〃;menul.setFont newFont〃宋体〃,Font.PLAIN,12;menu
2.setFont newFont“宋体〃,Font.PLAIN,12;menu
3.setFont newFont〃宋体〃,Font.PLAIN,12;*编辑栏editlteml=new JMenuItem〃复制C Ctrl+C〃;editltem2=new JMenuItem〃粘贴P Ctrl+V〃;editlteml.setFont newFont〃宋体〃,Font.PLAIN,12;editltem
2.setFont newFont〃宋体〃,Font.PLAIN,12;*查看栏seelteml=new JRadioButtonMenuItem〃科学型T〃;seeltem2=new JRadioButtonMenuItem〃标准型S〃;seeltem3=new JCheckBoxMenuItem〃数字分组I〃;seelteml.setFont newFont〃宋体〃,Font.PLAIN,12;seeltem
2.setFont newFont〃宋体〃,Font.PLAIN,12;seeltem
3.setFont newFont〃宋体〃,Font.PLAIN,12;*帮助栏helpl=new JMenuItem〃帮助主题H〃;help2=new JMenuItem〃关于计算器A〃;helpl.setFont newFont〃宋体〃,Font.PLAIN,12;help
2.setFont newFont〃宋体〃,Font.PLAIN,12;bgb=new ButtonGroup;〃选项组menul.addeditlteml;menul.add editltem2;menu
2.addseelteml;menu
2.addseeltem2;menu
2.addSeparator;〃添加一条分割线menu
2.addseeltem3;menu
3.add helpl;menu
3.addSeparator;//添加一条分割线menu
3.add help2;myBar.addmenul;myBar.addmenu2;myBar.addmenu3;this.setJMenuBarmyBar;numActionListener二new Num;〃实现数字监听*文本域,即为计算器的屏幕显示区域tf二new JTextFieldO;tf.setEditable false;〃文本区域不可编辑tf.setBackgroundColor,white;〃文本区域的背景色tf.setHorizontalAlignment JTextField.RIGHT;//文字右对齐tf.setText〃0〃;tf.setBorder BorderFactory.createLoweredBevelBorderQ;init;〃对计算器进行初始化*初始化操作*添加按钮private voidinit{addButton panel1,“Backspace”,new Clear,Color,red;addButton panel1,〃CE〃,new Clear,Color,red;〃C〃,new Clear,Color,red;addButtonpanel1,〃l/x〃,new Signs,Color,magenta;addButtonpanel2,〃log〃,new Signs,Color,magenta;addButton panel2,〃7〃,numActionListener,Color,blue;addButtonpanel2,〃8〃,numActionListener,Color,blue;addButtonpanel2,〃9〃,numActionListener,Color,blue;addButtonpanel2,〃・〃,new Signs,Color,red;addButtonpanel2,〃n!〃,new Signs,Color,magenta;addButtonpanel2,〃sqrt〃,new Signs,Color,magenta;addButtonpanel2,〃4〃,numActionListener,Color,blue;addButtonpanel2,〃5〃,numActionListener,Color,blue;addButtonpanel2,〃6〃,numActionListener,Color,blue;addButtonpanel2,addButtonpanel2,〃X〃,new Signs,Color,red;addButtonpanel2,〃sin〃,new Signs,Color,magenta;〃x^2〃,new Signs,Color,magenta;addButtonpanel2,addButtonpanel2,〃1〃,numActionListener,Color,blue;addButtonpanel2,〃2〃,numActionListener,Color,blue;addButtonpanel2,〃3〃,numActionListener,Color,blue;addButtonpanel2,〃一〃,new Signs,Color,red;addButtonpanel2,〃cos〃,new Signs,Color,magenta;addButtonpanel2,〃x3〃,new Signs,Color,magenta;〃0〃,numActionListener,Color,blue;addButtonpanel2,〃一/+〃,new Clear,Color,blue;addButton panel2,〃〃,new Dot,Color,blue;・addButton panel2,〃+〃,new Signs,Color,red;addButtonpanel2,〃%〃,new Signs,Color,magenta;addButtonpanel2,〃兀numActionListener,Color,orange;addButtonpanel2,〃e〃,numActionListener,Color,orange;addButtonpanel2,〃〃,new Signs,Color,orange;newaddButtonpanel2,〃二〃,Signs,Color,red;addButton panel2,〃tan〃,new Signs,Color,magenta;JButton btns=new JButton〃计算器〃;btns.setBorder BorderFactory.createLoweredBevelBorderQbtns.setEnabled false;〃按钮不可操作btns.setPreferredSizenew Dimension20,20;panel
3.add btns;〃加入按钮addButtonpanel3,〃MC〃,null,Color,red;addButtonpanel3,〃MR〃,null,Color,red;addButtonpanel3,〃MS〃,null,Color,red;addButtonpanel3,〃M+〃,null,Color,red;panel
4.add panel1,BorderLayout.NORTH;panel
4.add panel2,BorderLayout.CENTER;this.addtf,BorderLayout.NORTH;this,add panel3,BorderLayout.WEST;this.addpanel4;pack;this.setResizablefalse;〃窗口不可改变大小this.setLocation300,200;this.setDefaultCloseOperation JFrame.EXIT_0N_CL0S£;*统一设置按钮的的使用方式*@param panel*@param name*@param action*@param colorprivatevoid addButtonJPanelpanel,String name,ActionListener action,Color color{JButton bt=new JButtonname;panel,add bt;〃在面板上增加按钮bt.setForeground color;〃设置前景字体颜色bt.addActionListener action;〃增加监听事件*计算器的基础操作+-X*@param xprivatevoid getResultdouble x{if oper==〃+〃{result+=x;}else ifoper==〃-〃{result-=x;}else ifoper==〃X〃{result*=x;}else ifoper==z/4-/z{result/=x;}else ifoper==〃=〃{result=x;}tf.setTextdf.formatresult;*运算符号的事件监听class Signsimplements ActionListenerpublicvoidactionPerformedActionEvente{*用ActionEvent对象的getActionCommand方法*取得及引发事件对象相关的字符串String str=e.get ActionCommandO;/*sqrt求平方根*/ifstr.equals〃sqrt〃{double i=Double.parseDouble[Xi.getText;ifi〉=0{*String.valueOf转换为字符串*df.format按要求保留四位小数*Math,sqrt求算数平方根tf.setText String.valueOf^i.format Math,sqrti;。