还剩11页未读,继续阅读
本资源只提供10页预览,全部文档请下载后查看!喜欢就下载吧,查找使用更方便
文本内容:
实验四图形界面设计GUI一实验要求、掌握与的区别与联系;掌握常用图像组件的使用;掌握主要的布局1Awt SwingSwing管理器的使用方法;、了解事件处理机制;掌握常用图像组件的使用;掌握固定菜单和弹出式菜单的创2Swing建和使用;二实验内容、设计并实现一个类似于操作系统附件中自带的计算器的一款简单的计算器,耍1windows求界面美观,设计合理;带有常用的菜单并实现其功能;能完成加、减、乘、除等简单的计算,在下面写出其核心代码源代码1package com.cal;*;import javaawtoimport javaxoswingo*;import javaoawt evento*;;;import javaoutilo Stackimport java.awt GridLayoutclassCalextends DFrameimplements ActionListener{JTextField show=null;DPanel jpl,jp2;^Button c,jia,jian,cheng,chu,equ,point;//按钮清空力减,乘,除,等号,小数点IL;〃按钮数组^Button b[]=new JButton
[10]0-9//栈控制标记位int flag=l;〃结果标记位;int reflag=0public Cal〃创建组件jpl=new[Panel;jp2=new[Panel;show=new3TextField45;宋体,show.setFont newFont FontPLAIN,16;;jia=new[Button“+”1+2+3+4+5+6+7+8+9C易计算=
45.09789+3456,★123S0*/乘易计算器一口X1*2*3C789+456,★1233E0*/=
6.0C789+456,123*工.0*/清除国简易计器一□M X7896-222C789+456■★13S0*/国简易计算器一口乂IC■■■一789+456■★123S0*/小数国简易计器—□M X
0.2*2*5-0・5*2c789+456,★13s0*/简易计算器—□X=
1.0C789+16一,6■123*|©.一」;■*1三实验内容中遇到的问题、解决方法和体会遇到的问题如何转为类型
1.String double.栈的使用
2.符号放到最后出错
3.连续输入小数点出借
4.如何实现小数的运算
56.Exception inthread AWT-EventQueue-0”工java.lariR.St ringrdexOutOf BoundsException:String indexout ofrange:-1java lariRNumberFormatException解决方法;1o Inum=Doubleo parseDoubLeSnum查询开发文档解决.2Java.输出错误信息3输出错误信息4o5o char al=st charAtstolength-1;;char bl=st charAtst.length-21bl==o{showo setText;String.vatueO/Math.round Inum.不直接按等于号6体会这个实验是一个很综合的实验,需要用到事件监听,和栈涉及到很多的知识,自己也GUI,有很多不会的内容,做这个实验很考验自己的耐性,和信心,以及查阅信息的能力但最总的结果是好的,做出来很开心JJ;wjian=new JButton-”cheng=new3Button*;;chu=new^Button/“;equ=new^Button=equo setForegroundColor.RED;;equ.setBackground Color.GREE/Vn;point=new3Button b
[0]=new^Button0;;b
[1]=new DButton1;b
[2]=new JButton2;b
[3]=new^Button3;b
[4]=new^Button“4”;b
[5]=new^Button5b
[6]=new JButton6;b
[7]=new^Button7;;b
[8]=new JButton“8”W Jb
[9]=new JButton9;c=new^ButtonC;;Co setForegroundColor.REDCo setBackgroundColor.YELLOM;“黑体“,;〃添加组件Co setFontnewFont Font.PLAIN,15jplo addshow;jplo addc;//show.setEditable false;;jp2o addb
[7]jp
2.addb
[8];jp2o addb
[9];jp
2.add jia;jp2o addb
[4];;jp
2.addb
[5];jp
2.addb
[6];jp
2.addjianjp2o addb
[1];;jp2o addb
[2];jp
2.addb
[3]jp
2.add cheng;;jp
2.addequjp
2.addb
[0];jp2o addpoint;;;jp2o addchujp
2.setLayout newGridLayout4,4,3,3//注册监听;;show.addActionListener thisjia.addActionListenerthis;jian addActionListenerthis;cheng.addActionListener thisochu.addActionListener this;equo addActionListener this;;;;point.addActionListenerthis CoaddActionListenerthis for int i=0;i10i++{;b[i].addActionListenerthis//设置布局管理器;this.addjpl,BorderLayouto NORTH;thiSo addjp2//设置窗体属性简易计this.setTitle器”;this.setsize500,300;;thiSo setLocation500,200thiSo setDefaultCloseOperationJFrameEXIT_ON_CLOSE;o//显示窗体this.setVisible true;©Overridepublic voidactionPerformedActionEvent eHifreflag==1e.getSource!=equ show.setText;ife getSource==b
[0]{o;;show.setTextshoWo getText+0”flag=0reflag=0;}else ife.getSource==b
[1]{show.setText show getText+^1;flag=0;reflag=0;}else ife getSource==b
[2]{o;;;showo setText show getText+“2”flag=0reflag=0}else ife getSource==b
[3]{o JJ;show.setText show getText+3flag=0;;reflag=0}else ife getSource==b
[4]{o;show.setTextshow.getText+4”flag=0;;reflag=0}else ife.getSource==b
[5]{show.setText showgetText+5;;flag=0reflag=0;}else ife.getSource==b
[6]{showo setTextshow.getText+6;flag=0;reflag=0;}else ife getSource==b
[7]{o;showo setTextshowgetText+“7”flag=0;reflag=0;}else ifegetSource==b
[8]{o;show.setText showgetText+“8;flag=0reflag=0;}else ifegetSource==b
[9]{showo setTextshow.getText+^9;;;}flag=0reflag=0//操作符处理{else ifegetSource==jiao;;showo setText showgetText++”flag=l reflag=0;{}else ifegetSource==jiano;;show.setTextshowgetText+-flag=l reflag=0;{}else ife.getSource==cheng;;showo setTextshowgetText+“*”flag=l reflag=0;}else ife.getSource==chu{show.setTextshowgetText+/;;flag=l reflag=0;{//小数点只能按一次}else ife.getSource==point show.setTextshow.getText;;;;flag=l reflag=0else ife.getSource==c{showo setTextflag=l reflag=0;}〃计算结果并输出else{;String s=show.getText1ifSo charAt s length-1==/I|s charAts length-1,Js.charAts.length-l==-+||SocharAtsdengthC-!==-符号不能放最后”;showo setText,else ifSocharAts.length-1==.s charAts.length-
2.1show.setText pointsare boom;elseshow.setText=+SetSum s;reflag=l;flag=l;private doubleSetSumString st{;ifsto trim==return0〃实例化用来存放操作数和操作符的栈并清空;Stack ObjectSNum=new StackStackObject SOD-new Stack;SNumo removeAHElements;;S0p removeAHElementsoStringSnum=double lnum=0;charal=st.charAtst.length-1;char bl=st charAtst.length-2;;forinti=0i st.length;i++;char c=st charAti」ifc==*||c==Inum=Double parseDoubleSnum;0ifS0p emptyoSNum pushInum;oelseswitchchar SOppeek{case+;SNum push doubleSNum.pop+InumoS0p pop;obreak;case:一工SNum pushdoubleSNum popnum;o o;S0p popo;break case*SNum pushdouble SNum popInum;o0;SOp.pop break;case/;SNum.pushdouble SNum.pop/InumSOp.pop;break;〃将符号压入栈switch ccase:S0p push+;o;breakcase-:;S0p push-obreak;}二Srwm,1,else ifc==*I|c==V{;Inum=Double porseDoubteSnum0ifSOp.empty;SNurrio pushInumelse{J,{//如果是整数,则去掉后面的小数点和if al==0*bl==L0showo;setText StringovatueO/Math.round Inumswitchchar SOppeekcase;SNum pushdoubleSNum.pop*Inumo;SOp.pop;break case/SNum.pushdouble SNumpop^Inum;S0p pop;o;break defaultSNum.pushInum;break;//将符号压入符号栈;switch c{case*SOp push*;break0case/;S0p push7,obreak;Snum=;;}elseSnum=Snum+c};//将表达式最后一个数入栈SNum.push DoubleoparseDoubLeSnuv\double a,b;while!SOp.emptyO a=doubleSNumpop;b=doubleSNum.pop;switch charSOpopop{case:;SNum pushb+ao;breakcase;SNum push b-aobreak;case*:SNum.pushb*a;break;case:;SNum.pushb/abreak;}}return doubleSNum.pop;}public classCalculter{public staticvoid mainString[]args自动生成的方法存根//TODO;Cal cal=new Cal截图2I易计器-□简M X7*5+3C789+.4,
5..6|,12*=01•加㈤简易计算器一口乂=
38.0C789+;6一,6,123*.Q•1。
个人认证
优秀文档
获得点赞 0