簡體   English   中英

簡單的員工記錄 java 程序 GUI 刪除按鈕

[英]Simple employee records java program GUI remove button

我有一個程序 GUI,用戶可以輸入 ID#、名字、姓氏、薪水、開始日期。 用戶將此信息輸入到每個信息需要的文本區域后,用戶單擊將信息存儲到 arrayList 的添加按鈕。單擊添加后,用戶按下“列表”按鈕 output 將所有輸入到面板的信息.

存儲用戶數據的數組列表:

public class EmploymentRecords extends javax.swing.JFrame {

ArrayList <Data> Output = new ArrayList <Data>();

刪除按鈕代碼:

private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                          
    
    int index;
    String id = txtID.getText();
    boolean idCheck = Output.contains(id);

    if (idCheck = true){
        index = Output.indexOf(id);
        Output.remove(index);
        lblError.setText("Employee found and has been removed.");
    }
    else {
        lblError.setText("Employee not found. Please try again.");
    }

class 資料:

class Data {
    String id, firstName, lastName, salary, startDate;
    Data (String _id, String _firstName, String _lastName, String _salary, String _startDate) {
        id = _id;
        firstName = _firstName;
        lastName = _lastName;
        salary = _salary;
        startDate = _startDate;

這是我的問題:我希望用戶能夠在 GUI 的文本區域中輸入一個 ID,程序會檢查之前是否輸入過該 ID,並僅使用該 ID 完全刪除 output 屏幕和 arraylist 中的所有數據。 我在上面輸入的代碼對我不起作用,當我按下刪除按鈕時沒有任何反應。

請幫忙,我將不勝感激...謝謝!

您缺少一些要共享的代碼。 但是讓我們假設您的“添加”功能正在運行。 我們還假設“String id = txtID.getText();” 將能夠以字符串形式為您獲取 id。 一個明顯的錯誤是“if (idCheck = true)”,如在 java 中,您將其與“==”進行比較,因此也許您可以嘗試以這種方式修復它並報告答案。

您所做的工作適用於 ArrayList 中的單個實體對象(例如ArrayList<String>ArrayList<Integer> ),但對於數據class 中的多實體對象則不太好。換句話說,每個ArrayList 中的元素包含 class 的實例以及與其相關的所有成員,而不僅僅是一個簡單的字符串或 Integer。

您需要更深入一點 go 才能實際獲取任何特定數據 object實例的ID ,以便與某人在 GUI 中提供的內容進行比較,例如:

private void btnRemoveActionPerformed(java.awt.event.ActionEvent evt) {                                          
    String id = txtID.getText();

    boolean found = false;

    for (Data data : Output) {
        if (data.id.equals(id) {
           found = true;
           Output.remove(data);
           clearFieldsInForm();
           break;
        }
    }
    if (found) {
        lblError.setText("Employee was successfully removed.");
    }
    else {
        lblError.setText("Invalid ID! Employee not found! Please try again.");
    }
}

你會注意到clearFieldsInForm(); 上面代碼中使用的方法。 此方法只會將所有相關的表單字段設置為 Null String (""),這實際上什么都沒有:

private void clearFieldsInForm() {
    txtID.setText("");
    txtFirstName.setText("");
    txtLastName.setText("");
    txtsalary.setText("");
    txtStartDate.setText("");
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM