简体   繁体   English

简单的员工记录 java 程序 GUI 删除按钮

[英]Simple employee records java program GUI remove button

I have a program GUI that the user enters ID#, First Name, Last Name, Salary, Start Date.我有一个程序 GUI,用户可以输入 ID#、名字、姓氏、薪水、开始日期。 After the user enters this information into a text area for each information need, the users clicks the add button which stores the information into an arrayList. After clicking add, the user presses a "list" button to output all the information entered into a Panel.用户将此信息输入到每个信息需要的文本区域后,用户单击将信息存储到 arrayList 的添加按钮。单击添加后,用户按下“列表”按钮 output 将所有输入到面板的信息.

Array list to store users data:存储用户数据的数组列表:

public class EmploymentRecords extends javax.swing.JFrame {

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

Remove Button code:删除按钮代码:

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 Data: 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;

Heres my problem: I want the user to be able to enter an id in the text area of the GUI where the program checks if that ID is entered before and totally removes all the data from the output screen and arraylist using just the ID.这是我的问题:我希望用户能够在 GUI 的文本区域中输入一个 ID,程序会检查之前是否输入过该 ID,并仅使用该 ID 完全删除 output 屏幕和 arraylist 中的所有数据。 The code I entered above is not working for me and when I press the remove button nothing happens.我在上面输入的代码对我不起作用,当我按下删除按钮时没有任何反应。

Please help as i would appreciate this... Thanks!请帮忙,我将不胜感激...谢谢!

You are missing some code to share.您缺少一些要共享的代码。 But lets suppose your "add" functionality is working.但是让我们假设您的“添加”功能正在运行。 Lets also suppose that "String id = txtID.getText();"我们还假设“String id = txtID.getText();” will be able to get the id for you as a string.将能够以字符串形式为您获取 id。 An obvious mistake is "if (idCheck = true)", as in java you compare with "==" So maybe you can try to fix it that way and report the answer.一个明显的错误是“if (idCheck = true)”,如在 java 中,您将其与“==”进行比较,因此也许您可以尝试以这种方式修复它并报告答案。

What you have done works alright for single entity objects within an ArrayList (like ArrayList<String> or ArrayList<Integer> for example) but not so good for multi-entity objects like what you have in your Data class. In other words, each element within your ArrayList is holding an instance of a class and all members related to it, not just a simple String or an Integer.您所做的工作适用于 ArrayList 中的单个实体对象(例如ArrayList<String>ArrayList<Integer> ),但对于数据class 中的多实体对象则不太好。换句话说,每个ArrayList 中的元素包含 class 的实例以及与其相关的所有成员,而不仅仅是一个简单的字符串或 Integer。

You will need to go a wee bit deeper in order to actually acquire the ID of any particular Data object instance for comparison to what someone has supplied within a GUI, for example:您需要更深入一点 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.");
    }
}

You will notice the clearFieldsInForm();你会注意到clearFieldsInForm(); method use in the above code.上面代码中使用的方法。 This method would just set all pertinent form fields to Null String ("") which is essentially nothing:此方法只会将所有相关的表单字段设置为 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