课程设计说明书(论文)用纸1.制作一个JFrame的应用
程序,设置背景色为蓝色(blue),布局管理为流式布局,该JFrame包含一个JButton,名为“关闭”,点击后关闭该JFrame,退出程序。(10分)解;importjava.awt.*;importjavax.swing.*;publicclassFlow{publicstaticvoidmain(String[]args){FlowLayoutflow=newFlowLayout();JFramef=newJFrame("Test");f.getContentPane().setLayout(flow);JButtonbutton=newJButton(“关闭关闭”)关闭button.addActionListener(newActionListener(){publicvoidactionPerformed(ActionEvente){System.exit(0);}});f.getContentPane().add(button);f.setSize(100,200);f.setBackground(Color.blue);f.setVisible(true);}}2.有下面一段Server段程序,目的是能够同时服务多个客户,客户的请求是一句话(一个String)。如果这个请求的内容是字符串"plain"的话,服务器仅将"hello"字符串返回给用户。否则将用户的话追加到当前目录的文本文件Memo.txt中(路径为"Memo.txt"),并向用户返回"OK"。注意Server并发的处理多用户,Memo.txt被共享,要求不能出现数据不一致。Server的程序如下文件Server.java:。。。。。。请根据题目的要求和现有的Server.java,UserThread.java的程序,完成类MemoController.java的程序。(10分)解:importjava.io.*;publicclassMemoController{FileOutputStreamfos;OutputStreamWriterosw;BufferedWriterbw;publicMemoController(){try{fos=newFileOutputStream("memo.txt",true);osw=newOutputStreamWriter(fos);bw=newBufferedWriter(osw);}catch(FileNotFoundExceptione){};}publicsynchronizedvoidappend(Strings){try{bw.write(s,0,s.length());bw.flush();bw.close();osw.close();fos.close();}catch(IOExceptione){}}publicstaticvoidmain(Stringargs[]){MemoControllermmc=newMemoController();mmc.append("Iamxubin");}}
1
课程设计说明书(论文)用纸
3.下面的程序A.java是一个应用程序(其源文件中还定义了另一个类B),其实现各自功能的代码没有给出,若程序的功能是在命令行把当前文件夹下的所有后缀名为.java文件显示在命令行界面,每行显示一个文件名,请完成程序后面的1、2小题,将程序补充完整。(8分)1):importjava.io9):}10):11):2):publicclassA{3):publicstaticvoidmain(Stringargs[]){…//将当前目录下所有以.java结尾的文件显示在命令行,每行显示一个文件名。12):classBimplementsFilenameFilter{19):}(1).类B实现了文件名过滤器接口13):publicbooleanaccept(Filedir,Strings){…//过滤文件名以.java结尾的文件。18):}FilenameFilter,请为接口中的accept方法编写方法体,实现以下功能:若代表文件名的第2个参数s是以字符串.java结尾,则返回true,否则返回false。if(s.endsWith(".java"))returntrue;elsereturnfalse;(2).类A中的main方法的功能是:将当前目录下所有以.java结尾的文件显示在命令行,每行显示一个文件名。请写出main方法中的所有代码。Filefile=newFile(".");String[]str1=file.list(newB());for(inti=0;i
2
课程设计说明书(论文)用纸
4.编写一个完整的JavaApplication程序。包含类Student、TestStudent,具体要求如下:(10分)⑴Student类:①属性示性别②方法对象,表示家庭地址Student(Stringname,charsex,longid):构造函数StringgetName():返回voidvoidsetId(longid):设置voidsetAddress(Stringadd):设置家庭地址name:String对象,表示一个人sex:char类型,用来表address:Stringid:long类型,表示classinfo:String对象,表示班级
setClass(Stringclassinfo):设置班级信息括、性别等上述属性
publicStringtoString():返回学生的各项信息,包