java Gui从另一个类访问arrayList

时间:2015-01-20 18:21:52

标签: java swing

我想从文本字段中读取数据,将其保存在第一个类的数组中 在第二节课我想在文本字段中显示这些数据,但我的程序没有返回任何内容

这里是第一堂课

import java.util.ArrayList;
public class Patient extends javax.swing.JFrame {
int emg;
String clas;
String day;
int sex;
String name;
String cls;
int count;
boolean[] Assignbed = new boolean[31];
ArrayList<Integer> list1;
ArrayList list2=new ArrayList();
ArrayList list3;
    /**
     *
     */
    public Patient() {
        this.list3 = new ArrayList<Integer>();
        this.list1 = new ArrayList<Integer>();
        initComponents();
    }
    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
    AWellcome a=new AWellcome();
    a.setVisible(true);
    dispose();
    }                                          

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Name();
    }                                                                               

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Level();
    }                                                                                                                                                                   

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    occupation();
    }                                        

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Sex();
    }                                                                                                                      

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Age();
    }                                        

    private void jButton7ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Payment();
    }                                        

    private void jButton8ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Reserve();
    }                                        

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Date();
    }                                                                                                                                                                

    private void jButton9ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Department();
    }                                        
public ArrayList<Integer> getList() {
       return list1;
   }
public void Department(){
if(jComboBox3.getSelectedIndex()==0){
emg=Integer.parseInt(jTextField20.getText());
list1.add(emg);
}
else if(jComboBox3.getSelectedIndex()==1){
list2.add(jTextField20.getText());
}
else if(jComboBox3.getSelectedIndex()==2){
list3.add(jTextField20.getText());
}
}
public void Date(){
day=jTextField23.getText();
jTextField7.setText(day);
}
public void occupation(){
cls=jTextField44.getText();
jTextField15.setText(cls);
}
public void count(){
jTextField20.setText(Integer.toString(count));
}
public void fulllevel1(){
jTextField25.setText("level1_Full");
}
public void fulllevel2(){
jTextField25.setText("level2_Full");
}
public void fulllevel3(){
jTextField25.setText("Level3_Full");
}
public void Level(){
if(jComboBox2.getSelectedIndex()==0){
jTextField9.setText("Level 1");
}
else if(jComboBox2.getSelectedIndex()==1){
jTextField9.setText("Level 2");
}
else if(jComboBox2.getSelectedIndex()==2){
jTextField9.setText("Level 3");
}
}
public void Age(){
clas=jTextField43.getText();
jTextField13.setText(clas);
}
public void Sex(){
if(jComboBox1.getSelectedIndex()==0){
jTextField17.setText("Male");
}else if(jComboBox1.getSelectedIndex()==1)
{
jTextField17.setText("Female");
}
}
public void Name(){
name=jTextField42.getText();
jTextField11.setText(name);   
}
public void Payment(){
if(jComboBox2.getSelectedIndex()==0){
jTextField19.setText("50 Birr/Day");
}
else if(jComboBox2.getSelectedIndex()==1){
jTextField19.setText("30 Birr/Day");
}
else if(jComboBox2.getSelectedIndex()==2){
jTextField19.setText("20 Birr/Day");
}
}
public void Reserve()
    {
        if ( jComboBox2.getSelectedIndex() == 0 )
        {
            firstlevel();
        }
        else if(jComboBox2.getSelectedIndex()== 1)
        {
            secondlevel();
        }
         else if(jComboBox2.getSelectedIndex()== 2)
        {
            thirdlevel();
        }
    }

 public void firstlevel() // 
    {
        for ( count = 1; count <= 10; count++ )
        {
            if ( Assignbed[count] == false )  // if false, then a bed available for assignment
            {
                Assignbed[count] = true;  // assign bed
                count();
                break;
            }
            else if ( Assignbed[10] == true ) // If assignedbed[10] is true then first level is full
            {
                fulllevel1();

            }
        }
    }   

 public void secondlevel() //
    {
        for ( count = 11; count <= 20; count++ )
        {
            if ( Assignbed[count] == false ) //
            {
                Assignbed[count] = true; //
               count();
                break;
            }
            else if ( Assignbed[20] == true ) // 
            {
                fulllevel2();

            }
        }
    }
  public void thirdlevel() // 
    {
        for ( count = 21; count <= 30; count++ )
        {
            if ( Assignbed[count] == false ) 
            {
               Assignbed[count] = true;
               count();
                break;
            }
            else if ( Assignbed[30] == true ) 
            {
                fulllevel3();

            }
        }
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Patient().setVisible(true);
            }
        });
    }


} 

第二课

import java.util.ArrayList;
public class Staff extends javax.swing.JFrame {
private Patient p;
    public Staff() {
        initComponents();
    }                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
    Department1();
    }                                        
    public ArrayList<Integer> Department1(){
    p=new Patient();
    ArrayList<Integer> list1 = p.getList();  
        return list1;
    } 
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Staff().setVisible(true);
            }
        });
    }

}

2 个答案:

答案 0 :(得分:2)

你的问题在这里:

p=new Patient();  // ***** here *****
ArrayList<Integer> list1 = p.getList(); 

您正在创建 患者对象,并希望它与原始患者对象具有相同的状态。不要这样做 - 而是将原始患者(即保存感兴趣的数据的患者)的引用传递到新的类中:

import java.util.ArrayList;

public class Staff extends javax.swing.JFrame {
    private Patient p;

    public Staff(Patient p) {
        this.p = p;
        initComponents();
    }                      

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                         
        Department1();
    }                                        

    public ArrayList<Integer> Department1(){
        // p=new Patient();
        ArrayList<Integer> list1 = p.getList();  
        return list1;
    } 

无论你做什么,请忽略任何告诉你让列表静态的人,因为这样做,你将忽略所有面向对象的编程原则。

答案 1 :(得分:0)

您可以将其声明为static并使用简单的类名称点变量名称来访问它,或者创建get函数,如:

public List getList(){
    return <yourList>'
}