【发布时间】:2011-12-23 14:45:06
【问题描述】:
嘿,我正在尝试从另一个类访问我在 main 方法中创建的对象,但是当我在另一个类中引用它时,它无法识别。经过一些研究,我认为这与访问修饰符有关,但我尝试公开对象只是为了显示“删除无效修饰符”的评论。任何指针?
很抱歉,这太基础了,但我只是一个初学者,我发现这东西很难。
抱歉没提!我正在用 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();
【问题讨论】:
-
你用什么语言写作?能举个简单的例子吗?
-
请发布一些代码,否则每个人都只是在猜测。
-
你能发布更多其他类的代码吗?您打算在其他类中使用大型机的内容窗格做什么?如果您想在 UI 中添加一些内容,请在 Mainframe 中进行。
标签: java access-modifiers