1

这个项目的目标是创建两个类:一个学生类和一个 GUI 类。Student 类包含姓名、地址、余额和专业。我有学生和 GUI 类都创建了我的问题是数组和循环。我需要让 Student 类数组和关联的计数器变量成为静态变量,并且每次用户输入 Student 类的信息(名称、地址、余额、专业)时,它都会向 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);

    }

}
4

1 回答 1

3

要存储学生,您可以静态(使用具有固定大小的数组)或动态(使用诸如 a 的集合List)进行存储。

在这两种方式中,您的 SecondAssignment 类都需要存储此元素并填写创建的学生。

静态方式

如果你需要一个大小为 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());
}

此解决方案的主要缺点是您的数组具有固定大小,因此您不能处理超过 50 个学生。

动态方式

相反,您可以将元素添加到会不时增加的集合中。首先导入包:

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());
}

您可以使用 检索学生人数myStudents.size(),并受益于收集功能(从列表中删除、在列表中搜索等)。

编辑

正如 Nick Rippe 所说,如果 echoStudent 标签必须处理所有学生的列表,更简单的方法是保留当前文本而不是每次都重置:替换echoStudent.setText(student1.toString());echoStudent.setText(echoStudent.getText() + "\n" + student1.toString());例如可以完成这项工作(\n是换行的转义序列)。

于 2014-02-19T20:39:43.463 回答