2014-02-19 24 views
1

此項目的目標是創建兩個班級:學生班級和GUI班級。學生類包含姓名,地址,餘額和專業。我有學生和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); 

    } 

} 
+0

你的意思是你想每次點擊提交按鈕時清除表單輸入文本?或者你想存儲所有的學生並將其全部顯示出來? – bagage

+0

對不起,我不是很清楚。我的數組可以是一個50的靜態大小,可以存儲50個「學生」,而不必在按下按鈕後清除JTextArea。 – Ben

+0

爲什麼你需要一個大小爲50的靜態數組而不是像一個簡單的List容器那樣的動態數組? – bagage

回答

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()找回學生的數量,並從中獲益收藏功能(從列表中刪除,在列表中搜索等)。

編輯

尼克Rippe說,如果echoStudent標籤必須處理所有學生的名單中,更簡單的方法是保留而不是每次都重新設置當前文本:與echoStudent.setText(echoStudent.getText() + "\n" + student1.toString());更換echoStudent.setText(student1.toString());會做例如作業(\n是新行的轉義序列)。

+0

問題的第一部分的好答案。 OP還詢問如何保持文本區域('echoStudent')與所有學生記錄(不只是最後一個)保持一致。我想提到那個簡單的修復。 –

+1

這是一個夢幻般的答案。謝謝!!我利用動態的方式和靜態的方式讓這個程序完成預定的任務,而且我絕對覺得我現在可以更好地理解這些概念。接受答案! – Ben

+1

@NickRippe我確實編輯了我的答案。 @Ben很高興你用'swing'學到了一些東西,祝你好運:D – bagage