《经典Java程序源代码.docx》由会员分享,可在线阅读,更多相关《经典Java程序源代码.docx(56页珍藏版)》请在课桌文档上搜索。
1、经典Java程序源代码1 .加法器(该java源文件的名称是Addenjava)importjava.awu*;importjava.awt.cvcnt.;importjavax.swing.;pub1.icC1.aXSAdderIrnp1.ernen1.sAc1.ion1.is1.enerIJFramCAddcrFramc;JTextFiekITOPrandkJ1.extFieIdTOpr三1.2:J1.abeI1.Add.1.Sum:.!ButtonBA(M,BCIcar;JPaneIJP1.JP2;pub1.icAddert)IA(WcrFran=ncwJFramc(,Ad(krFram
2、c,*);TOpGUHII:OeWJTex1.Fiek1.CQO);TOprand2=newJTcxtFickICQO):1.Add=11cwJ1.abc1.C+);1.Sum=ncwJ1.abc1(=);BAdd二IIeWJBuiton(wAddw);BCIcar=newJBuUonCCkarw);JPI=newJPanc1.();JP2=ncwJPand();BAdd.MdAciion1.iNenerUhisXBCIear.ddAc1.ion1.istener(newAction1.is1.enert)pub1.icvoidactinPerfor11d(ActionEven1.event)
3、TOpramII.SCt1.cxtC0.();TOrax1.2,set1.ext(nO.O):1.Sum.set1ex1.(M=M):I);A(MerFrame.add(JP1.);JP1.addiTOprand1.):JPI.ad1.Add):JPI.aid(TOprand2);JP1.add(1.Sum);AicrFran.add(JP2);JP2.a1.d(BAdd);JPZiKkkBCIear):AddcrFra11c.gctContcntPaneewBordcr1.ay(HitO):A(WcrFran.gctComcntPanc0.akKJP1.Brder1.-ayou1.NORTH
4、);AdderFjme.getCon(eHPane().ad;AddcrFramc.addWindow1.istcncrtnewWindowAdapcr()pub1.icvoidWiIKIoWC1.oSing(WifK1.oWEVen1.event)Systcm.cxit();II);AdderF11n.pack():A(MerFne.seVisib1.e(rue);ACk1.erFnin.se1.Resizab!e(fa1.se):AddcrFrame.setSizc:名称为NotepadJava)2 .小型记事本(该java源文件由两个类构成,importjava.aw(.*;import
5、java.awt.even1.*:importjavax.swing.;importjava.io.*;dnssInynotepadextendsJFrameFi1.ef1.1.c=nukCo1.orCo1.or=Cotormd:myntHepad()ini1.Tex1.Conten1.();initMcnu();initAboutDiaIogO;voidinitTcxtCntcnt()IgetCon(en(PaneO,i.ncwJMenu1.tcm(Sf1.JF*).ncwJMCnIJItCmr保存).ncWJMenU1.temr退出newJMCnUkemC及制),newJMenuhem(剪
6、切),newJMenUhemr粘贴)mcwJMeiwItemC强色.).newJMenukemC关于。);voidinitMcnu()for(inti=OJmenus.1.eng(hi+)nnu.add(nenusi)ir(in1.j=Oijfmenu(i.IengthJ+)(mcnusi).add(op(ionofnwnuij);op(ionofnuij1.addAc(ion1.is(e11ertaction):IIthis.scUMenuBar(mcnu);Action1.istcncraction=ncwAction1.istcncrO添加事件监听pub1.icvoidMtionPe向m
7、ed(Ae1.ionEVeme)Stringname=e.getActionCommand():JMenuItcmMI=(JMenuhem)c.gcSourcc();if(新建”.equa1.s(name)(if(fi1.c!=nu1.1.)openfik.setSe1.ec:if(rctumVa1.=JFi1.eChscr.APPROVEJ)F11ON)f1.1.e=cpenfi1.e.getSe1.ec(edFi1.e():unfo1.d(:e1.seif保存.equak(namc)if(1.e!=nu1.1.)pen1.!e.setSe1.ectedFi1.e(fi1.e):ntretu
8、rnVai=opcnfi!c.showSavcDia1.og(mynotcpad.this):if(rc1.urnVa1.=JFi1.cChooscr.APPROVE_OPTION)(fi1.e=openfi1.e.getSe1.ec(edFi1.e():saving();IIe1.seiff退出1.equaMnamc)mynotepadf=newmyno(epad();intS=JopdonpaneshowConfinnDid1.ogFmiH?M.jHHr.JOp1.ionPane.YES_NO_OPT1.ON);jf(s=jOptionPanc.YES)F1.ON)Systcm.Cxit(
9、O);Ie1.seif(w!7iJJM.equa1.s(name(contcnt.cut();Ie1.seiff复制.equa1.s(name)(con(cnt.coy();e1.sei粘贴”.equak(name)03BJmS=-s,d一QUo-2.3282280一x%o三3No-Xo1.Um.=o-uo3j三v三a-xu三tu三Av一MUUU三M。-=A。一工)-n一Os三WSP三二一-cMoqs.moXos-.Od-SPSTnOqK一UDEnUNIIntW.*+张三%F一XUO-8=2EOX-.三01UXJ三ou!,.2三-三“duouuoPMoq,.JMM三2o。70OOU)(GUi2
10、cngst建,=一OsO一NrpdcBuouvoidunfo1.d()try(FiIcRcaderRCadknCWFiIcRcadcrtfiIc);int1.en=(infi1.e.!engh(:charbuf1.*er=newcharIen;Readf.read(bu1.Ycr.():ReadfdOWXCo1.Nen1.Se1.TeX(newSring(buffer);Icatchckgnound(Co1.or.whi(c);about.getConte11(Pane().add(newJ1.abeK”我的记程序”)H对话框内?about.getCnten1.Pand).add(11ewJ1
11、.a1.1.(,JFwxm):about.gctContcntPane().add(newJ1.abc1.(2007年12月,):about.sctModa1.(1.11*c);设置对话框的湘显示about.setSize(1.00.100);about.sct1.ca(ion(250.170);设置对话框显示位置Icu1.atorJava)pub1.icc1.assNotepadIpub1.icsta1.kvoidmain(Siringargs)入口main函数mynmcpadnotcd=newmynotcpad();eJ.addWindow1.isienertnewWindoWAdaPIe
12、rOI);记出本标即设置记事本大小设置记事本显示位竹.ned.setSizd640.320);ncd.show():no=newJBuuoii(hOm);staticJButtonc=newJButton(,):static.!Buttontwo-cwJButton(,2,*);staticJBution(hree=newJBUHOn(3):staticJBuuonfour=newJBuUon(,4);MaticJButtonfivc=ncwJBuuon(5*)zstaticJBuuonsix=ncwJBu1.ton(TT);staticJBuuonseven=newJBUHOM7):stat
13、icJBu1.toneigh1.=newJBuUon(8,:staticJButtonninc=ncwMatic.!ButtonxkincwJBUHonstaticJBuuonWb=OeWJBUUOIW“);staticJBuUonmu1.=newJB1.1.uOnstaticJButtondiv=11cwJButton():staticJButtonQuFan=ncwJButton(w/-);staticJBuuonDian=newJBuuon(,.w);sia1.icJBuUonequa1.=ncwJBuUon(M=M);staticJButtonC1.ear=newJBUt1.on(P*
14、);staticJBut1.onBdiFCn=newJBimOn%”):staticJButtonFcnZhiY=ncwJBuuon(1.x,*);staticinti=0;staticDoub1.eaddNumber;staticDoub1.eSUbNUiIIbe匚staticDoub1.enwINumber:staticDoub1.edivNumber;staticDoub1.eequa1.Number;staticDoub1.e(ernp:staticJ1.cxtArcarcsu1.t=newJTcxtArca(UO):pub1.icstaticvoidnuin(S(ing)args)(
15、JEramcframe=ncwJHramc(i1.WK,*);resuh.sctEditab1.c(fa1.se);resu1.i.set1.extCM);ReSUkS3c=窗口空”:JPancIForRcsu1.t=11cwJPancK);JPaneIForButton7_c1.carncwJPanc1.();JPaneIForBut(on4_inu1.-newJPaneIO;JPane1.ForBu1.1.on1._sub=newJPcine1.():JPanc1.ForButtonO_cqua1.=ncwJPanc!();FIow1.ayoutF1.O=newF1.ow1.iiyouK)
16、;ForResu1.t.add(rvsu1.1.:ForButton7_cIcar.se1.1.4yout(F1.O);ForBui(on7_c1.ear.add(seven);ForBu(ton7-c1.ear.add(eigh1.:ForBu(on7_c1.ear.add(nine);ForBu(ton7-c1.ear.add(div):ForButton7_c!car.add(c1.car);ForBu(ton4_mu1.sei1.ayou(F1.O);ForBu1.1.on4_InU1.add(four):ForButton4_mu1.add(five);ForButton4_mu1.
17、a(!d(six);ForBu(ton4_mu),add(mu1.);ForBu1.1.on4_mu1.add(BaiFenkForButtonI_sub.sc(1.ayout(F1.O);ForBuuonIqUbFdd(One);ForButton1_sub.add(Iwo;ForBu1.ton1.sub.add(three);ForButtonI-sub.add(sub);ForBuKon1.suKadd(FenZhiYi);ForButton()-cqua1.sct1.ayHit(F1.O);ForButtonO_equa1.add(zero);ForBu(tonO_equa1.add(
18、QuFin);ForBu(1.onO_equa1.addDian):ForButton()_cqua1.addadd):ForBu(ton0.cqua1.dd(cqua!);1.rane.ge1.Con(entPane().se1.1.ayou1.(F1.O);framc.gctContcntPanc().add1.HorRcsu1.t);fhmc.gctContcntPanc().add(ForButton7.c1.ear);fniine.ge(Con(en(Pane().add(ForBut(on4_mu);1.rane.ge1.ContentPanc().add(ForBuUonI_su
19、b);frame.ge(ContentPane().add(ForBtton0.equa1.);framc.sctDefau1.tC1.osc)peration(fninc.EX,ON-C1.OSE);framc.sctBounis(250,250,245,245);frame.seIReSiZab1.e(fa1.se);frame.se1.Visib1.e(me);c1.car.jidAcdonI.is(cncrtnewActionI.istcner()Ipub1.icvoidac1.ionPerforrned(Ac1.ionEven1.e)(rcsuh.sctTcxt(*);ZERO=-:
20、ONE=*:two=m;THREE=”;FOUR=”;F1.VE=Tsix=-;SEVEN=”;EIGHT=*;NINE=-;ReSUItStatC=窗U空Ypoint=*;i=0:);zero.1.dction1.istenednewAction1.istenerOpub1.icvoidacionPerforned(ActionEvente)(Z1.Ro=”已经点击”:ReSUkState=窗口不为空if(ONE=已经点击ITWO=己姓点击HREE=已经点击)FoUR=己经点击F1.VE=己经点击SIX=己经点击SEVEN=已经点击HEIGHT=已经点击“I1.N1.NE=已经点击”)(rc
21、su1.(.appcnd(O);Jif(R(rcsuh.scTcx(O);J);one.a(resh.se(TeM(2);)I);(htee.a1.dction1.isienertnewAction1.isienerO(pub1.icvoidactionPcrformed(ActionEventc)THREE=己经点击“;RCUIIState=窗口不为空”;point=已经点击ZEReH=已经点击”ONE=已经点击fWO=已经点击rHREE=已经点击I1.F1.VE=M已经点击SIX=已经点击“I1.SEVEN=已经点击I1.EIGHT=已线点击NINE=已经点击”&rcsun.getTexU
22、)!=O)resu1.(.appendnPerformed(ActionEven1.e)nPerfo11ned(ActionEven1.e)(SIX=已经点击”;ReSUItState=帕口不为空,;if(poini=己经点击IZERok己经点击QNE=1.1.经点击rWO=己经点击rHREE=己经点击IFIVE=己经点击SIX=己经点击IISEVEN=已经点击11EIGHT=”已经点击NINE=已经点击喳&rc$uk.gegtO!=,0)(resuh.append(*6);)RcsuhS1.atc=窗口空)(resh.seTeM6);)I);seven.1.dcnon1.istener(ne
23、wAction1.istenerOpub1.icvoidacionPerforned(Ac(ionEven(e)(SEVEN=”已经点击ReSUItSurte=窗口不为空if(poin1.=已经点击ZERo!=己姓点击IIONE=己姓点击WO=已经点击!THREE=已经点击IIFIVE=已经点击SIX=已经点击IISEVEN=已经点击|E1.GHT=己经点击NINE=己经点击“&必业.51叫戕()!=0)(rcsuh.appcndC?);Jif(Resu1.tStae=口空)(rcsuh.scTcxi(7);JI):cigh.addAction1.istcncr(newAction1.istc
24、ncrOpub1.icvoidac1.ionPerfor11*ed(ActionEven1.e)(E1.GHT=已羟点击.;ReSUhState=归口不为空if(poini=k1.经点击ZERok己经点击“IIONE=己经点击11TWO=已经点击rHREE=己经点击FIVE=己经点击SIX=己经点击SEVEN=已经点击EIGHT=已经点击飞N1.NE=已经点击”&gU1.1.gCtTeX1.()!=()(res1.pend(*8):)iftResuItStatc=Sf1.I)res1.(.se(Text(8);I);ninc.addAction1.istcnertnewAction1.istc
25、ncrt)pub1.icvoidacUonPerfb门ned(Ac1.ionEveme)(NINE=已经点击”:RCSUItStatC=窗口不为空”;if(point=已经点击1.1.ZERo!=己线点击ONE=f己姓点击WO=已经点击HTHREE=已经点击”|F1.VE=已经点击SIX=已经点击“IISEVEN=已经点击|E1.GHT=己经点击NINE=已经点击&rcsu1.1.geCRx1.O!=O)(rcsu1.(.appcndC9);Jif(Resu1.tStaie=窗口空)(rcsuh.sc(TcxiC9);)Dian.addction1.istener(newAciion1.ist
26、enerO(pub1.icvoidactionPcrformedActionEventC)POinI=己姓点击;i=i+1.;if(Rcsu1.tSMe=窗口不为空“&i=I)resuh.append*.*):);ad1.addAc1.ion1.istenertnewAction1.is1.enerOIpub1.icvoidactionPerfbrmcd(ActionEvcmc)(AmaI=已经选择加号”;addNumbcr=Doub1.ca1.ueif(rcsu!t.gc(TcxU).doub1.cVa1.ue();rcsu1.t.sctTcxt();i=0:I):sub.aAIAction
27、1.istcncr(newAciion1.is(cncr()pub1.icvoidac1.ionPerfo11nedActionEvcn1.e)AmaI=己经选择减号”;SubNumber=Dob1.e.va1.ueOfres1.(.geText(),dobkVaIueO;resu1.t.scText();i=0;);nu1.an1.istener(newAC1.iOn1.iSIenert)Ipub1.icvoidactionPerfbrmcd(ActionEvcmc)(Ama1.=已经选择除号;divNumber=Doub1.c.va1.ue()f(rcsu1.t.gccxt(),doub1
28、.cVa1.uc();rcsu1.t.sctTcxt();i=0:I):QUFan.addACtien1.iNCnCNnewAction1.istcncrOIpub1.icvoidac1.ionPerfo11nedActionEvcn1.e)QF=newDoub1.etDub1.e.va1.ueO1.(resu1.1.ge(Text().cJoub1.eVa1.ue():QF=QHQ1.):rcsu1.t.sctTcxt(QEoString();I);equa1.apub1.icvoidacionPcrformed(ActionEvcntc)if(Resu1.(S(ate=口不为空”(e11=
29、Dub1.e.va1.ueOf(resu!1.getText().doub1.eVa1.ue()/100:resu1.t.sctTcxt(tcnp.toSring(););FcnZhiYi.iuMAction1.istcnertnewAc(ion1.istencr()pub1.icvoidactionPcrformed(Ac1.ionEvente)tcm=1.(Doub1.e.va1.ucOf(rcsu1.(.gct1.cx().kHib1.cVa1.uc();resdt.see);JMenuhe1.p=DeWJMCnU(帮助(Hr,true):U工具条JTooIBar(oo1.Bar-new
30、J1bo1.Bar();/菜单内容JMenuItemjrni:实例化剪切板C1.ipboardc1.ipbd-gcToo1.kit().gcSystcmC1.ipboard();Siringtext=;U构造函数pub1.icJNoicPadUIOc1.assnew1.imp1.ementACUon1.iS1.enerpub1.icvoidacionPerfrmed(ActionEvcntc)ed(ActionEven1.e)(JFi1.cChooscrfc二newJFi1.cChooMMXXinireturnVa1.=他showDiakIg(JN3ePadUI.his.”翻开.);if(returnVa1.=JFi1.eChOgbAPPROVE-OPTION)(Stringfi1.e-fc.gcSc1.cctiFi1.c().gcPath();if(fi1.e=三nu1.1.)(return:1H读取文件try(Re1.crin=ne