




版權說明:本文檔由用戶提供并上傳,收益歸屬內容提供方,若內容存在侵權,請進行舉報或認領
文檔簡介
《Java程序設計》課后答案作者:崔志磊曾海吳道君任務29任務實訓1、importjava.util.Set;importjava.util.TreeSet;classPhonesxaimplementsComparable{ Stringname; //品牌 intxl; //銷量 publicPhonesxa(Stringn,inti){ xl=i; name=n; } publicStringtoString(){ return"品牌:"+name+"\t銷量:"+xl+"萬"; } @Override publicintcompareTo(Objectarg0){ //TODOAuto-generatedmethodstub Phonesxaother=(Phonesxa)arg0; if(this.xl>other.xl){ return1; } elseif(this.xl<other.xl){ return-1; }else return0; }}publicclassApp29_sxa{ /** *@paramargs */ publicstaticvoidmain(String[]args){ //TODOAuto-generatedmethodstub Set<Phonesxa>setl=newTreeSet<Phonesxa>(); setl.add(newPhonesxa("華為",85)); setl.add(newPhonesxa("小米",68)); setl.add(newPhonesxa("蘋果",75)); setl.add(newPhonesxa("華為",85)); for(Phonesxae:setl){ System.out.println(e); } }}任務實訓2、importjava.util.Comparator;importjava.util.Set;importjava.util.TreeSet;classStudsxb{ Stringname; intscore; publicStudsxb(Stringn,intcj){ name=n; score=cj; } publicStringtoString(){ returnname+"\t"+score; }}classStudScoreCompimplementsComparator<Studsxb>{ publicintcompare(Studsxbarg0,Studsxbarg1){ if(arg0.score<arg1.score){return1;} elseif(arg0.score>arg1.score){return-1;} elsereturn0; }}publicclassApp29_sxb{ publicstaticvoidmain(String[]args){ Set<Studsxb>setl=newTreeSet<Studsxb>(newStudScoreComp()); //說明2 setl.add(newStudsxb("端木",85)); setl.add(newStudsxb("歐陽",68)); setl.add(newStudsxb("上官",75)); setl.add(newStudsxb("司馬",90)); System.out.println("姓名\tJava"); for(Studsxbe:setl){ System.out.println(e); } }}習題選擇題1、(D)。任務30任務實訓1、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassApp30_sxaextendsJFrameimplementsActionListener{ //窗口類 Image[]img=newImage[10]; String[]str={"gz1.jpg","gz2.jpg","gz3.jpg","gz4.jpg","gz5.jpg", "gz6.jpg","gz7.jpg","gz8.jpg","gz9.jpg","gz10.jpg"}; JButtonbt=newJButton("慢放"); staticintptr=0,x=1000; Containerc=getContentPane(); staticXclxc; publicApp30_sxa(){ setSize(500,460); setLocation(400,200); bt.setBounds(200,500,50,50); bt.addActionListener(this); c.add(bt,BorderLayout.SOUTH); for(inti=0;i<10;i++){ img[i]=Toolkit.getDefaultToolkit().getImage("圖片/"+str[i]); } setVisible(true); } publicvoidpaint(Graphicsg){ super.paint(g); g.drawImage(img[ptr%10],0,20,this); } publicvoidGetptr(inta){ //用于從線程類獲得圖片編號a ptr=a; } publicvoidactionPerformed(ActionEvente){ if(e.getSource()==bt){ x+=500; } xc.Getx(x); //使線程類獲得x的增值 } publicstaticvoidmain(String[]args){ App30_sxahd=newApp30_sxa(); xc=newXcl(hd); //構建線程對象 xc.start(); //啟動線程 }}classXclextendsThread{ //自定義線程類 intptr=0,x=500; App30_sxah; publicXcl(App30_sxaa){ h=a; } publicvoidGetx(inta){ x=a; } publicvoidrun(){ for(;;){ ptr++; h.Getptr(ptr); try{ Thread.sleep(x); h.repaint(); }catch(InterruptedExceptione){ } } }}任務實訓2、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassApp30_sxbextendsJFrame{ //畫圓 Containerc=getContentPane(); booleanbol=true; inta=100,b=100; publicApp30_sxb(){ setSize(300,300);setLocation(400,10);newThread(newRunnable(){ publicvoidrun(){ for(;;){ if(a<=250){a+=3;b+=3;} elsebreak; try{ Thread.sleep(500); repaint(); } catch(InterruptedExceptione){} } } }).start();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true); } publicvoidpaint(Graphicsg){ super.paint(g); g.drawOval(15,35,a,b); } publicstaticvoidmain(String[]args){ App30_sxbhda=newApp30_sxb(); hfybhdb=newhfyb(); }}classhfybextendsJFrame{ //畫矩形 Containerc=getContentPane(); booleanbol=true; inta=100,b=100; publichfyb(){ setSize(300,300);setLocation(50,10);newThread(newRunnable(){ publicvoidrun(){ for(;;){ if(a<=250){a+=3;b+=3;} try{ Thread.sleep(500); repaint(); } catch(InterruptedExceptione){} } } }).start();setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);setVisible(true); } publicvoidpaint(Graphicsg){ super.paint(g); g.setColor(Color.red); g.drawRect(15,35,a,b); }}習題一、填空題1、Thread。2、Runnable。3、 CPU資源 。二、選擇題1、(A)。2、(C)。任務31任務實訓1、publicclassApp31_sxa{Match[]t=newMatch[9];publicApp31_sxa(){ for(inti=0;i<9;i++){ t[i]=newMatch(); t[i].setName(""+i); }}classMatchextendsThread{ intw=100; publicvoidrun(){ while(w>0){ intx=Integer.parseInt(Thread.currentThread().getName()); w--; System.out.println(x+"位置:"+w);; /*try{ //可以不用 sleep(100); } catch(InterruptedExceptione){return;}*/ } System.out.println(getName()+"\t線程到達終點了"); } } publicstaticvoidmain(String[]args){ App31_sxaa=newApp31_sxa(); for(inti=0;i<9;i++){ a.t[i].start(); } }}實訓任務2、publicclassApp31_sxb{ Match[]ta=newMatch[9]; Thread[]t=newThread[9]; Booleanflag=true; publicApp31_sxb(){ for(inti=0;i<9;i++){ ta[i]=newMatch(); t[i]=newThread(ta[i]); t[i].setName(""+i); } } classMatchimplementsRunnable{ //接口實現 intw=100; publicvoidrun(){ while(flag){ intx=Integer.parseInt(Thread.currentThread().getName()); w--; System.out.println(x+"位置:"+w); if(w==0){ flag=false; //結束比賽 System.out.println(Thread.currentThread().getName() +"線程到達終點了比賽結束"); } try{ Thread.sleep(100); }catch(InterruptedExceptione){return;} } } } publicstaticvoidmain(String[]args){ App31_sxba=newApp31_sxb(); for(inti=0;i<9;i++){ a.t[i].start(); } }}習題一、填空題1、 創建 、 就緒、睡眠、 運行消亡。2、就緒。3、 運行 。二、選擇題1、(C)。2、(A)。任務32任務實訓1、classTicket{ privatestaticinttickets=10; //總火車票 publicvoidSell(){ //統計剩余車票 synchronized(this){ System.out.println(Thread.currentThread().getName()+":賣出了第8車,第" +tickets+"座"); tickets=tickets-1; } }}classTicketOfficeextendsThread{ //售票點 Ticketbk; publicTicketOffice(Tickets){ bk=s; } publicvoidrun(){ bk.Sell(); //售出票 try{ sleep(100); }catch(InterruptedExceptione){} }}publicclassApp32_sxa{ publicstaticvoidmain(String[]args){ Tickets=newTicket(); TicketOfficeoff=newTicketOffice(s); Thread[]thr=newThread[10]; //10個線程變量 for(inti=0;i<10;i++){ thr[i]=newThread(off); //創建線程 thr[i].setName("No"+(i+1)); //命名線程 thr[i].start(); //啟動線程 } }}任務實訓2、classTicket{ privatestaticinttickets=10; publicsynchronizedvoidSell(){ System.out.println(Thread.currentThread().getName()+":賣出了第8車,第" +tickets+"座"); tickets=tickets-1; }}classTicketOfficeextendsThread{ Ticketbk; publicTicketOffice(Tickets){ bk=s; } publicvoidrun(){ bk.Sell(); try{ sleep(100); }catch(InterruptedExceptione){} }}publicclassApp32_sxb{ publicstaticvoidmain(String[]args){ Tickets=newTicket(); TicketOfficeoff=newTicketOffice(s); Thread[]thr=newThread[10]; for(inti=0;i<10;i++){ thr[i]=newThread(off); thr[i].setName("No"+(i+1)); thr[i].start(); } }}習題一、填空題1、 同步方法 同步語句塊 。2、 類型 。3、 同步對象。二、選擇題1、(C)。2、(C)。任務33任務實訓1、publicclassApp33_sxa{ publicstaticvoidmain(String[]args){ Transfert=newTransfer(); Originbox1=newOrigin(t); Goalbox3=newGoal(t); box1.start(); box3.start(); }}classOriginextendsThread{ //發球機 publicTransferts; publicOrigin(Transferq){ts=q;} publicvoidrun(){ synchronized(ts){ //鎖定傳輸類對象 for(inti=1;i<7;i++){ ts.Getball(i); System.out.println("傳球機得到的球->"+Transfer.box); } } }}classGoalextendsThread{ //接球機 publicTransferts; intx=0; publicGoal(Transferq){ts=q;} publicvoidrun(){ synchronized(ts){ //鎖定傳輸類對象 for(inti=1;i<7;i++){ x=ts.Putball(); //接球機得球 System.out.println("傳球機送出的球->"+Transfer.box); } } }}classTransfer{ //傳球機 staticintbox=0; booleanbol=false; //協調Getball和Putball publicvoidGetball(intv){ while(bol==true){ try{ wait(); }catch(InterruptedExceptione){} } box=v; bol=true;notifyAll(); } publicintPutball(){ while(bol==false){ //bol內無球就不接。等待 try{ wait(); }catch(InterruptedExceptione){} } bol=false;notifyAll(); //被喚醒或bol內有球,便接球 returnbox; }}任務實訓2、publicclassApp33_sxb{ publicstaticvoidmain(String[]args){ Transfert=newTransfer(); //定義傳球機對象變量與對象 Originbox1=newOrigin(t); //定義發球機對象變量與對象 Goalbox3=newGoal(t); //定義接球機對象變量與對象 box1.start(); //啟動Origin線程 box3.start(); //啟動Goal線程 }}classOriginextendsThread{ //發球機 publicTransferts; publicOrigin(Transferq){ts=q;} publicvoidrun(){ for(inti=1;i<7;i++){ ts.Getball(i); } }}classGoalextendsThread{ //接球機 publicTransferts; publicGoal(Transferq){ts=q;} publicvoidrun(){ for(inti=1;i<7;i++){ ts.Putball(); } }}classTransfer{ //傳球機 staticintbox=0; //定義box數組中,準備放球 booleanbol=false; //協調Getball和Putball publicsynchronizedvoidGetball(intv){ //發球,Getball是被同步的方法 while(bol==true){ try{ wait(); }catch(InterruptedExceptione){} } box=v;bol=true; System.out.println("傳球機得到的球->"+box); notifyAll(); } publicsynchronizedvoidPutball(){ //接球,Putball是被同步的方法 while(bol==false){ try{ wait(); }catch(InterruptedExceptione){} } bol=false; System.out.println("傳球機送出的球->"+box); notifyAll(); }}習題選擇題1、(D)。2、(C)。任務34任務實訓1、importjava.awt.event.*;importjava.io.*;importjava.util.regex.Pattern;importpag23.App23_ss;publicclassApp25_sxaextendsApp23_ssimplementsActionListener{ FileWriterfw;…… @Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub…… if(arg0.getSource()==bt){ //是"提交"按鈕…… if(ck.CheckMobile(stra)){ //驗證手機是否合法 if(ck.CheckEmail(strb)){ //驗證郵箱是否合法 ta.setText("提交成功"); //下面是文件部分 try{ //指定寫入文件,以追加的方式寫入 FileWriterfw=newFileWriter("文件/Registration.txt",true); fw.write("\r\n"); //寫入換行 fw.write(jtxm.getText()); //將姓名寫到文件中 fw.write('\t'); fw.write(stra); //將手機號寫到文件中 fw.write('\t'); fw.write(strb); //寫入Email fw.close(); }catch(IOExceptione){} //以上是文件部分 } …… }}任務實訓2、importjava.awt.event.*;importjava.io.*;importjava.util.regex.Pattern;importpag23.App23_ss;publicclassApp25_sxaextendsApp23_ssimplementsActionListener{…… //省略部分同上 @Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub…… //省略部分同上 if(arg0.getSource()==bt){ //是"提交"按鈕 char[]ch=newchar[1024]; intx; Stringstra=jta.getText(); Stringstrb=jtb.getText(); RegexTestck=newRegexTest(); if(ck.CheckMobile(stra)){ //驗證手機是否合法 if(ck.CheckEmail(strb)){ //驗證郵箱是否合法 try{ //指定讀入文件 FileReaderfd=newFileReader("文件/Registration.txt"); x=fd.read(ch); //按字符讀入,長度賦給x stra=newString(ch,0,x); //將char轉換為String }catch(FileNotFoundExceptione){} catch(IOExceptione){} if(stra.indexOf(jtxm.getText())==-1){ //驗證是否有重名 ta.setText("提交成功"); try{ //在指定文件的尾部寫入 FileWriterfw=newFileWriter("文件/Registration.txt",true); fw.write("\r\n"); //寫入換行 fw.write(jtxm.getText());//寫入姓名 fw.write('\t'); fw.write(stra); //寫入手機號 fw.write('\t'); fw.write(strb); //寫入Email fw.close(); }catch(IOExceptione){} } else ta.setText("有重名不能提交"); } else ta.setText("Email格式出錯"); } else ta.setText("手機號格式出錯"); } }}習題一、填空題1、 字符 。2、 "\r\n"。3、"\\" "/"。二、選擇題1、(A)任務35任務實訓1、importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;publicclassApp35_sxaextendsJFrameimplementsActionListener{Containerc=getContentPane();JButtonbt=newJButton("查看");JTextAreatf=newJTextArea();JScrollPanejsp=newJScrollPane(tf);inti=0; //簽名者編號Stringstra,strb;publicApp35_sxa(){setSize(400,200);c.add(jsp);tf.setBounds(160,130,90,20);c.add(bt,BorderLayout.SOUTH);bt.addActionListener(this);setVisible(true);}@Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub stra=""; tf.setText(null); try{ FileReaderfr=newFileReader("文件/sig.txt"); BufferedReaderbfr=newBufferedReader(fr); while((strb=bfr.readLine())!=null){ stra+=stra+strb+"\n"; } tf.setText(stra); fr.close(); } catch(IOExceptione1){} } publicstaticvoidmain(String[]arguments){App35_sxahead=newApp35_sxa();}}任務實訓2、importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;publicclassApp35_sxbextendsJFrameimplementsActionListener{ Containerc=getContentPane(); JButtonbta=newJButton("查看"); JButtonbtb=newJButton("清除"); JTextAreatf=newJTextArea(); JPanelpan=newJPanel(); JScrollPanejsp=newJScrollPane(tf); inti=0; //簽名者編號 Stringstra,strb; publicApp35_sxb(){ setSize(400,200); c.add(jsp);// tf.setBounds(160,130,90,20); pan.add(bta); pan.add(btb); c.add(pan,BorderLayout.SOUTH); bta.addActionListener(this); btb.addActionListener(this); setVisible(true); } @Override publicvoidactionPerformed(ActionEventarg0){ //TODOAuto-generatedmethodstub stra=""; tf.setText(null); if(arg0.getSource()==bta){ try{ FileReaderfr=newFileReader("文件/sig.txt"); BufferedReaderbfr=newBufferedReader(fr); while((strb=bfr.readLine())!=null){ stra+=stra+strb+"\n"; } tf.setText(stra); fr.close(); }catch(IOExceptione1){} } if(arg0.getSource()==btb){ FileWriterfw; //Writer使用也可 i++; try{ fw=newFileWriter("文件/sig.txt"); //指定目標文件寫入 fw.write(""); //寫入文件 fw.close(); } catch(IOExceptione){} } } publicstaticvoidmain(String[]arguments){ App35_sxbhead=newApp35_sxb(); }}習題一、填空題1、 flush() 。2、 Data Calendar 。3、 定義對象 獲得或設定 。4、 Reader 。二、選擇題1、(D)。任務36任務實訓1、importjava.io.FileInputStream;importjava.io.FileOutputStream;importjava.io.IOException;publicclassApp36_sxa{ publicstaticvoidmain(String[]args)throwsIOException{ FileInputStreamfin=newFileInputStream("文件/z.rar"); //打開源文件,讀 FileOutputStreamfout=newFileOutputStream("文件/w.rar"); //打開目標文件,寫 System.out.println("filesize="+fin.available()); //輸出文件大小 byte[]ba=newbyte[1]; while(fin.read(ba)!=-1){ fout.write(ba); } System.out.println("filecopyedsuccess"); fin.close(); fout.close();}}任務實訓2、importjava.io.*;publicclassApp36_sxb{ charlineSep=System.getProperty("line.separator").charAt(0);//得到一個行結尾 String[]products={"黃魚","大米","雞蛋","牛肉"}; double[]prices={180,5.6,17,56.3}; int[]units=newint[]{1,8,3,5}; Stringname="職工食堂"; publicvoidJsqdOutput()throwsIOException{ FileOutputStreamfout=newFileOutputStream("文件"+File.separator +"Data.txt"); //帶擴展名 DataOutputStreamDataOut=newDataOutputStream(fout); DataOut.writeChars(name); //寫入姓名 DataOut.writeChar(lineSep); //寫入換行 for(inti=0;i<products.length;i++){ DataOut.writeChars(products[i]); //寫入產品名稱 DataOut.writeChar('\t'); //寫一個空格 } DataOut.writeChar(lineSep); //寫入換行 for(inti=0;i<prices.length;i++){ DataOut.writeDouble(prices[i]); //寫入價格 } for(inti=0;i<prices.length;i++){ DataOut.writeInt(units[i]); //寫入食品數量 } fout.close(); DataOut.close(); } publicvoidJsqdInput()throwsIOException{ FileInputStreamfin=newFileInputStream("文件\\Data.txt"); DataInputStreamDataIn=newDataInputStream(fin); StringBuffertheName=newStringBuffer(); StringBufferproduct=newStringBuffer(); doubletot=0; charchr; while(DataIn.available()>0){ while((chr=DataIn.readChar())!=lineSep) theName.append(chr); System.out.println("公司:"+theName); System.out.print("\t"); while((chr=DataIn.readChar())!=lineSep){ product.append(chr); } System.out.println(product); System.out.print("價格\t"); for(inti=0;i<prices.length;i++){ prices[i]=DataIn.readDouble(); System.out.print(prices[i]+"\t"); } System.out.println(); System.out.print("數量\t"); for(inti=0;i<prices.length;i++){ units[i]=DataIn.readInt(); System.out.print(units[i]+"\t"); } System.out.println(); System.out.print("貨款\t"); for(inti=0;i<prices.length;i++){ System.out.print(prices[i]*units[i]+"\t"); tot=tot+prices[i]*units[i]; } System.out.print("\n合計\t"); System.out.println(tot); } DataIn.close(); } publicstaticvoidmain(String[]args)throwsIOException{ App36_sxbfrm=newApp36_sxb(); try{ frm.JsqdOutput(); frm.JsqdInput(); } catch(IOExceptione){} }}習題一、填空題1、 只讀 。2、 讀取 寫入 。二、選擇題1、(B)。任務37實訓任務1、importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjavax.swing.*;classStudentimplementsSerializable{ Stringstr; publicvoidSaveDatas(App37_sxasd){ //定義序列化方法 str=sd.textArea.getText().trim(); } publicvoidLoadDatas(App37_sxatar){ //定義反序列化。 tar.textArea.setText(str);//將該對象中的內容加載到tar的對象中 }}classApp37_sxaextendsJFrame{ JLabellab=newJLabel("學號姓名年齡電話"); JButtonbt1=newJButton("儲存當前的信息"),bt2=newJButton("讀出保存的信息"); StudentslObj; JTextAreatextArea=newJTextArea(); publicApp37_sxa(){ setSize(400,200); setTitle("任務實訓A"); Containerc=this.getContentPane(); c.setLayout(null); bt1.setBounds(250,100,130,28); bt2.setBounds(250,50,130,28); lab.setBounds(13,10,150,40); textArea.setBounds(13,50,222,90); c.add(lab); c.add(bt1); c.add(bt2); c.add(textArea); bt1.addActionListener(newActionListener(){ //按鈕事件處理 publicvoidactionPerformed(ActionEvente){ bt1_actionPerformed(e); //進入序列化 } }); bt2.addActionListener(newActionListener(){ //按鈕事件處理 publicvoidactionPerformed(ActionEvente){ bt2_actionPerformed(e);//進入反序列化 } }); setVisible(true); } voidbt1_actionPerformed(ActionEvente){ //保存對象的狀態 try{ FileOutputStreamtar=newFileOutputStream("data.ser"); ObjectOutputStreamouts=newObjectOutputStream(tar); slObj=newStudent(); slObj.SaveDatas(this); outs.writeObject(slObj); outs.flush(); outs.close(); }catch(IOExceptiona){ System.out.println(a); } } voidbt2_actionPerformed(ActionEvente){ //恢復對象的狀態 try{ FileInputStreamsour=newFileInputStream("data.ser"); ObjectInputStreamins=newObjectInputStream(sour); slObj=(Student)ins.readObject(); ins.close(); }catch(IOExceptionb){ System.out.println(b); }catch(ClassNotFoundExceptionecp){ } if(slObj!=null) slObj.LoadDatas(this); } publicstaticvoidmain(Stringargs[]){ App37_sxamainFrame=newApp37_sxa(); }}實訓任務2、importjava.awt.*;importjava.awt.event.*;importjava.io.*;importjava.util.StringTokenizer; importjavax.swing.*;classStudentimplementsSerializable{ intID; //學號 Stringname; //姓名 intage; //年齡 Stringtelephone; //電話 Stringstr; publicvoidSaveDatas(App37_sxbsd){ //定義序列化方法 Stringid,na,ag,tel; str=sd.textArea.getText().trim(); StringTokenizerst=newStringTokenizer(str); id=st.nextToken(); na=st.nextToken(); ag=st.nextToken(); tel=st.nextToken(); ID=Integer.parseInt(id); name=na; age=Integer.parseInt(ag); telephone=tel; } publicvoidLoadDatas(App37_sxbtar){ //定義反序列化。 tar.tfa.setText(""+ID); tar.tfb.setText(name); tar.tfc.setText(""+age); tar.tfd.setText(telephone); str=tar.slObj.ID+""+tar.slO+""+tar.slObj.age +""+tar.slObj.telephone; tar.textArea.setText(str); //將該對象中的內容加載到tar的對象中 }}classApp37_sxbextendsJFrame{ JLabellab=newJLabel("學號姓名年齡電話"); JLabellaba=newJLabel("學號"); JTextFieldtfa=newJTextField(); JLabellabb=newJLabel("姓名"); JTextFieldtfb=newJTextField(); JLabellabc=newJLabel("年齡"); JTextFieldtfc=newJTextField(); JLabellabd=newJLabel("電話"); JTextFieldtfd=newJTextField(); JButtonbt1=newJButton("儲存留言"),bt2=newJButton("讀出留言"); StudentslObj=newStudent(); JTextAreatextArea=newJTextArea(); publicApp37_sxb(){ setSize(400,300); setTitle("任務實訓B"); Containerc=this.getContentPane(); c.setLayout(null); bt1.setBounds(250,100,130,28); bt2.setBounds(250,50,130,28); lab.setBounds(13,10,150,40); textArea.setBounds(13,50,222,90); c.add(lab); c.add(bt1); c.add(bt2); c.add(textArea); laba.setBounds(13,160,50,20); tfa.setBounds(50,160,60,20); c.add(laba); c.add(tfa); labb.setBounds(140,160,50,20); tfb.setBounds(180,160,90,20); c.add(labb); c.add(tfb); labc.setBounds(13,210,50,20); tfc.setBounds(50,210,60,20); c.add(labc); c.add(tfc); labd.setBounds(140,210,50,20); tfd.setBounds(180,210,90,20); c.add(labd); c.add(tfd); bt1.addActionListener(newActionListener(){ //按鈕事件處理,序列化 publicvoidactionPerformed(ActionEvente){ slObjFun(); //序列化方法 } }); bt2.addActionListener(newActionListener(){ //按鈕事件處理 publicvoidactionPerformed(ActionEvente){ antislObjFun(); //反序列化方法 } }); setVisible(true); } voidslObjFun(){ //定義序列化方法 try{ FileOutputStreamtar=newFileOutputStream("文件/myObj.j"); ObjectOutputStreamouts=newObjectOutputStream(tar); slObj.SaveDatas(this); outs.writeObject(slObj); //執行序列化寫出 outs.flush(); outs.close(); }catch(IOExceptionecp){ } } voidantislObjFun(){ //定義反序列化方法 try{ FileInputStreamsour=newFileInputStream("文件/myObj.j"); ObjectInputStreamins=newObjectInputStream(sour); //指定數據從sour中讀 slObj=(Student)ins.readObject(); //得到對象并轉換 ins.close(); }catch(IOExceptionecp){ }catch(ClassNotFoundExceptionecp){ } if(slObj!=null) slObj.LoadDatas(this);//為對象裝入數據 } publicstaticvoidmain(String[]args){ App37_sxbframe1=newApp37_sxb(); }}習題一、填空題1、 對象序列化 。2、二、選擇題1、(D)。任務38任務實訓1、importjava.awt.*;importjava.awt.event.*;importjavax.swing.JFrame;publicclassApp38_sxaextendsJFrameimplementsMouseMotionListener{ intfx,fy,tx,ty; //出發點和終點 Graphics2Dg2; publicApp38_sxa(){ Containerc=getContentPane(); c.setLayout(null); setSize(500,400); c.setBackground(Color.cyan); addMouseMotionListener(this); setVisible(true); } publicvoidmouseMoved(MouseEvente){ fx=e.getX(); fy=e.getY(); //決定首次畫的起點 } publicvoidmouseDragged(MouseEvente){ tx=e.getX(); ty=e.getY(); //拖到點為終點 Graphicsg=getGraphics(); g2=(Graphics2D)g; g2.setColor(Color.red); //設置筆色 g2.setStroke(newBasicStroke(3.0f)); //設置筆寬,它是Graphics2D的方法 g2.drawLine(fx,fy,tx,ty); fx=tx; fy=ty; //終點為新的起點 } publicstaticvoidmain(String[]args){ App38_sxaframe=newApp38_sxa(); }}任務實訓2、繪制多邊形importjava.awt.*;importjavax.swing.*;importpag22.App22_ss;importpag40.*;importjava.awt.event.*;publicclassApp38_tzextendsApp22_ssimplementsActionListener{ …… //省略部分同App38_tz publicApp38_tz(){ …… jbd.addActionListener(this); } @Override publicvoidactionPerformed(ActionEvente){ //TODOAuto-generatedmethodstub …… if(bol){ …… if(e.getSource()==jbd) key=3; …… } if(!bol){ …… if(e.getSource()==jbd) key=7; …… }…… } classMydpanelextendsJPanel{ publicvoidpaint(Graphicsg){…… int[]px={300,350,400}; int[]py={150,50,150}; switch(key){…… case3: g.setColor(Color.pink); g.drawPolygon(px,py,3); break;…… case7: g2.setColor(Color.cyan); g2.fill3DRect(300,100,70,50,true); break;…… } } }}習題一、填空題1、 左上角 右 下 。2、 面板 、 窗口 Canvas類對象 。二、選擇題1、(D)。任務39任務實訓1、importjava.awt.*;importjavax.swing.*;publicclassApp39_sxaextendsJFrame{ Mypanelmp=newMypanel(); Containerc=getContentPane(); publicApp39_sxa(){ setSize(900,700); c.add(mp); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } publicstaticvoidmain(String[]args){App39_sxaframe=newApp39_sxa();}}classMypanelextendsJPanel{ Imageimage; publicMypanel(){ image=Toolkit.getDefaultToolkit().getImage("圖片/fox.png"); } publicvoidpaint(Graphicsg){ Graphics2Dg2=(Graphics2D)g; g2.shear(1,0); //圖像底邊向右移動一個y高度的像素 g2.drawImage(image,0,90,200,140,this); g2.shear(-1,0); //圖像底邊向左移動一個y高度的像素 g2.drawImage(image,390,90,200,140,this); g2.shear(-1,0); g2.drawImage(image,790,90,200,140,this); g2.shear(1,0); g2.rotate(Math.toRadians(10)); //轉10度 g2.drawImage(image,400,200,200,140,this); g2.rotate(Math.toRadians(10)); //再轉10度 g2.drawImage(image,400,200,200,140,this); g2.rotate(Math.toRadians(10)); //又轉10度 g2.drawImage(image,400,200,200,140,this); }}任務實訓2、importjava.awt.*;importjava.awt.event.*;importjavax.swing.*;publicclassApp39_sxbextendsJFrameimplementsActionListener{//sxc Mypanelmp=newMypanel(); Containerc=getContentPane(); Timertim=newTimer(800,this); booleanbl=false; inta=1; publicApp39_sxb(){ setSize(800,368); c.add(mp); tim.start(); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setVisible(true); } publicstaticvoidmain(String[]args){ App39_sxbframe=newApp39_sxb(); } publicvoidactionPerformed(ActionEventarg0){ if(arg0.getSource()==tim){ if(bl==false){ a=-1; bl=true; }else{ a=1; bl=false; } Graphicsg=getGraphics(); paint(g); } } classMypanelextendsJPanel{ Imageimage; publicMypanel(){ image=Toolkit.getDefaultToolkit().getImage("圖片/fox.png"); } publicvoidpaint(Graphicsg){ Graphics2Dg2=(Graphics2D)g; g2.shear(a,0); if(bl==false) g2.drawImage(image,100,90,200,140,this); else g2.drawImage(image,550,90,200,140,this); } }}習題選擇題1、(C)。任務40任務實訓1/*App40-sxa訪問時間服務器,端口8888*/publicclassSimTimeClient{publicstaticvoidmain(String[]argv){try{SockettimeSocket=newSocket("",8888); try{ InputStreamtimeStream=timeSocket.getInputStream(); OutputStreamtimeOutStream=timeSocket.getOutputStream(); PrintWriterpw=newPrintWriter(timeOutStream,true); Scannerinfo=newScanner(timeStream); StringinfoLine="Serversays:"+info.nextLine(); System.out.println(infoLine); System.out.println("Clientssays:TIME"); pw.println("TIME"); infoLine=info.nextLine(); System.out.println(infoLine); System.out.println("Clientssays:EXIT"); pw.println("EXIT"); infoLine=info.nextLine(); System.
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯系上傳者。文件的所有權益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網頁內容里面會有圖紙預覽,若沒有圖紙預覽就沒有圖紙。
- 4. 未經權益所有人同意不得將文件中的內容挪作商業或盈利用途。
- 5. 人人文庫網僅提供信息存儲空間,僅對用戶上傳內容的表現方式做保護處理,對用戶上傳分享的文檔內容本身不做任何修改或編輯,并不能對任何下載內容負責。
- 6. 下載文件中如有侵權或不適當內容,請與我們聯系,我們立即糾正。
- 7. 本站不保證下載資源的準確性、安全性和完整性, 同時也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。
最新文檔
- 鍍鋅焊管項目投資可行性研究分析報告(2024-2030版)
- 2025年中國暗線槽行業市場發展前景及發展趨勢與投資戰略研究報告
- 中國黃茶行業市場前景預測及投資價值評估分析報告
- 探索虛擬現實在教育領域的邊界
- 火源風險評估報告
- 未來教育中情緒管理的創新應用研究
- 2025年中國手板模型行業投資研究分析及發展前景預測報告
- 南昌膩子粉項目投資分析報告參考模板
- 2025年中國蓄能器截止閥市場調查研究及行業投資潛力預測報告
- 船備修理行業深度研究分析報告(2024-2030版)
- 單光纖光鑷數值仿真和光阱力計算的中期報告
- 一份完整的鹵菜店創業計劃書 工作計劃
- 手術物品清點手術室護理實踐指南課件
- 中國鋁業股份有限公司偃師市東溝鋁土礦礦山地質環境保護與土地復墾方案
- 2023-2024學年河南省濮陽市小學語文五年級期末通關考試題附參考答案和詳細解析
- 國語經典歌曲歌詞接龍考試題庫(180題)
- 2021年暖通工程師專業基礎考試真題及答案
- 項目信息報備表(模板)
- 壓力容器制造過程控制點一覽表
- 《干部履歷表》填寫樣本-1999年
- 工程建設EHS管理協議
評論
0/150
提交評論