经典Java程序源代码_第1页
经典Java程序源代码_第2页
经典Java程序源代码_第3页
经典Java程序源代码_第4页
经典Java程序源代码_第5页
已阅读5页,还剩30页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

经典Java程序源代码经典Java程序源代码经典Java程序源代码经典Java程序源代码编制仅供参考审核批准生效日期地址:电话:传真:邮编:经典Java程序源代码1.加法器(该java源文件的名称是)import.*;import.*;publicclassAdderimplementsActionListener{ JFrameAdderFrame; JTextFieldTOprand1; JTextFieldTOprand2; JLabelLAdd,LSum; JButtonBAdd,BClear; JPanelJP1,JP2; publicAdder() { AdderFrame=newJFrame("AdderFrame"); TOprand1=newJTextField(""); TOprand2=newJTextField(""); LAdd=newJLabel("+"); LSum=newJLabel("="); BAdd=newJButton("Add"); BClear=newJButton("Clear"); JP1=newJPanel(); JP2=newJPanel(); (this); (newActionListener() { publicvoidactionPerformed(ActionEventevent) { (""); (""); ("="); } }); (JP1); (TOprand1); (LAdd); (TOprand2); (LSum); (JP2); (BAdd); (BClear); ().setLayout(newBorderLayout()); ().add(JP1,; ().add(JP2,; (newWindowAdapter() { publicvoidwindowClosing(WindowEventevent) { (0); } }); (); (true); (false); (250,100); } publicvoidactionPerformed(ActionEventevent) { doublesum=(double)()).doubleValue()+()).doubleValue()); ("="+sum); } publicstaticvoidmain(String[]args) { Adderadder=newAdder(); }}小型记事本(该java源文件由两个类构成,名称为)import.*;import.*;import.*;classmynotepadextendsJFrame{Filefile=null; Colorcolor=; mynotepad() { initTextContent(); initMenu(); initAboutDialog(); } voidinitTextContent() { getContentPane().add(newJScrollPane(content)); }JTextPanecontent=newJTextPane();JFileChooseropenfile=newJFileChooser();JColorChooseropencolor=newJColorChooser();JDialogabout=newJDialog(this);JMenuBarmenu=newJMenuBar(); ength;j++) { menus[i].add(optionofmenu[i][j]); optionofmenu[i][j].addActionListener(action); } } (menu);}ActionListeneraction=newActionListener(){quals(name)) { (""); file=null; } elseif("打开".equals(name)) { if(file!=null) (file);intreturnVal=;if(returnVal=={ file=();unfold();} } elseif("保存".equals(name)) { if(file!=null)(file); intreturnVal=;if(returnVal=={ file=(); saving();}}elseif("退出".equals(name)){ mynotepadf=newmynotepad(); ints=(f,"退出","退出",;if(s==(0);}elseif("剪切".equals(name)){ ();}elseif("复制".equals(name)){ ();}elseif("粘贴".equals(name)){ ();}elseif("颜色".equals(name)){ color=,"",color);(color); } elseif("关于".equals(name)) {(300,150);(); } } }; voidsaving() { try { FileWriterWritef=newFileWriter(file); ()); ();} catch(Exceptione) { (); }} voidunfold() { try { FileReaderReadf=newFileReader(file); intlen=(int)(); char[]buffer=newchar[len]; (buffer,0,len);();(newString(buffer));}catch(Exceptione){ ();} } voidinitAboutDialog() { (newGridLayout(3,1)); ().setBackground; ().add(newJLabel("我的记事本程序"));dd(newJLabel("制作者:Fwx")); ().add(newJLabel("2007年12月")); (true);;import.*;classsimplecalculator{ staticStringpoint=newString(); staticStringAmal=newString(); staticStringONE=newString(); staticStringTWO=newString(); staticStringTHREE=newString(); staticStringFOUR=newString(); staticStringFIVE=newString(); staticStringSIX=newString(); staticStringSEVEN=newString(); staticStringEIGHT=newString(); staticStringNINE=newString(); staticStringZERO=newString(); staticStringResultState=newString(); staticDoubleQF; staticJButtonzero=newJButton("0"); staticJButtonone=newJButton("1"); staticJButtontwo=newJButton("2"); staticJButtonthree=newJButton("3"); staticJButtonfour=newJButton("4"); staticJButtonfive=newJButton("5"); staticJButtonsix=newJButton("6"); staticJButtonseven=newJButton("7"); staticJButtoneight=newJButton("8"); staticJButtonnine=newJButton("9"); staticJButtonadd=newJButton("+"); staticJButtonsub=newJButton("-"); staticJButtonmul=newJButton("*"); staticJButtondiv=newJButton("/"); staticJButtonQuFan=newJButton("+/-"); staticJButtonDian=newJButton("."); staticJButtonequal=newJButton("="); staticJButtonclear=newJButton("C"); staticJButtonBaiFen=newJButton("%"); staticJButtonFenZhiYi=newJButton("1/x"); staticinti=0; staticDoubleaddNumber; staticDoublesubNumber; staticDoublemulNumber; staticDoubledivNumber; staticDoubleequalNumber; staticDoubletemp; staticJTextArearesult=newJTextArea(1,20); publicstaticvoidmain(String[]args) { JFrameframe=newJFrame("计算器"); (false); (""); ResultState="窗口空"; JPanelForResult=newJPanel(); JPanelForButton7_clear=newJPanel(); JPanelForButton4_mul=newJPanel(); JPanelForButton1_sub=newJPanel(); JPanelForButton0_equal=newJPanel(); FlowLayoutFLO=newFlowLayout(); (result); (FLO); (seven); (eight); (nine); (div); (clear); (FLO); (four); (five); (six); (mul); (BaiFen); (FLO); (one); (two); (three); (sub); (FenZhiYi); (FLO); (zero); (QuFan); (Dian); (add); (equal); ().setLayout(FLO); ().add(ForResult); ().add(ForButton7_clear); ().add(ForButton4_mul); ().add(ForButton1_sub); ().add(ForButton0_equal); ; (250,250,245,245); (false); (true); (newActionListener() { publicvoidactionPerformed(ActionEvente) { (""); ZERO=""; ONE=""; TWO=""; THREE=""; FOUR=""; FIVE=""; SIX=""; SEVEN=""; EIGHT=""; NINE=""; ResultState="窗口空"; point=""; i=0; } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { ZERO="已经点击"; ResultState="窗口不为空"; if(ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FOUR=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击") { ("0"); } if(ResultState=="窗口空") { ("0"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { ONE="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("1"); } if(ResultState=="窗口空") { ("1"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { TWO="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("2"); } if(ResultState=="窗口空") { ("2"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { THREE="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("3"); } if(ResultState=="窗口空") { ("3"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { FOUR="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("4"); } if(ResultState=="窗口空") { ("4"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { FIVE="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("5"); } if(ResultState=="窗口空") { ("6"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { SIX="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("6"); } if(ResultState=="窗口空") { ("6"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { SEVEN="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("7"); } if(ResultState=="窗口空") { ("7"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { EIGHT="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("8"); } if(ResultState=="窗口空") { ("8"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { NINE="已经点击"; ResultState="窗口不为空"; if(point=="已经点击"||ZERO!="已经点击"||ONE=="已经点击"||TWO=="已经点击"||THREE=="已经点击"||FIVE=="已经点击"||SIX=="已经点击"||SEVEN=="已经点击"||EIGHT=="已经点击"||NINE=="已经点击"&&()!="0") { ("9"); } if(ResultState=="窗口空") { ("9"); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { point="已经点击"; i=i+1; if(ResultState=="窗口不为空"&&i==1) { ("."); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { Amal="已经选择加号"; addNumber=()).doubleValue(); (""); i=0; } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { Amal="已经选择减号"; subNumber=()).doubleValue(); (""); i=0; } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { Amal="已经选择乘号"; mulNumber=()).doubleValue(); (""); i=0; } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { Amal="已经选择除号"; divNumber=()).doubleValue(); (""); i=0; } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { QF=newDouble()).doubleValue()); QF=QF*(-1); ()); } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { equalNumber=()).doubleValue(); if(Amal=="已经选择加号") { temp=addNumber+equalNumber; ()); } if(Amal=="已经选择减号") { temp=subNumber-equalNumber; ()); } if(Amal=="已经选择乘号") { temp=mulNumber*equalNumber; ()); } if(Amal=="已经选择除号") { temp=divNumber/equalNumber; ()); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { if(ResultState=="窗口不为空") { temp=()).doubleValue()/100; ()); } } }); (newActionListener() { publicvoidactionPerformed(ActionEvente) { temp=1/()).doubleValue()); ()); } }); } }图形化写字板(该java源文件的名称是);import.*;import.*;importclassJNotePadUIextendsJPanel{ etSystemClipboard(); Stringtext=""; etPath(); if(file==null) { return; } etPath(); if(savefile==null) { return; } else { StringdocToSave=(); if(docToSave!=null) { FileOutputStreamfstrm=null; BufferedOutputStreamostrm=null; try { fstrm=newFileOutputStream(savefile); ostrm=newBufferedOutputStream(fstrm); byte[]bytes=null; try { bytes=(); } catch(Exceptione1) { (); } (bytes); } catch(IOExceptionio) { "IOException:"+()); } finally { try { (); (); (); } catch(IOExceptionioe) { "IOException:"+()); } } } } } else { return; } } } etVisible(true); (0); } dd(applet,; (newappCloseL()); (800,500); (500,170); (true); (); (false); }}数字化的连连看(该java源文件的名称是)import.*;import.*;importpublicclasslianliankanimplementsActionListener{ JFramemainFrame;ddActionListener(this); (diamondsButton[cols][rows]); } } exitButton=newJButton("退出"); (this); resetButton=newJButton("重列"); (this); newlyButton=newJButton("再来一局"); (this); (exitButton); (resetButton); (newlyButton); ()))); (fractionLable); (280,100,500,450); (true); } publicvoidrandomBuild() { intrandoms,cols,rows; for(inttwins=1;twins<=15;twins++) { randoms=(int)()*25+1); for(intalike=1;alike<=2;alike++) { cols=(int)()*6+1); rows=(int)()*5+1); while(grid[cols][rows]!=0) { cols=(int)()*6+1); rows=(int)()*5+1); } [cols][rows]=randoms; } } } publicvoidfraction() { ())+100)); } publicvoidreload() { intsave[]=newint[30]; intn=0,cols,rows; intgrid[][]=newint[8][7]; for(inti=0;i<=6;i++) { for(intj=0;j<=5;j++) { if[i][j]!=0) { save[n]=[i][j]; n++; } } } n=n-1; =grid; while(n>=0) { cols=(int)()*6+1); rows=(int)()*5+1); while(grid[cols][rows]!=0) { cols=(int)()*6+1); rows=(int)()*5+1); } [cols][rows]=save[n]; n--; } (false); pressInformation=false;etVisible(false); } } } publicvoidestimateEven(intplaceX,intplaceY,JButtonbz) { if(pressInformation==false) { x=placeX; y=placeY; secondMsg=grid[x][y]; secondButton=bz; pressInformation=true; } else { x0=x; y0=y; fristMsg=secondMsg; firstButton=secondButton; x=placeX; y=placeY; secondMsg=grid[x][y]; secondButton=bz; if(fristMsg==secondMsg&&secondButton!=firstButton) { xiao(); } } }publicvoidxiao(){;import.*;import.*;importMenuDemoextendsJPanelimplementsActionListener{ JTextAreaJTA=newJTextArea(13,22); JFrameframe=newJFrame("TheEditor"); JScrollPaneJSP=newJScrollPane(JTA); JFileChooserJFC=newJFileChooser(); Clipboardclipbd=getToolkit().getSystemClipboard(); intJFC_ReturnValue; Stringselection; MenuDemo() { JMenuBarjmb=newJMenuBar(); JMenufile=newJMenu("File"); JMenuedit=newJMenu("Edit"); JMenusearch=newJMenu("Search"); JMenuconfigure=newJMenu("Configure"); JMenuhelp=newJMenu("Help"); (file); (edit); (search); (configure); (help); JMenuItemNEW=newJMenuItem("New"); JMenuItemOPEN=newJMenuItem("Open"); JMenuItemSAVE=newJMenuItem("SaveAs"); JMenuItemEXIT=newJMenuItem("Exit"); (NEW); (this); (OPEN); (this); (SAVE); (this); (EXIT); (this); JMenuItemCUT=newJMenuItem("Cut"); JMenuItemCOPY=newJMenuItem("Copy"); JMenuItemPASTE=newJMenuItem("Paste"); JMenuItemDELETE=newJMenuItem("Delete"); (CUT); (this); (COPY); (this); (PASTE); (this); (DELETE); (this); JMenuItemFIND=newJMenuItem("Find"); JMenuItemREPLACE=newJMenuItem("Replace"); JMenuItemBOOK_MARKS=newJMenuItem("Bookmarks"); (FIND); (REPLACE); (BOOK_MARKS); JMenuSET_COLOR=newJMenu("SetColor"); JMenuItemSET_BACK_GROUND_COLOR=newJMenuItem("SetBackGroundColor"); JMenuItemSET_FONT_COLOR=newJMenuItem("SetFontColor"); (SET_BACK_GROUND_COLOR); (this); (SET_FONT_COLOR); (this); JMenuSET_FONT=newJMenu("SetFont"); JMenuItemSET_FONT_SIZE=newJMenuItem("SetFontSize"); JMenuItemSET_FONT_BLOCK=newJMenuItem("SetFontBlock"); (SET_FONT_SIZE); (SET_FONT_BLOCK); (SET_COLOR); (SET_FONT); JMenuItemHELP_CONTENTS=newJMenuItem("HelpContents"); JMenuItemABOUT_THE_PROGRAM=newJMenuItem("AboutTheProgram"); (HELP_CONTENTS); (ABOUT_THE_PROGRAM); (this); (true); (true); ().setLayout(newFlowLayout()); ().add(jmb); ().add(JSP); (); (252,300); (400,250); (false); ; } publicstaticvoidmain(String[]args) { MenuDemoMD=newMenuDemo(); } publicvoidactionPerformed(ActionEvente) { if()=="New") { ints=(frame,"Areyousuretobuildnewone\n","New",;if(s== (""); } if()=="Open") { JFC_ReturnValue=(frame); if(JFC_ReturnValue== { Stringfile=().getPath(); if(file==null) { return; } try { Readerin=newFileReader(file); char[]buff=newchar[4096]; intnch; while((nch=(buff,0,)!=-1) { (newPlainDocument()); (newString(buff,0,nch)); } } catch(IOExceptionio) { "IOException:"+()); } } else { return; } } if()=="SaveAs") { JFC_ReturnValue=(frame); if(JFC_ReturnValue== { Stringsavefile=().getPath(); if(savefile==null) { return; } else { StringdocToSave=(); if(docToSave!=null) { FileOutputStreamfstrm=null; BufferedOutputStreamostrm=null; try { fstrm=newFileOutputStream(savefile); ostrm=newBufferedOutputStream(fstrm); byte[]bytes=null; try { bytes=(); } catch(Exceptione1) { (); } (bytes); } catch(IOExceptionio) { "IOException:"+()); } finally { try { (); (); (); } catch(IOExceptionioe) { "IOException:"+()); } } } } } else { return; } } if()=="Exit") { ints=(frame,"Areyousuretoexit","Exit",;if(s==(0); } if()=="Cut") { selection=(); StringSelectionclipString=newStringSelection(selection); (clipString,clipString); ("",(),()); } if()=="Copy") { selection=(); StringSelectionclipString=newStringSelection(selection); (clipString,clipString); } if()=="Paste") { TransferableclipData=(frame); try { StringclipString=(String); (clipString,(),()); } catch(Exceptionex) { } } if()=="Delete") { ("",(),()); } if()=="SetBackGroundColor") { Colorcolor=(this,"ChooseBackGroundColor",; if(color!=null) { (color); } else return; } if()=="SetFontColor") { Colorcolor=(this,"ChooseBackGroundColor",; if(color!=null) { (color); } else return; } if()=="HelpContents") { } if()=="AboutTheProgram") { (JTA,"HelloWorld!","AboutTheProgram",; } }}小型抽奖系统——导入手机号和姓名(该java源文件的名称是)在源代码之前告诉大家一声,导入的文件格式是txt文本格式,文件的内容可以有上千百万个用户和手机号构成,格式如下:叶尔凯西-革命-热依古丽-杰格尔-小叶-大爷-小花-系统最少要输入7名用户,三等奖3名,二等奖2名,一等奖1名,特等奖1名;下面是Java源代码import.*;import.*;import.*;import.*;publicclassChooseAwardextendsJFrame{/****/"),newJMenuItem("保存"),newJMenuItem("退出")};JTextFieldt_identNumber=newJTextField();ddActionListener(newActionListener(){ddActionListener(newActionListener(){.");..{awardThread=newChooseThread(this);();("将产生:特(1名),一(1名),二(2名),三(3名)等奖");("选取中...");(false);(true);}}}/**暂停按钮的事件*/publicvoidb_stop_ActionPerformed(ActionEvente){quals(k))){quals(k))){str_name=(String)(k);(k);(k);break;}}("二等奖");awardmessage="二等奖"+()+str_name+"\r\n";(awardmessage);intserial=chooseTime-3;quals(k))){str_name=(String)(k);(k);(k);break;}}("一等奖");awardmessage="一等奖"+()+str_name+"\r\n";(awardmessage);/**第几位得主,排名序号,其实没必要这样写,因为本来就这一名*现在这样写的作用是为我以后改进这个程序(可以自主选择产生几名得奖主)*的时候做好准备*/quals(k))){str_name=(String)(k);(k);(k);break;}}("特等奖");awardmessage="特等奖"+()+str_name+"\r\n";(awardmessage);message="特等奖得主为:"+str_name;,message);("此次抽奖全部结束,保存抽奖结果!");break;default:,"抽奖已经结束");("开始");();break;}(true);(false);}/**输出按钮的事件*/publicvoidb_printaward_ActionPerformed(ActionEvente){try{FileOutputStreamfs_out=newFileOutputStream("");DataOutputStreamout=newDataOutputStream(fs_out);for(inti=0;i<();i++){测试用((String)(i)+"\r\n");}();("文件输出成功!保存在当前目录下..");}catch(FileNotFoundExceptionfe){}catch(IOExceptionioe){}}/**定义一个方法,将号码和名字分离开来*/lementAt(num));try{sleep(50);}catch(Exceptione){();}}}}图形化的能求输入的任意数的——阶乘(该java源文件的名称是)import.*;import.*;classFactorimplementsActionListener{ JTextAreajta=newJTextArea(1,15); JFrameframe=newJFrame("求阶乘"); Factor() { JButtonOK=newJButton("Ok"); (this); JButtonCancel=newJButton("Cancel"); (this); JButtonClear=newJButton("Clear"); (this); ().setLayout(newFlowLayout()); ().add(jta); ().add(OK); ().add(Cancel); ().add(Clear); ; (); (300,250); (400,200); } publicstaticvoidmain(String[]args) { Factorfac=newFactor(); } publicvoidactionPerformed(ActionEvente) { if()=="Ok"); { calcu(); } if()=="Clear") { (""); } if()=="Cancel") { (0); } } voidcalcu() { Integera; a=()).intValue(); for(inti=a-1;i>=1;i--) { a=a*i; } ()); }}非图形化界面的——统计所输入的字符串的程序(该java源文件的名称是)importcounter{ publicstaticvoidmain(String[]args)

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论