2016-07-06 64 views
0

我希望從其他類獲得初始化對象,而無需創建新對象或使用靜態對象。那可能嗎 ?在不使用靜態的情況下獲取初始化對象

我儘量讓上飛的例子不知道如果它的工作原理:

package.display  
public class Display extends JFrame{  
    public Display(){ 
     initUI(); 
    } 
    public void initUI(){ 
     // initialize panel1 
     Panel1 panel1 = new Panel1(); 
     // setting window adding panel1 
     this.setSize(400,400); 
     this.add(panel1); 
     this.setVisible(true); 
    } 
    public static void main(String[] args){ 
     new Display(); 
    } 
} 
package.panle1 
public class Panel1 extends JPanel{ 
    Canvas canvas; 
    public Display(){ 
     initUI(); 
    } 
    public void initUI(){ 
     // initialize canvas and panel2 
     canvas = new Canvas(); 
     canvas.setSize(new Dimension(200, 200); 
     Panel2 panel2 = new Panel2(); 
     // adding canvas and panel2 
     this.add(canvas, BorderLayout.North); 
     this.add(panle2, BorderLayout.South); 
    } 
    public Canvas getCanvas(){ 
     return canvas; 
    } 
} 
package.panle2 
public class Panel2 extends JPanel{  
    public Display(){ 
     initUI(); 
    } 
    public void initUI(){ 
     // just 1 button 
     JButton btn1 = new JButton(); 
     btn1.addActionListener(new ActionListener() { 
      // create new JPanel and center on canvas 
      public void actionPerformed(ActionEvent arg0){ 
       JPanel canvasPanel = new JPanel(); 
       canvasPanel.setSize(100,100); 
       canvasPanel.setLocationRelativeTo(
       // and here is my PROBLEM 
       // how i get this panel1 canvas object without creating new ? 
       // getter getCanvas() dont work with existing object instance 
       // i want exactly the object which is created when its called 
       // from display and not the way i have to make new Panel1 
     }); 
     this.add(btn1); 
    } 
} 

(我的問題是在代碼中最後一部分描述)

我所知道的唯一的解決方法是靜態的,並且工作正常。單詞 靜態是非常好的,我會使用它遍佈整個地方,因爲它非常方便 訪問所有。 有了我不知道的反思。我只看到了一個例子,你必須創建一個對象的新實例,並且我想要。

不久,我想要去可能是3D編程,並從OpenGL開始。在那裏,我讀到你必須隨身攜帶物品,並且不能像lwgl那樣使用static這個詞。

這就是爲什麼我問這個問題,看看我有哪些可能性來解決這樣的問題。我希望任何人都可以幫助和thx。

+1

通行證它到'''Panel2'''構造函數; '''Panel2 panel2 = new Panel2(this);''' –

回答

0

另一種方式是創建Panel1的final實例,所以你就可以隨時隨地訪問該對象的getCanvas()方法類,甚至裏面的內部類的:

public class Panel2 extends JPanel{  
    final Panel1 panel1=new Panel1(); 
    public Display(){ 
     initUI(); 
    } 
    public void initUI(){ 
     // just 1 button 
     JButton btn1 = new JButton(); 
     btn1.addActionListener(new ActionListener() { 
      // create new JPanel and center on canvas 
      public void actionPerformed(ActionEvent arg0){ 
       JPanel canvasPanel = new JPanel(); 
       canvasPanel.setSize(100,100); 
       canvasPanel.setLocationRelativeTo(
       // and here you have access to the canvas of panel1 
     }); 
     this.add(btn1); 
    } 
} 
+0

對不起,但我認爲final也不適用於我的問題。 – medialcore

+0

對不起,但我認爲最後也不適合我的問題。 OFC。我可以訪問getCanvas(),但我也可以訪問創建一個普通的對象。但這正是問題所在。因爲我想從 - > Display(新Panel1) - > Panel1(新Canvas和Panel2) - >訪問已經創建的對象,然後從Panel2訪問由Panel1提供的Canvas,而不使用關鍵字「new」再次因爲我做了一個新的對象。 – medialcore

相關問題