我正在尝试制作一个有 3 个课程的应用程序。控制器(主类)、SerialHandler 和 MainWindow,它是使用 NetBeans Gui Builder 创建的 JFrame 窗体。
public class Controller {
SerialHandler serialHandler;
MainWindow mainWindow;
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
Controller controller = new Controller();
controller.initializeSystem(controller);
}
public void initializeSystem(Controller _controller){
mainWindow = new MainWindow(_controller);
mainWindow.setVisible(true);
}
public void anotherMethod(){
mainWindow.setMyLabelText("Hello");
}
}
所以问题是,如果我这样做并且来自 SerialHandler 类的事件调用 anotherMethod(),则 setMyLabelText 方法不起作用,但如果我从 initializeSystem() 调用它;有用。
现在,如果我在主窗口中声明主窗口,则从另一个方法()中看不到主窗口实例。
如果我在 main 之外声明 mainWindow 对象并尝试从 main 上下文中使用它的方法,我不能因为 mainWindow 对象已在非静态上下文之外声明。
任何人都可以帮助我或至少指出我正确的方向吗?
谢谢!