嘿我试图从另一个类访问我在主方法中创建的对象,但是当我在其他类中引用它时,它不被识别。经过一番研究后,我认为这与访问修饰符有关,但我试图将该对象公开为仅对评论出现“删除无效修饰符”。任何指针?从其他类访问在主要方法中的对象
对不起,这是如此基本,但我只是一个初学者,我发现这件事很艰难。
对不起没有提及!我正在用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();
你在写什么语言?你能提供一个简单的例子吗? – 2011-12-23 14:46:47
请发布一些代码,或者所有人都会猜测。 – 2011-12-23 14:47:35
你可以发布其他类的更多代码吗?您打算如何使用其他类中的Mainframe内容窗格进行操作?如果你想添加一些东西到用户界面,在大型机上执行。 – 2011-12-23 15:01:11