问题描述
请把以下代码student加到gui代码中:packagejavaapplication1;publicclassdemoextendsjavax.swing.JFrame{publicdemo(){initComponents();}@SuppressWarnings("unchecked")privatevoidinitComponents(){jScrollPane1=newjavax.swing.JScrollPane();jTextArea1=newjavax.swing.JTextArea();jScrollPane2=newjavax.swing.JScrollPane();jTextArea2=newjavax.swing.JTextArea();jLabel1=newjavax.swing.JLabel();jSeparator1=newjavax.swing.JSeparator();jButton1=newjavax.swing.JButton();jButton2=newjavax.swing.JButton();jButton3=newjavax.swing.JButton();jButton4=newjavax.swing.JButton();setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);jTextArea1.setColumns(20);jTextArea1.setRows(5);jScrollPane1.setViewportView(jTextArea1);jTextArea2.setColumns(20);jTextArea2.setRows(5);jScrollPane2.setViewportView(jTextArea2);jLabel1.setText("抽奖器");jButton1.setText("排序");jButton2.setText("选择");jButton3.setText("开始");jButton3.addActionListener(newjava.awt.event.ActionListener(){publicvoidactionPerformed(java.awt.event.ActionEventevt){jButton3ActionPerformed(evt);}});jButton4.setText("退出");javax.swing.GroupLayoutlayout=newjavax.swing.GroupLayout(getContentPane());getContentPane().setLayout(layout);layout.setHorizontalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addContainerGap().addComponent(jScrollPane1,javax.swing.GroupLayout.PREFERRED_SIZE,javax.swing.GroupLayout.DEFAULT_SIZE,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18,18,18).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jButton4).addComponent(jButton3).addComponent(jButton2).addComponent(jButton1)).addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED,51,Short.MAX_VALUE).addComponent(jScrollPane2,javax.swing.GroupLayout.PREFERRED_SIZE,146,javax.swing.GroupLayout.PREFERRED_SIZE)).addGroup(layout.createSequentialGroup().addGap(191,191,191).addComponent(jLabel1))).addContainerGap()).addComponent(jSeparator1,javax.swing.GroupLayout.DEFAULT_SIZE,438,Short.MAX_VALUE));layout.setVerticalGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(30,30,30).addComponent(jSeparator1,javax.swing.GroupLayout.PREFERRED_SIZE,14,javax.swing.GroupLayout.PREFERRED_SIZE).addGap(18,18,18).addComponent(jLabel1).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGroup(layout.createSequentialGroup().addGap(38,38,38).addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING,false).addComponent(jScrollPane2,javax.swing.GroupLayout.Alignment.LEADING).addComponent(jScrollPane1,javax.swing.GroupLayout.Alignment.LEADING,javax.swing.GroupLayout.DEFAULT_SIZE,359,Short.MAX_VALUE)).addContainerGap(34,Short.MAX_VALUE)).addGroup(javax.swing.GroupLayout.Alignment.TRAILING,layout.createSequentialGroup().addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED).addComponent(jButton1).addGap(53,53,53).addComponent(jButton2).addGap(47,47,47).addComponent(jButton3).addGap(51,51,51).addComponent(jButton4).addGap(61,61,61)))));pack();}//</editor-fold>privatevoidjButton3ActionPerformed(java.awt.event.ActionEventevt){//TODOaddyourhandlingcodehere:}/***@paramargsthecommandlinearguments*/publicstaticvoidmain(Stringargs[]){java.awt.EventQueue.invokeLater(newRunnable(){publicvoidrun(){newdemo().setVisible(true);}});}//Variablesdeclaration-donotmodifyprivatejavax.swing.JButtonjButton1;privatejavax.swing.JButtonjButton2;privatejavax.swing.JButtonjButton3;privatejavax.swing.JButtonjButton4;privatejavax.swing.JLabeljLabel1;privatejavax.swing.JScrollPanejScrollPane1;privatejavax.swing.JScrollPanejScrollPane2;privatejavax.swing.JSeparatorjSeparator1;privatejavax.swing.JTextAreajTextArea1;privatejavax.swing.JTextAreajTextArea2;//Endofvariablesdeclaration}student.txt内的内容有100行。以下是importjava.io.*;importjava.util.*;publicclassTest9{privatestaticList<Student>students=newArrayList<Student>();publicstaticvoidmain(String[]args)throwsIOException{//读入学生数据,保存到Student对象数组中FileReaderfin=newFileReader("Students.txt");BufferedReaderin=newBufferedReader(fin);Stringline=in.readLine();while(line!=null){line=line.trim();//去除字符串中前后空格if(line.length()>0)//检查是否是空行{Studentstudent=newStudent();student.parseStudent(line);students.add(student);}line=in.readLine();}in.close();fin.close();Studentstudent=newStudent();//按姓名查找Comparator<Student>c=newStudent.NameComparator();Collections.sort(students,c);student.setName("杨晓非");intindex=Collections.binarySearch(students,student,c);System.out.println(students.get(index));}publicstaticvoidprintStudents(){Iterator<Student>iter=students.iterator();while(iter.hasNext()){Students=iter.next();System.out.println(s);}}}classStudentimplementsComparable<Student>{privateStringid;privateStringname;privateStringdepartment;publicvoidparseStudent(Stringstr){inttokenCount;StringTokenizert=newStringTokenizer(str);tokenCount=t.countTokens();id=t.nextToken();//学号name=t.nextToken();//姓名department=t.nextToken();//学院}publicStringtoString(){Strings=id+""+name;for(intl=s.length();l<21;l++)s+="";//对齐returns+department;}publicintcompareTo(Studento){return(id.compareTo(o.id));}publicvoidsetName(StringaName){name=aName;}publicstaticclassNameComparatorimplementsComparator<Student>{publicintcompare(Students1,Students2){returns1.name.compareTo(s2.name);}}publicclassDepartComparatorimplementsComparator<Student>{publicintcompare(Students1,Students2){returns1.department.compareTo(s2.department);}}}
解决方案
本帖最后由 spring_0832 于 2010-12-01 10:22:06 编辑
解决方案二:
好多的啊,要写关键代码就可以了
解决方案三:
按钮事件监听之后通过FileInputStream输入流来读取文件防止到文本域中
解决方案四:
publicstaticStringgetFileStr(Stringpath){InputStreamin=LGDao.class.getResourceAsStream(path);Stringstr="";try{str=convertStreamToString(in);}catch(IOExceptione){e.printStackTrace();}returnstr;}publicstaticStringconvertStreamToString(InputStreamis)throwsIOException{if(is!=null){StringBuildersb=newStringBuilder();Stringline;try{BufferedReaderreader=newBufferedReader(newInputStreamReader(is,"UTF-8"));while((line=reader.readLine())!=null){sb.append(line).append("n");}}finally{is.close();}returnsb.toString();}else{return"";}}
解决方案五:
LGDao.class.getResourceAsStream(path);这一行你改下LGDao改你放txt文件附近的一个类名然后用getFileStr方法读文件返回的String用setText写进去