2013-03-23 77 views
0

class1-座標的動態值已保存,並且希望將值返回給類2的函數。將數值從一個類別返回到另一個類別

public class NewJFrame extends javax.swing.JFrame {  

    public NewJFrame() { 
     initComponents(); 
    }  

    ArrayList<Integer> al= new ArrayList<Integer>(); 
    private void jLabel1MouseClicked(java.awt.event.MouseEvent evt) { 
     int x= evt.getX(); 
     int y= evt.getY();   
     al.add(x); 
     al.add(y); 
     System.out.println("hi1"+al); 
    } 
    public ArrayList<Integer> cord() 
    { 
      System.out.println("hi2"+al); 
     return al; 
    } 

    public static void main(String args[]) { 

     java.awt.EventQueue.invokeLater(new Runnable() { 

      public void run() { 
       new NewJFrame().setVisible(true); 
      } 
     }); 
    }  

    private javax.swing.JLabel jLabel1;  
} 

類class2

public class JavaApplication13 { 

    public void hello() 
    { 
     NewJFrame j= new NewJFrame(); 
     ArrayList<Integer> al=j.cord(); 
     System.out.println("hi3"+al); 
    } 

    public static void main(String[] args) { 
     JavaApplication13 j= new JavaApplication13(); 
     j.hello(); 
    } 
} 

我想返回x的值和y座標的Class1到Class2中。但它顯示爲空。幫幫我。

回答

0

In NewJFrame class make ArrayList<Integer> al public static and in JavaApplication13 read it System.out.println("hi3"+NewJFrame.al.get())。當然,這個問題只是一個來自四萬億方法的問題,如何做到這一點,你的代碼有一些問題:你不能(我認爲你是從ArrayList獲得一些價值)做"hi3"+al

相關問題