2010-04-18 76 views
1

我已經創建了一個陣列曼一類:使用數組作爲參數來

 
public main blah blah{ 
man = man[10]; 
} 

曼具有諸如

 
Man.name; 
Man.age; 
... 

在曼類,存在打開一個新的一個OnClick方法顯示其名稱和年齡的窗口。

 
public Man(){ 

    Onclick(){ 
     InfoWindow showinfo = new InfoWindow(this.getid()) // If this is Man[2] the id would be 2. 

} 

而且在信息窗口類:

 
public class InfoWindow extends JFrame{ 
    public InfoWindow(Man selectedMan){ 
     setSize(300, 200); 
     JLabel info = new JLabel(selectedMan.getname()); 
     add(info); 
     info.setVisible(true); 
    } 
} 

基本上,這是想acomplish(顯示在僞代碼),通過一個人[I]成當創建一個窗口類,顯示的信息相關對那個男人。這就是我如何實際執行它,但它不起作用,我敢肯定在某些部分我有一個誤解。

任何幫助?

實際代碼:

 
***MAN CLASS*** 
private class MouseListenerHandler extends MouseAdapter { 
     public void mousePressed(MouseEvent e) { 
      InfoWindow manShowInfo = new InfoWindow(this); Not Working. Getting "constructor not defined" 
      unitShowInfo.setVisible(true); 

     } 
    } 

*InfoWindow class* 
public class InfoWindow extends JFrame { 
    public InfoWindow(Man selectedMan){ 
     setSize(300, 200); 
     JLabel label = new JLabel(selectedMan.getName()); 
     add(label); 
     label.setVisible(true); 

    } 

And the Man[] is created in the main class. 
} 
+2

對症狀的描述將有所幫助。它不是在編譯?它拋出一個異常(哪個例外)?它只是在默默地失敗嗎?還有別的嗎? – 2010-04-18 01:47:16

回答

4

嘗試這種情況:

InfoWindow manShowInfo = new InfoWindow(Man.this); 

因爲事件偵聽器是本身的對象實例,一個普通的this指監聽器。執行Man.this將提取封閉的Man實例以傳遞到InfoWindow

+0

+1請參閱http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.8.3 – barrowc 2010-04-18 02:07:03

+0

工作!這是當OOP變得複雜的新手... – 2010-04-18 02:28:43