2011-12-23 207 views
2

嘿我試圖從另一個類訪問我在主方法中創建的對象,但是當我在其他類中引用它時,它不被識別。經過一番研究後,我認爲這與訪問修飾符有關,但我試圖將該對象公開爲僅對評論出現「刪除無效修飾符」。任何指針?從其他類訪問在主要方法中的對象

對不起,這是如此基本,但我只是一個初學者,我發現這件事很艱難。

對不起沒有提及!我正在用Java編寫。這是我有:

public static void main(String[] args) { 

    Mainframe mainframe = new Mainframe(); 
    mainframe.initialiseMF();  
    LoginPanel lp = new LoginPanel(); 
    mainframe.add(lp); 
} 

public class Mainframe extends JFrame { 

public Mainframe() { 
    // Set size of mainframe 
    setBounds(0, 0, 500, 500); 

} 

public void initialiseMF(){ 
    // Get the size of the screen 
    Dimension dim = Toolkit.getDefaultToolkit().getScreenSize(); 

    // Determine the new location of the mainframe 
    int w = getSize().width; 
    int h = getSize().height; 
    int x = (dim.width-w)/2; 
    int y = (dim.height-h)/2; 

    // Move the mainframe 
    setLocation(x, y); 
    setVisible(true); 
} 

}

我試圖做這個說法在另一大類:

Container content = mainframe.getContentPane(); 
+1

你在寫什麼語言?你能提供一個簡單的例子嗎? – 2011-12-23 14:46:47

+1

請發佈一些代碼,或者所有人都會猜測。 – 2011-12-23 14:47:35

+0

你可以發佈其他類的更多代碼嗎?您打算如何使用其他類中的Mainframe內容窗格進行操作?如果你想添加一些東西到用戶界面,在大型機上執行。 – 2011-12-23 15:01:11

回答

0

記住,主機對象是本地到main()方法,是靜態的。你不能在課堂外訪問它。

也許這會更清潔一些。

public class Mainframe extends JFrame{ 
    public Mainframe(){ 
      initialiseMF(); 
    } 

    public void initialiseMF(){ 
      //do ur inits here 
    } 

} 

那麼做到這一點,

public class TheOtherClass{ 

    private Mainframe mainFrame; 

    public TheOtherClass(){ 
     mainFrame = MainFrame.mainFrame; //although I would not suggest this, it will avoid the Main.mainFrame call 
    } 

    public void otherMethodFromOtherClass(JFrame mainFrame){ 
     Container content = mainFrame.getConentPane(); 
    } 
} 
+0

感謝您的幫助!我試圖通過從一個空白框架(Mainframe)開始,根據需要添加/刪除面板來顯示不同的屏幕來切換我的程序的屏幕。我通過擴展JFrame類創建了一個Mainframe類,並編寫了一些代碼將其集中在屏幕上。 我想在我創建的大型機上添加面板。啊,我似乎通過將其更改爲Main.mainframe.getContentPane()修復它 - 有什麼辦法可以避免引用Main? – user1058210 2011-12-23 16:36:34

+0

@ user1058210:你可以看到更新後的答案。您可以在TheOtherClass – bragboy 2011-12-23 17:31:41

0

我不完全相信你所說的「對象」的意思,但我只是猜測,你的意思是可變的。

爲了使在類中聲明的變量可以從外部訪問(以某種方式,即直接或通過某種方法),它必須是成員變量而不是局部變量。

例如本地(方法本地)變量:

//local variables are the ones that are declared inside a method 
//their life and visibility is limited only within the block in which they are define. 
public static void main(String[] args) { // args is also a local variable 
    String localVar = "Access modifiers are not allowed for local variables."; 
    //the reason that the access modifiers are not allowed is because 
    //the local variables are not members of the class   
} 

它纔有意義,使任一類privateprotected,包私有,或public的成員。

例如成員變量:

class AClass { 
    private String memberVar = "A member variable can have access modifier."; 
    //the access modifier will determine the visibility of that variable. 

    public String getMemberVar() { 
     return this.memberVar; 
    } 
} 
  1. 私人:僅在類可見。你可以使用一些公開的方法使其可訪問。通常稱爲getter方法。

  2. package-private:僅在包中可見。再次,您可以通過使用某種公共方法使其可在包外訪問。

  3. protected:僅在類中可見並且它是子類(即使子類不在包中也無關緊要)。

  4. public:無處不在。

+0

中將該變量保存爲成員,我需要再次閱讀該問題。因爲它已被更新。 – 2011-12-23 15:06:35

相關問題