该项目的目标是创建两个类:学生类和GUI类。 Student类包含名称,地址,余额和专业。我有所有创建的Student和GUI类,我的问题是数组和循环。我需要让Student类数组和相关的计数器变量成为静态变量,每次用户输入Student类的信息(name,address,balance,major)时,它都会向JTextArea添加另一个Student对象。实质上,每次按下JButton时,JTextArea都会使用新学生的信息进行更新,而不会删除旧信息。任何帮助做这项工作将不胜感激!
我的代码:
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);
}
}
要存储学生,您可以静态地(使用具有固定大小的数组)或动态地(使用诸如qazxsw poi之类的集合)来存储。
在这两种方式中,您的SecondAssignment类都需要存储此元素并填写创建的学生。
静态方式
如果需要大小为50的静态数组,只需使用以下命令声明:
List
然后在执行的操作中,您可以保存您的学生:
private Student[] myStudents = new Student[50];
private int currentIndice = 0;
此解决方案的主要缺点是您的阵列具有固定的大小,因此您无法处理超过50名学生。
动态的方式
相反,您可以将元素添加到将不时增加的集合中。首批进口包:
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());
}
然后声明你的名单:
import java.util.ArrayList;
import java.util.List;
在你的private List<Student> myStudents = new ArrayList<>;
方法:
actionPerformed
您可以使用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());
}
检索学生人数,并从收集功能中受益(从列表中删除,在列表中搜索等)。
编辑
正如Nick Rippe所述,如果echoStudent标签必须处理所有学生的列表,更简单的方法是保持当前文本而不是每次重置:用myStudents.size()
替换echoStudent.setText(student1.toString());
就可以完成这项工作(echoStudent.setText(echoStudent.getText() + "\n" + student1.toString());
是一个新的转义序列)线)。
• 如何在两个不直接连接的类之间建模和实现关联类? (C++)
• 如何使用 Thymeleaf 在 Javascript 中访问具有一组另一个对象的 Spring Boot POJO
• SQLKata 使用 Include/IncludeMany 返回复杂对象
• 需要有关如何使用 flutter app 启动和 rfid 考勤的指导
• How to solve this issue Segment fault (core dumped)?
• 如何使用HTML和其他语言将多个输入相加并将结果放入另一个输入? [关闭]
• 我写代码作为这个问题的解决方案,但它给出了错误的输入,我找不到正确的解决方案