Swing 设计和实现一个;学生“;类,该类具有持续接受输入和创建多个student对象的能力
这个项目的目标是创建两个类:一个学生类和一个GUI类。学生类包含姓名、地址、余额和专业。我创建了学生和GUI类。我的问题是数组和循环。我需要将Student类数组和关联的计数器变量设置为静态变量,每次用户输入Student类的信息(名称、地址、余额、专业)时,它都会将另一个Student对象添加到JTextArea。实际上,每次按下JButton时,JTextArea都会更新新学生的信息,而不会删除旧信息。任何帮助使这项工作将不胜感激 我的代码:Swing 设计和实现一个;学生“;类,该类具有持续接受输入和创建多个student对象的能力,swing,class,user-interface,awt,actionevent,Swing,Class,User Interface,Awt,Actionevent,这个项目的目标是创建两个类:一个学生类和一个GUI类。学生类包含姓名、地址、余额和专业。我创建了学生和GUI类。我的问题是数组和循环。我需要将Student类数组和关联的计数器变量设置为静态变量,每次用户输入Student类的信息(名称、地址、余额、专业)时,它都会将另一个Student对象添加到JTextArea。实际上,每次按下JButton时,JTextArea都会更新新学生的信息,而不会删除旧信息。任何帮助使这项工作将不胜感激 我的代码: import javax.swing.*; i
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
class Student {
private String name;
private String address;
private String balance;
private String major;
// Constructs fields
public Student(String name, String address, String balance, String major) {
this.name = name;
this.address = address;
this.balance = balance;
this.major = major;
}
public String setName(String Name) {
this.name = name;
return name;
}
public String setAddress(String address) {
this.address = address;
return address;
}
public String setBalance(String balance) {
this.balance = balance;
return balance;
}
public String setMajor(String major) {
this.major = major;
return major;
}
public String toString() {
return ("Name: " + this.name + " Address: " + this.address
+ " Balance: " + this.balance + " Major: " + this.major);
}
}
public class SecondAssignment extends JFrame implements ActionListener {
public SecondAssignment() {
setLayout(new GridLayout(6, 1, 1, 1));
// Creates TextField, TextArea, and button components
name = new JTextField();
address = new JTextField();
balance = new JTextField();
major = new JTextField();
JButton jbtSubmit = new JButton("Submit");
echoStudent = new JTextArea();
// Add TextField, TextArea, and button components to the frame
add(new JLabel("Name: "));
add(name);
add(new JLabel("Address: "));
add(address);
add(new JLabel("Balance: "));
add(balance);
add(new JLabel("Major: "));
add(major);
add(new JLabel("Submit Button: "));
add(jbtSubmit);
jbtSubmit.addActionListener(this);
add(echoStudent);
echoStudent.setEditable(false);
}
// TextFields
private JTextField name;
private JTextField address;
private JTextField balance;
private JTextField major;
// Echo TextArea
private JTextArea echoStudent;
// Listener
public void actionPerformed(ActionEvent a) {
Student student1 = new Student(name.getText(), address.getText(),
balance.getText(), major.getText());
echoStudent.setText(student1.toString());
}
public static void main(String[] args) {
SecondAssignment frame = new SecondAssignment();
frame.setTitle("Student Interface");
frame.setSize(500, 700);
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
frame.setVisible(true);
}
}
要存储学生,可以静态(使用固定大小的数组)或动态(使用集合,如
列表
)
在这两种情况下,您的第二个作业类都需要存储此元素并使用创建的学生进行填充
静态方式
如果需要大小为50的静态数组,只需使用以下命令声明它:
private Student[] myStudents = new Student[50];
private int currentIndice = 0;
然后在执行的操作中,您可以保存您的学生:
public void actionPerformed(ActionEvent a) {
Student student1 = new Student(name.getText(), address.getText(),
balance.getText(), major.getText());
//check that we have still room in the array
if (currentIndice < 50) {
myStudents[currentIndice] = student1;
// increase indice to the next available element
++currentIndice;
}
//otherwise handle error
else {
//some error management
}
echoStudent.setText(student1.toString());
}
然后申报你的名单:
private List<Student> myStudents = new ArrayList<>;
您可以使用myStudents.size()
检索学生人数,并从集合功能(从列表中删除、在列表中搜索等)中获益
编辑
正如Nick Rippe所说,如果echoStudent标签必须处理所有学生的列表,那么更简单的方法是保留当前文本,而不是每次都重置:替换
echoStudent.setText(student1.toString())使用echoStudent.setText(echoStudent.getText()+“\n”+student1.toString())编写代码>例如,code>将执行此任务(\n
是新行的转义序列)。您的意思是每次单击“提交”按钮时都要清除表单输入文本吗?或者你想把你所有的学生都存储起来并全部展示出来?对不起,我不是很清楚。我的数组可以是一个静态大小为50的数组,可以存储50个“学生”,而无需在按下按钮后清除JTextArea。为什么需要一个静态大小为50的数组,而不是像简单列表容器那样的动态数组?我想没有理由不这样做,作业只是说明数组的静态大小可以是50。这是问题第一部分的好答案。OP还要求将文本区域(echoStudent
)与所有学生记录(而不仅仅是最后一个)保持最新。我想我会提到这个简单的解决方案。这是一个很棒的答案。非常感谢。我用动态方式和静态方式让这个程序完成预期的任务,我现在肯定觉得我对这些概念有了更好的理解。回答接受@尼克·克里普:我确实据此编辑了我的答案@Ben很高兴你学到了一些东西,祝你好运
private List<Student> myStudents = new ArrayList<>;
public void actionPerformed(ActionEvent a) {
Student student1 = new Student(name.getText(), address.getText(),
balance.getText(), major.getText());
//simply add the new student to the list
myStudents.add(student1);
echoStudent.setText(student1.toString());
}