删除我的数组列表中的所有项目并将 JTextarea 设置为空
Removing all items in my arraylist and setting the JTextarea to empty
我有一个 JButton
用于添加数据,第二个用于查看数据,两者都运行良好,但第三个 JButton
用于从 ArrayList
中删除所有数据并设置 JTextarea
清空。我尝试将 JTextArea
设置为“”和 listof.remove
,但是当我单击 JButton
时没有任何反应。这是代码。
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、name 和其他字段,但如果名称很长,它会将其他数据推离它们的标题,当我更改选项卡和 space 时,短名称也会从它们的标题中获取数据。这是输出和标题的代码
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();
}
有什么正确的方法可以做到这一点。任何帮助将不胜感激。
您正在删除您的 new Student()
,但单击 "clear" 按钮时他从未被添加。你必须做 listof.clear()
else if(ev.getSource()==clear){
listof.clear();//removes all entries from the list
display.setText("");
}
}
为了对齐 headers 我建议使用 JTable
但如果您不想使用它,您可以尝试以下操作:
在 Student
class 中创建 static
方法来对齐 headers。在这种情况下使用静态,因为它不会绑定单个 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
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);
}
}
我有一个 JButton
用于添加数据,第二个用于查看数据,两者都运行良好,但第三个 JButton
用于从 ArrayList
中删除所有数据并设置 JTextarea
清空。我尝试将 JTextArea
设置为“”和 listof.remove
,但是当我单击 JButton
时没有任何反应。这是代码。
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、name 和其他字段,但如果名称很长,它会将其他数据推离它们的标题,当我更改选项卡和 space 时,短名称也会从它们的标题中获取数据。这是输出和标题的代码
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();
}
有什么正确的方法可以做到这一点。任何帮助将不胜感激。
您正在删除您的 new Student()
,但单击 "clear" 按钮时他从未被添加。你必须做 listof.clear()
else if(ev.getSource()==clear){
listof.clear();//removes all entries from the list
display.setText("");
}
}
为了对齐 headers 我建议使用 JTable
但如果您不想使用它,您可以尝试以下操作:
在 Student
class 中创建 static
方法来对齐 headers。在这种情况下使用静态,因为它不会绑定单个 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
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);
}
}