2011-12-23 100 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

相关问题