设计和实现一个“学生”类,它有能力不断接受输入并创建多个学生对象

时间:2014-02-19 20:07:18

标签: swing class user-interface awt actionevent

这个项目的目标是创建两个类:学生类和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);

    }

}

1 个答案:

答案 0 :(得分:3)

要存储学生,您可以静态(使用具有固定大小的数组)或动态(使用诸如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是新行的转义序列。)