請問,我怎樣才能在2個內部類中共享ArrayList的相同副本? picture of the GUI,我把兩個按鈕:第一個「添加患者」,我做了它,但我必須添加到「檢索」按鈕與第一個按鈕中註冊相同的值的功能?如何在兩個內部匿名類中共享ArrayList的相同副本?
package my.firstProject;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.LayoutStyle;
import javax.swing.WindowConstants;
public class PatientRecordUI extends javax.swing.JFrame {
/**
* Creates new form PatientRecordUI
*/
public PatientRecordUI() {
initComponents();
}
private void jButton1ActionPerformed(ActionEvent evt) {
// TODO add your handling code here:
String first, second, last;
int bMonth, bDay, bYear, aMonth, aDay, aYear, fileNo;
first = jTextField1.getText();
second = jTextField2.getText();
last = jTextField3.getText();
bMonth = Integer.parseInt(jTextField4.getText());
bDay = Integer.parseInt(jTextField5.getText());
bYear = Integer.parseInt(jTextField6.getText());
aMonth = Integer.parseInt(jTextField7.getText());
aDay = Integer.parseInt(jTextField8.getText());
aYear = Integer.parseInt(jTextField9.getText());
fileNo = Integer.parseInt(jTextField10.getText());
Date birth = new Date(bMonth, bDay, bYear);
Date admissionDate = new Date(aMonth, aDay, aYear);
Patient patient = new Patient(first, second, last, fileNo, birth, admissionDate);
ArrayList<Patient> obj = new ArrayList<Patient>();
obj.add(patient);
}
private void jButton2ActionPerformed(ActionEvent evt) {
// TODO add your handling code here:
}
/**
* @param args
* the command line arguments
*/
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new PatientRecordUI().setVisible(true);
}
});
}
// Variables declaration - do not modify
private JButton jButton1;
private JButton jButton2;
private JLabel jLabel1;
private JLabel jLabel2;
private JLabel jLabel3;
private JLabel jLabel4;
private JLabel jLabel5;
private JLabel jLabel7;
private JLabel jLabel8;
private JPanel jPanel1;
private JTextField jTextField1;
private JTextField jTextField10;
private JTextField jTextField11;
private JTextField jTextField2;
private JTextField jTextField3;
private JTextField jTextField4;
private JTextField jTextField5;
private JTextField jTextField6;
private JTextField jTextField7;
private JTextField jTextField8;
private JTextField jTextField9;
// End of variables declaration
}
,這裏的病人類別:
import java.util.Calendar;
public class Patient {
private String firstName;
private String secondName;
private String lastName;
private int fileNumber;
private Date birthDate;
private Date admissionDate;
// constructor to initialize name, birth date and admission date
public Patient(String first, String second, String last, int fileNo, Date dateOfBirth, Date dateOfAdmission) {
firstName = first;
secondName = second;
lastName = last;
fileNumber = fileNo;
birthDate = dateOfBirth;
admissionDate = dateOfAdmission;
} // end Patient constructor
public int getAage() {
return Calendar.getInstance().get(Calendar.YEAR) - birthDate.getYear();
}
// convert Patient to String format
public String toString() {
return String.format("%s, %s %s File No.:%d\n Admission: %s age: %d\n\n", lastName, firstName, secondName,
fileNumber, admissionDate, getAage());
} // end method toString
} // end class Patient
這裏Date類
public class Date
{
private int month; // 1-12
private int day; // 1-31 based on month
public int year; // any year
// constructor: call checkMonth to confirm proper value for month;
// call checkDay to confirm proper value for day
}
public Date(int theMonth, int theDay, int theYear)
{
month = checkMonth(theMonth); // validate month
setYear(theYear); // could validate year
day = checkDay(theDay); // validate day
} // end Date constructor
// utility method to confirm proper month value
private int checkMonth(int testMonth)
{
if (testMonth > 0 && testMonth <= 12) // validate month
return testMonth;
else // month is invalid
{
System.out.printf(
"Invalid month (%d) set to 1.", testMonth);
return 1; // maintain object in consistent state
} // end else
} // end method checkMonth
// utility method to confirm proper day value based on month and year
private int checkDay(int testDay)
{
int daysPerMonth[] =
{ 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
// check if day in range for month
if (testDay > 0 && testDay <= daysPerMonth[ month ])
return testDay;
// check for leap year
if (month == 2 && testDay == 29 && (year % 400 == 0 ||
(year % 4 == 0 && year % 100 != 0)))
return testDay;
System.out.printf("Invalid day (%d) set to 1.", testDay);
return 1; // maintain object in consistent state
} // end method checkDay
private void setYear(int testYear)
{
year = testYear < 0 ? 0 : testYear;
}
public int getYear()
{
return year;
}
// return a String of the form month/day/year
public String toString()
{
return String.format("%d/%d/%d", month, day, year);
} // end method toString
} // end class Date
雖然我不太清楚你在問什麼,但我不認爲你應該用那種方法初始化那個數組。每次按下該按鈕,都會創建一個新列表。您不應該將患者信息添加到已有的列表中嗎? – Voldemort
向我們展示定義此方法的類。 – Voldemort
@Voldemort檢查代碼,發佈患者分類和患者分類_has-a_ Date Class進行驗證,所以我不會發布它 –