2017-04-06 16 views
0

我有一個JButton添加數據和第二查看數據,兩者都可以正常使用,但第三JButtonArrayList刪除所有數據,並設置JTextarea空。我試着將JTextArea設置爲「」和listof.remove,但是當我點擊JButton時沒有任何反應。這是代碼。刪除我的ArrayList中的所有項目和設置的JTextArea空

public class thehandler implements ActionListener{ 

     @Override 
      public void actionPerformed(ActionEvent ev){ 

       Student student=new Student(); 

      if(ev.getSource()==addData){ 


       if(listof.size() <= 4){ 

       try{ 

        student.setEng(Double.parseDouble(eng.getText().toString())); 
        student.setScience(Double.parseDouble(sci.getText().toString())); 
        student.setSocial(Double.parseDouble(soc.getText().toString())); 
        student.setMath(Double.parseDouble(math.getText().toString()));} 
       catch(Exception e){ 

      JOptionPane.showMessageDialog(null, "one or more of the subject fields contains invalid inputs,PLEASE NUMBER REQUIRED, please check it", "error", JOptionPane.ERROR_MESSAGE); 

      return; 
       } 

        student.setId(sidInput.getText().toString()); 
        student.setSn(snameInput.getText().toString()); 
        student.setLn(lnameInput.getText().toString()); 


        listof.add(student); 


        sidInput.setText(""); 
        snameInput.setText(""); 
        lnameInput.setText(""); 
        eng.setText(""); 
        math.setText(""); 
        soc.setText(""); 
        sci.setText(""); 
       }else 
       { 
        // if the size exceeds required alert the user 
        JOptionPane.showMessageDialog(null, "The maximum Number of Student that can be added is 50 ", 
          "ERROR",JOptionPane.ERROR_MESSAGE); 

        // and set the id field to no input state abd clear all fields 
        sidInput.setEditable(false); 
         sidInput.setText(""); 
        snameInput.setText(""); 
        lnameInput.setText(""); 
        eng.setText(""); 
        math.setText(""); 
        soc.setText(""); 
        sci.setText(""); 
        return; 
       } 


       Collections.sort(listof, new sortAverage()); 

      } 
      else if(ev.getSource()==viewData){ 
       display.setText(student.header()); 
       display.setFont(new Font("serif",Font.BOLD,14)); 

       int x=0; 
       Student lastStudent =null; 
       for(Student of:listof){ 

        if(lastStudent == null ||Double.compare(of.getAverage(), lastStudent.getAverage()) !=0){ 
        x++; 
        } 
       display.append(of.toString()+"\r\t\r "+of.getPosition(x)+"\r\n"); 
       lastStudent=of; 

      } 
      } 
     else if(ev.getSource()==clear){ 
      listof.remove(student); 
      display.setText(""); 
      } 
     } 
    } 

而且數據輸出不一致。我有一個studentid,名稱和其餘的字段,但如果名稱很長,它會將其他數據推到遠離標題的位置,當我更改標籤和空格時,短名稱也會從標題中獲取數據。這是輸出和標題代碼

public String header(){ 
      // returns the header of the ressult 
     return "STUDENTID \r \t STUDENT FULLNAME \r\tENGLISH \t MATH \t SOCIAL \t SCIENCE " 
       + "\tTOTAL \t AVERAGE \t POSITIONS\n"; 

      } 
      @Override 
      public String toString(){ 
       // display all the values of the student 
      return "\r "+getSid()+"\r\t"+getSname()+"\r "+getLname()+ 
        "\t\r \t "+getEng()+"\t\r "+getMath()+"\t\r "+getSocial()+ 
        "\t\r "+getScience()+"\t\r "+getTotalMark()+ 
        "\t\r "+getAverage(); 
      } 

是否有任何適當的方式來做到這一點。任何幫助將不勝感激。

回答

0

您正在刪除new Student(),但點擊「清除」按鈕時他從未被添加過。你所要做的listof.clear()

else if(ev.getSource()==clear){ 
     listof.clear();//removes all entries from the list 
     display.setText(""); 
     } 
    } 

對於對準頭我會建議使用JTable 但是,如果你不想使用它,你可以嘗試以下方法:

Student類創建static方法對齊標題。因爲那時它沒有綁定一個Student在這種情況下使用靜態:

public static String getAlignedHeader(int maxLength){ 
    // returns the header of the ressult 
    String tmpStrg = " STUDENT FULLNAME "; 
    int currentLength = tmpStrg.length(); 
    for(int i = 0; i < (maxLength-currentLength); i++){ 
     tmpStrg+=" "; 
    } 
    return "STUDENTID \r \t"+tmpStrg+"\r\tENGLISH \t MATH \t SOCIAL \t SCIENCE "+ "\tTOTAL \t AVERAGE \t POSITIONS\n"; 
} 

然後使用它:

display.setFont(new Font("serif",Font.BOLD,14)); 

int maxLength = Integer.MAX_VALUE; 
for (Student student : listof) { 
    maxLength = Math.min(maxLength, (student.getSname().length()+getLname().length())); 
} 
display.setText(Student.getAlignedHeader(maxLength)); 
//append student as you already do 
+0

它的工作。謝謝,現在我的問題是讓輸出顯示沒有任何「推翻」。 – Whiteag

+0

爲什麼不使用'JTable',那麼你不必自己處理間距?! –

+0

更新了我的答案。但重新思考使用'JTable'! –

0
else if(ev.getSource()==viewData){ 
       // display.setText(student.header()); 
       display.setFont(new Font("serif",Font.BOLD,14)); 

       int x=0; 
      Student lastStudent =null; 
       for(Student of:listof){ 

       if(lastStudent == null ||Double.compare(of.getAverage(), lastStudent.getAverage()) !=0){ 
        x++; 

        } 

      Object [] row ={of.getSid(),of.getFullname(),of.getEng(),of.getMath(),of.getSocial(),of.getScience(),of.getTotalMark(),of.getAverage(),of.getPosition(x)}; 

       lastStudent=of; 
       tableModel.addRow(row); 
      } 
      } 
+0

不要添加代碼作爲答案,但用它更新你的問題。但不要覆蓋原始代碼。將此代碼發佈到** Update JTable **部分 –