Java 按下按钮后如何添加数据,数据是构造函数格式?

Java 按下按钮后如何添加数据,数据是构造函数格式?,java,swing,Java,Swing,第一爪哇 package a; abstract class Employee{ private String staffID; private String name; public Employee(String staffID, String name) { this.staffID = staffID; this.name = name; } public String getStaffID() { return staffID; } public void

第一爪哇

package a;
abstract class Employee{
private String staffID;
private String name;

public Employee(String staffID, String name) {
    this.staffID = staffID;
    this.name = name;
}
public String getStaffID() {
    return staffID;
}

public void setStaffID(String staffID) {
    this.staffID = staffID;
}

public String getName() {
    return name + "EM";
}

public void setName(String name) {
    this.name = name;
}
}
第二爪哇

 package a;
 class Manager extends Employee{
 private int salary;

 public Manager(String staffID, String name, int salary) {
    super(staffID, name);
     this.salary = salary;
 }

 }
图形用户界面java

   package a;
   import java.io.*;
   java.awt.*;
   java.awt.event.*;
   import javax.swing.*;

   public class Gui extends JFrame implements ActionListener{
     private JLabel l1, l2;
    private JTextField i1,i2;
    private JButton b1;
    String string = "";
    public Gui(){
    super("Title");

    Container container = getContentPane();
    container.setLayout(null);

    l1 = new JLabel("Staff ID:");
    l1.setBounds(65, 31, 46, 14);
    container.add(l1);

    l2 = new JLabel("Name:");
    l2.setBounds(65, 115, 46, 14);
    container.add(l2);

    i1 = new JTextField(20);
    i1.setBounds(120,31,250,20);
    container.add(i1);      
    i2 = new JTextField(20);
    i2.setBounds(120,115,250,20);
    container.add(i2);      


    b1 = new JButton("add m");
    b1.setBounds(30,390,120,30);
    container.add(b1);
    addMButton.addActionListener(new ActionListener() { 
        public void actionPerformed(ActionEvent e) { 
            Employee emp[] = new Manager(i1.getText(),i2.getText());

            string = "Staff Id:" + Emp[].getstaffID  +"Name: "+ Emp[].getName();


            try{
                String data = "Staff.txt";
                BufferedWriter reader = new BufferedWriter(new FileWriter(data));
                reader.write(string);
                reader.newLine();
                reader.close();

            }catch (IOException E){
                System.out.println("Error is " + E);
            }
        }
    });         

    setSize(600,600enter code here);
    setVisible(true);

}   
public static void main (String args[]){
    Gui application = new Gui();
    application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
}

    }
}
}
例如,Id是A1234567 我叫汤姆·李。 构造函数如何插入这些数据

我是一个初学者,我知道有一些常见的错误
希望有人能纠正我的错误。。非常感谢

您的错误是什么?另外,为什么要使用Employee
Employee emp[]
数组?我在线程“AWT-EventQueue-0”java.lang.NullPointerException中遇到异常,因为我不仅插入了一条记录,如果使用Employee emp[]是否有误?
[]
将其作为
Employee
类对象的数组。如果您不想要
Employee
类对象的数组,请删除它。我建议您首先学习Java的基础知识,了解数组是如何工作的,什么是类、对象、如何声明变量等。如果您想使用数组,但一次只能存储和检索一个对象,请尝试将
emp[]
替换为
emp[0]
这一行后面的所有地方
emp[]=new Manager(i1.getText(),i2.getText())然后查看输出是否可以复制您得到的错误?另外,为什么要使用Employee
Employee emp[]
数组?我在线程“AWT-EventQueue-0”java.lang.NullPointerException中遇到异常,因为我不仅插入了一条记录,如果使用Employee emp[]是否有误?
[]
将其作为
Employee
类对象的数组。如果您不想要
Employee
类对象的数组,请删除它。我建议您首先学习Java的基础知识,了解数组是如何工作的,什么是类、对象、如何声明变量等。如果您想使用数组,但一次只能存储和检索一个对象,请尝试将
emp[]
替换为
emp[0]
这一行后面的所有地方
emp[]=new Manager(i1.getText(),i2.getText())然后查看输出是否可以复制