Java-2实用教程实验指导模版代码_第1页
Java-2实用教程实验指导模版代码_第2页
Java-2实用教程实验指导模版代码_第3页
Java-2实用教程实验指导模版代码_第4页
Java-2实用教程实验指导模版代码_第5页
已阅读5页,还剩21页未读 继续免费阅读

下载本文档

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

文档简介

Java-2实用教程实验指导模版代码Java-2实用教程实验指导模版代码Java-2实用教程实验指导模版代码Java-2实用教程实验指导模版代码编制仅供参考审核批准生效日期地址:电话:传真:邮编:TOC\o"1-3"\h\z上机实践1初识Java 3实验1一个简单的应用程序 3实验2一个简单的JavaApplet程序 3实验3联合编译 4上机实践2基本数据类型与控制语句 6实验1输出希腊字母表 6实验2回文数 6实验3猜数字游戏 9上机实践3类与对象 10实验1三角形、梯形和圆形的类封装 10实验2实例成员与类成员 13实验3使用package语句与import语句 14上机实践4继承与接口 17实验1继承 17实验2上转型对象 19实验3接口回调 20上机实践5字符串、时间与数字 22实验1String类的常用方法 22实验2比较日期的大小 24实验3处理大整数 25上机实践6图形用户界面设计(1) 27实验2信号灯 28实验3布局与日历 31上机实践7图形用户界面设计(2) 35实验2字体对话框 36实验3华容道游戏 39上机实践8多线程 47实验1线程的4种状态 47实验2线程之间共享数据 49实验3挂起、恢复和终止线程 51上机实践9输入输出流 55实验1文件加密 55实验2给文件的内容添加行号 56实验3使用RandomAcessFile类处理文件 58实验4对象的写入与读取 60上机实践10Java中的网络编程 61实验1读取服务器端文件 61实验2使用套接字读取服务器端对象 63实验3基于UDP的图像传输 67上机实践11javaSwing 72实验1JLayeredPane分层窗格 72实验2进度条 73实验3使用表格显示日历 75实验4多文档界面(MDI) 80上机实践12数据结构 84实验1随机布雷 84实验2使用TreeSet排序 86

上机实践1初识Java实验1一个简单的应用程序程序模板publicclassHello{publicstaticvoidmain(Stringargs[]){【代码1】;import.*;publicclassFirstAppletextendsApplet{publicvoidpaint(Graphicsg){;【代码1】;classSunRise{publicstaticvoidmain(Stringargs[]){SquareEquationequation=newSquareEquation(4,5,1);();(-3,4,5);();}}

上机实践4继承与接口实验1继承程序模板classPeople{protecteddoubleweight,height;publicvoidspeakHello(){"yayawawa");}publicvoidaverageHeight(){height=173;"averageheight:"+height);}publicvoidaverageWeight(){weight=70;"averageweight:"+weight);}}classChinaPeopleextendsPeople{【代码1】;importDateExample{publicstaticvoidmain(Stringargs[]){Stringstr=("输入第一个日期的年份:");intyearOne=(str);str=("输入该年的月份:");intmonthOne=(str);str=("输入该月份的日期:");intdayOne=(str);str=("输入第二个日期的年份:");intyearTwo=(str);str=("输入该年的月份:");intmonthTwo=(str);str=("输入该月份的日期:");intdayTwo=(str);Calendarcalendar=【代码1】;classBigIntegerExample{publicstaticvoidmain(Stringargs[]){BigIntegern1=newBigInteger("654321"),n2=newBigInteger("456789"),result=null;"n1的二进制表示:"+(2));"n2的八进制表示:"+(8));result=【代码1】;import.*;publicclassGuessExampleextendsAppletimplementsActionListener{intnumber;TextFieldinputNumber;LabelfeedBack;ButtonnuttonGetNumber,buttonEnter;publicvoidinit(){nuttonGetNumber=newButton("得到一个随机数");feedBack=newLabel("无反馈信息",;;inputNumber=newTextField("0",5);buttonEnter=newButton("确定");【代码1】;import.*;importclassSignalExampleextendsAppletimplementsActionListener{ButtonbuttonRed,buttonGreen,buttonYellow;SignalCanvassignal;publicvoidinit(){buttonRed=【代码1】;import.*;import.*;publicclassBoyextendsAppletimplementsActionListener{Labela[]=newLabel[42];ButtontitleName[]=newButton[7];Stringname[]={"星期日","星期一","星期二","星期三","星期四","星期五","星期六"};ButtonnextMonth,previousMonth;intyear=2005,month=1;CalendarBeancalendar;LabelshowMessage=newLabel("",;publicvoidinit(){【代码1】etText(day[i]);}nextMonth=newButton("下月");previousMonth=newButton("上月");(this);(this);PanelpNorth=newPanel(),pSouth=newPanel();(previousMonth);(nextMonth);(showMessage);("日历:"+()+"年"+()+"月");ScrollPanescrollPane=newScrollPane();(pCenter);【代码5】etText(day[i]);}}elseif()==previousMonth){month=month-1;if(month<1)month=12;(month);Stringday[]=();for(inti=0;i<42;i++){a[i].setText(day[i]);}}("日历:"+()+"年"+()+"月");}}classCalendarBean{Stringday[];intyear=2005,month=0;publicvoidsetYear(intyear){=year;}publicintgetYear(){returnyear;}publicvoidsetMonth(intmonth){=month;}publicintgetMonth(){returnmonth;}publicString[]getCalendar(){Stringa[]=newString[42];et(year,month-1,1);int星期几=日历.get-1;intday=0;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){day=31;}if(month==4||month==6||month==9||month==11){day=30;}if(month==2){if(((year%4==0)&&(year%100!=0))||(year%400==0)){day=29;}else{day=28;}}for(inti=星期几,n=1;i<星期几+day;i++){a[i]=(n);n++;}returna;}}

上机实践7图形用户界面设计(2)程序模板import.*;importMyFrameextendsFrameimplementsActionListener{MenuBarbar=null;Menumenu=null;MenuItemitemRed,itemGreen;MyFrame(Strings){super(s);【代码1】;importFontDialogextendsDialogimplementsItemListener,ActionListener{Choicelist;JTextAreatext;Fontfont;Buttonyes,cancel;Componentcom;FontDialog(Framef,Stringname,Componentcom){super(f,name);=com;【代码1】;importclassMoveExample{publicstaticvoidmain(Stringargs[]){newHua_Rong_Road();}}classPersonextendsButtonimplementsFocusListener{intnumber;Colorc;Person(intnumber,Strings){super(s);=number;setFont(newFont("宋体",,14));setBackground;【代码1】etBounds(104,54,100,100);person[1].setBounds(104,154,100,50);person[2].setBounds(54,154,50,100);person[3].setBounds(204,154,50,100);person[4].setBounds(54,54,50,100);person[5].setBounds(204,54,50,100);person[6].setBounds(54,254,50,50);person[7].setBounds(204,254,50,50);person[8].setBounds(104,204,50,50);person[9].setBounds(154,204,50,50);【代码6】idth;inth=().height;if(y>h/2){goDown(man);}if(y<h/2){goUp(man);}if(x<w/2){goLeft(man);}if(x>w/2){goRight(man);}}publicvoidmouseReleased(MouseEvente){}publicvoidmouseEntered(MouseEvente){}publicvoidmouseExited(MouseEvente){}publicvoidmouseClicked(MouseEvente){}publicvoidgoDown(Personman){booleanmove=true;RectanglemanRect=();intx=().x;inty=().y;y=y+50;(x,y);RectanglebelowRect=();for(inti=0;i<10;i++){RectanglepersonRect=person[i].getBounds();if((personRect))&&!=i)){move=false;}}if(belowRect)){move=false;}if(move==true){(x,y);}}publicvoidgoUp(Personman){booleanmove=true;RectanglemanRect=();intx=().x;inty=().y;y=y-50;(x,y);RectangleaboveRect=();for(inti=0;i<10;i++){RectanglepersonRect=person[i].getBounds();if((personRect))&&!=i)){move=false;}}if(aboveRect)){move=false;}if(move==true){(x,y);}}publicvoidgoLeft(Personman){booleanmove=true;RectanglemanRect=();intx=().x;inty=().y;x=x-50;(x,y);RectangleleftRect=();for(inti=0;i<10;i++){RectanglepersonRect=person[i].getBounds();if((personRect))&&!=i)){move=false;}}if(leftRect)){move=false;}if(move==true){(x,y);}}publicvoidgoRight(Personman){booleanmove=true;RectanglemanRect=();intx=().x;inty=().y;x=x+50;(x,y);RectanglerightRect=();for(inti=0;i<10;i++){RectanglepersonRect=person[i].getBounds();if((personRect))&&!=i)){move=false;}}if(rightRect)){move=false;}if(move==true){(x,y);}}publicvoidactionPerformed(ActionEvente){removeAll();init();validate();repaint();}}

上机实践8多线程实验1线程的4种状态程序模板classTortoiseextendsThread{intsleepTime=0,liveLength=0;Tortoise(intsleepTime,Stringname,intliveLength){=sleepTime;=liveLength;setName(name);}publicvoidrun(){while(true){liveLength--;"@");try{【代码1】;importWinextendsFrameimplementsRunnable,ActionListener{ThreadmoveOrStop;Buttonstart,hang,resume,die;LabelmoveLabel;booleanmove=false,dead=false;Win(){【代码1】;inty=().y;y=y+2;if(y>=200)y=10;(x,y);try{(200);}catch(InterruptedExceptione2){}if(dead==true){【代码5】;publicclassSecretExample{publicstaticvoidmain(Stringargs[]){FilefileOne=newFile(""),fileTwo=newFile("");charb[]=newchar[100];try{FileReaderin=【代码1】;publicclassReadExample{publicstaticvoidmain(Stringargs[]){Filefile=newFile("c:/1000","");FiletempFile=newFile("");try{FileReaderinOne=【代码1】;publicclassRandomExample{publicstaticvoidmain(Stringargs[]);import.*;publicclassObjectExample{publicstaticvoidmain(Stringargs[]){Calendarcalendar1=();;import.*;import.*;import.*;publicclassReadFileextendsAppletimplementsActionListener,Runnable{Filefile;Choicechoice;TextAreatext;Threadthread;Stringitem=null;Buttonbutton;URLurl;publicvoidinit(){choice=newChoice();thread=newThread(this);intN=(getParameter("总数"));for(inti=1;i<=N;i++){(getParameter(i)));}button=newButton("开始读取");text=newTextArea(12,40);(this);add(choice);add(button);add(text);}publicvoidactionPerformed(ActionEvente){(null);item=();item=();if(!())){thread=newThread(this);}try{();}catch(Exceptionexp){}}publicvoidrun(){try{url=newURL(getCodeBase(),item);InputStreamin=【代码1】;import.*;import.*;importClientextendsFrameimplementsRunnable,ActionListener{Buttonconnection;Socketsocket=null;ObjectInputStreamin=null;ObjectOutputStreamout=null;Threadthread;publicClient(){socket=newSocket();connection=newButton("连接服务器,读取文本区对象");add(connection,;(this);thread=newThread(this);setBounds(100,100,360,310);setVisible(true);addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){(0);}});}publicvoidrun(){while(true){try{TextAreatext=(TextArea)();add(text,;validate();}catch(Exceptione){break;}}}publicvoidactionPerformed(ActionEvente){if()==connection){try{if()){}else{InetAddressaddress=("");InetSocketAddresssocketAddress=【代码1】;import.*;import.*;import.*;publicclassServer{publicstaticvoidmain(Stringargs[]){ServerSocketserver=null;ServerThreadthread;Socketyou=null;while(true){try{server=【代码1】tart();;import.*;import.*;classImageCanvasextendsCanvas{Imageimage=null;publicImageCanvas(){setSize(200,200);}publicvoidpaint(Graphicsg){if(image!=null)(image,0,0,getSize().width,getSize().height,this);}publicvoidsetImage(Imageimage){=image;}}classClientextendsFrameimplementsRunnable,ActionListener{Buttonb=newButton("获取图像");ImageCanvascanvas;Client(){super("Iamaclient");setSize(320,200);setVisible(true);(this);add(b,;canvas=newImageCanvas();add(canvas,;Threadthread=newThread(this);validate();addWindowListener(newWindowAdapter(){publicvoidwindowClosing(WindowEvente){(0);}});();}publicvoidactionPerformed(ActionEventevent)rim().getBytes();try{InetAddressaddress=("");DatagramPacketdata=【代码1】;import.*;publicclassServer{publicstaticvoidmain(Stringargs[]){DatagramPacketpack=null;DatagramSocketmailReceive=null;ServerThreadthread;byteb[]=newbyte[8192];InetAddressaddress=null;pack=newDatagramPacket(b,;while(true){try{mailReceive=【代码1】tart();}else{continue;}}}}classServerThreadextendsThread{InetAddressaddress;DataOutputStreamout=null;DataInputStreamin=null;Strings=null;ServerThread(InetAddressaddress){=address;}publicvoidrun(){FileInputStreamin;byteb[]=newbyte[8192];try{in=newFileInputStream("");intn=-1;while((n=(b))!=-1){DatagramPacketdata=【代码3】etBytes();DatagramPacketdata=【代码6】;import.*;classLayerExample{publicstaticvoidmain(Stringargs[]){JFramewin=newJFrame("窗体");(100,100,300,300);(true);JButtonb1=newJButton("我在DEFAULT_LAYER"),b2=newJButton("我在PALETTE_LAYER"),b3=newJButton("我在MODAL_LAYER"),b4=newJButton("我在POPUP_LAYER"),b5=newJButton("我在DRAG_LAYER");ContainercontenetPane=();JLayeredPanepane=【代码1】;import.*;importclassBarExample{publicstaticvoidmain(Stringargs[]){BarWinwin=newBarWin();}}classBarWinextendsJFrameimplementsRunnable{JProgressBarpbar1,pbar2;Threadthread1,thread2;JTextFieldtext1,text2;BarWin(){pbar1=【代码1】;import.*;import.*;importclassCalendarExample{publicstaticvoidmain(Stringargs[]){newGoodWindow();}}classGoodWindowextendsJFrameimplementsItemListener{JComboBoxlist=null;CalenderPanecalenderPane=null;Containercon=null;GoodWindow(){list=【代码1】/创建一个下拉列表,该列表不含有任何选项。for(inti=2000;i<=2010;i++){Stringtemp=""+i;【代码2】et(year,month-1,1);int星期几=日历.get-1;intday=0;if(month==1||month==3||month==5||month==7||month==8||month==10||month==12){day=31;}if(month==4||month==6||month==9||month==11){day=30;}if(month==2){if(((year%4==0)&&(year%100!=0))||(year%400==0)){day=29;}else{day=28;}}for(inti=星期几,n=1;i<星期几+day;i++){a[i]=(n);n++;}returna;}}实验4多文档界面(MDI)程序模板import.*;import.*;importMyInternalFrameextendsJInternalFrame{JTextAreatext;MyInternalFrame(Stringtitle){super(title,true,true,true,true);text=newJTextArea();Containercon=getContentPane();(newJScrollPane(text),;setDefaultCloseOperation;addInternalFrameListener(newInternalFrameAdapter(){publicvoidinternalFrameActivated(InternalFrameEvente){setLayer;}publicvoidinternalFrameDeactivated(InternalFrameEvente){setLayer;}});}publicJTextAreagetJTextArea(){returntext;}}classMywindowextendsJFrameimplementsActionListener{JDesktopPanedesk;;classBlock{Stringname;booleanboo=false;publicvoidsetNam

温馨提示

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

评论

0/150

提交评论