我正在尝试在创建患者时添加患者 ID,该 ID 每次都会增加一。但是当运行我的代码时,它也会增加所有其他 id 的 id。
JButton btnCp = new JButton("Create Patient");
btnCp.addActionListener(new ActionListener() {//add action listener to btnCp (Create Patient Button)
public void actionPerformed(ActionEvent arg0) {
name = pateintNameTextField.getText();//reads entered text
address = patientAddressTextField.getText();
phone = patientPhoneTextField.getText();
pateintNameTextField.setText("");//sets fields back to empty ("")
patientAddressTextField.setText("");
patientPhoneTextField.setText("");
Patient.setID(Patient.patientNum +1);
Patient patient = new Patient(name,address,phone);//adds the Patient to the patient class
patientListModel.addElement(patient);//adds patient to patientLst
Patient.aPatientList.add(patient);
我的患者类别如下:
public class Patient implements Serializable{
public static int patientNum;
public String patientName;
public String patientAddress;
public String patientPhone;
public static ArrayList<Patient> aPatientList = new ArrayList<Patient>();
public Patient(String n, String a, String p){
patientName = n;
patientAddress = a;
patientPhone = p;
}
public static void setID(int id){
patientNum = id;
}
public static int getID(){
return patientNum;
}
public void setName(String n){
patientName = n;
}
public String getName(){
return patientName;
}
public void setAddress(String a){
patientAddress = a;
}
public String getAddress(){
return patientAddress;
}
public void SetPhoneNum(String p){
patientPhone = p;
}
public String getPhoneNum(){
return patientPhone;
}
public String toString() {
return "ID: " +getID() + " Name: "+ getName() +" Address: "+getAddress() +" Phone Number: "+getPhoneNum();
}
}
我也将患者写入 jList。提前致谢