1

このプロジェクトの目標は、学生クラスと GUI クラスの 2 つのクラスを作成することです。Student クラスには、名前、住所、残高、専攻が含まれます。Student クラスと GUI クラスをすべて作成しましたが、問題は配列とループにあります。Student クラスの配列と関連するカウンター変数を静的変数にする必要があり、ユーザーが Student クラスの情報 (名前、住所、残高、専攻) を入力するたびに、別の Student オブジェクトが JTextArea に追加されます。基本的に、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

生徒を保存するには、静的 (固定サイズの配列を使用) または動的 ( などのコレクションを使用) のいずれかで行うことができます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 に答える