2012-12-23 63 views
0

我有一個框架,其中包含一個按鈕和文本字段。獲取ID和顯示細節在Jtable

我porpuse讀取ID號frome文本字段,當點擊按鈕時,我的表應該顯示該ID與它的名稱和標記。

我 「UserSearchFile.txt」 是這樣的:

12 joe 120 
14 ted 220 
19 alex 560 
22 julia 668 

我jButton6ActionPerformed整個代碼是這樣的:

int idS=Integer.parseInt(jTextField2.getText()); 
     final Vector data = new Vector(); 
     final Vector column = new Vector(); 
File f=new File("D:\\UserSearchFile.txt"); 
    try{ 
    FileReader fr=new FileReader(f); 
    BufferedReader br1=new BufferedReader(fr); 
    String s; 
while ((s = br1.readLine()) != null) { 
       String[] st = s.split(" "); 
       String id = st[0]; 
       String name = st[1]; 
       String mark = st[2]; 
       if (id.equals(String.valueOf(idS))) { 
try { 
      String line2; 
FileInputStream fis = new FileInputStream("D:\\UserSearchFile.txt"); 
     BufferedReader br2 = new BufferedReader(new InputStreamReader(fis)); 
     StringTokenizer st1 = new StringTokenizer(br2.readLine(), " "); 
       while (st1.hasMoreTokens()) 
       column.addElement(st1.nextToken()); 
       while ((line2 = br2.readLine()) != null) { 
         StringTokenizer st2 = new StringTokenizer(line2, " "); 
         while (st2.hasMoreTokens()) 
           data.addElement(st2.nextToken()); 
       } 
       br2.close(); 
     } catch (Exception e) { 
       e.printStackTrace(); 
     } 
       } 
      } 
     } catch (IOException ex) { 
      Logger.getLogger(MainFrame1.class.getName()).log(Level.SEVERE, null, ex); 
     } 

jTable1.setModel(new AbstractTableModel() { 

     public int getRowCount() { 
      return data.size()/getColumnCount(); 
     } 

     public int getColumnCount() { 
      return column.size(); 
     } 

     public Object getValueAt(int rowIndex, int columnIndex) { 
      return (String) data.elementAt((rowIndex * getColumnCount()) 
         + columnIndex); 
     } 
    }); 
    jTable1.setVisible(true); 

請幫幫我,告訴我怎麼寫cleany和簡單。

回答

4

首先爲變量和方法使用有意義的名稱。 jTable1,br1,br2jButton6ActionPerformed是不可接受的名稱。

然後嘗試將一個複雜的方法拆分成2個或3個操作,將它們自己拆分成2個或3個操作等。每個操作都應該是一個方法調用。例如:

private void readButtonClicked() { 
    String id = idTextField.getText(); 
    Student student = findStudentWithId(id); 
    showStudentInGUI(student); 
} 

private Student findStudentWithId(String id) { 
    List<String> lines = readLinesInFile(); 
    List<Student> students = transformLinesIntoStudents(lines); 
    Student studentWithId = findStudentWithId(students, id); 
} 

private Student findStudentWithId(List<Student> students, String id) { 
    for (Student student : students) { 
     if (student.getId().equals(id)) { 
      return student; 
     } 
    } 
    return null; 
} 

private List<Student> transformLinesIntoStudents(List<String> lines) { 
    List<Student> students = new ArrayList<Student>(lines.size()); 
    for (String line : lines) { 
     students.add(parseStudentLine(line); 
    } 
    return students; 
} 

... 
+0

根本不懂。 – Sajad

+0

你問(原文如此)「如何寫簡潔而簡單」。我的回答告訴你如何將你的代碼混亂變成乾淨,簡單和可讀的代碼。你不明白什麼? –

+0

我不明白什麼是「私人學生findStudentWithId(String id)」? 私人列表 transformLinesIntoStudents(列表行) – Sajad