2015-11-07 58 views
0

請問,我怎樣才能在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 
+1

雖然我不太清楚你在問什麼,但我不認爲你應該用那種方法初始化那個數組。每次按下該按鈕,都會創建一個新列表。您不應該將患者信息添加到已有的列表中嗎? – Voldemort

+0

向我們展示定義此方法的類。 – Voldemort

+0

@Voldemort檢查代碼,發佈患者分類和患者分類_has-a_ Date Class進行驗證,所以我不會發布它 –

回答

0

所以,首先,你方法是創建一個新的患者陣列,然後在每次調用時添加一個新患者。這根本不會有用。您需要創建一個且只有一個陣列,並使用它來存儲您的患者。

轉到您的PatientRecordUI類,並添加一個新的領域是這樣的:

public class PatientRecordUI extends javax.swing.JFrame { 

    private ArrayList<Patient> patientsArray = new ArrayList<Patient>(); 

現在,在您jButton1ActionPerformed()方法,去除這一部分:

ArrayList<Patient> obj = new ArrayList<Patient>(); 
    obj.add(patient); 

,取而代之的是

patientsArray.add(patient); 

現在你的程序應該存儲正確的患者LY。

您要做的下一件事是創建一種方法來檢索患者數據。我想這是在你的jButton2ActionPerformed()方法中應該發生的事情。

要檢索患者數據,首先需要基於fileNumber屬性找到它的方法。我的猜測是,用戶輸入使用第十一文本框的文件數量,因此數量會

int numberRequested = Integer.parseInt(jTextField11.getText()); 

既然你存儲在數組中的病人,應重複每一個病人,並尋找一個有一個文件數字等於numberRequested。一旦找到患者,您只需獲取他們的信息並在界面中顯示。

檢索患者數據的一種方法是在Patient類中放置一些吸氣劑。否則,您可以將這些字段公開。由你決定。

我不會詳細說明如何執行這些最新步驟 - 這應該足以讓您走上正軌。

+0

謝謝你,你幫助我很多!用於檢索病人數據我使用與文件編號相同的數組索引,因此第一個病人的文件號爲0 –

0

當你聲明方法內的變量將是局部的方法。

在這種情況下,只要你點擊Add Patient按鈕,它將創建一個新的數組列表。所以它會重置較早的值。

,而不用聲明在方法層面obj的,你應該在一流水平的聲明,以便它不會被重置,所有其他方法可以訪問它(要)

+0

哪裏應該聲明obj?再次檢查代碼;我發佈了整個課 –