我有一個JButton
添加數據和第二查看數據,兩者都可以正常使用,但第三JButton
從ArrayList
刪除所有數據,並設置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();
}
是否有任何適當的方式來做到這一點。任何幫助將不勝感激。
它的工作。謝謝,現在我的問題是讓輸出顯示沒有任何「推翻」。 – Whiteag
爲什麼不使用'JTable',那麼你不必自己處理間距?! –
更新了我的答案。但重新思考使用'JTable'! –