3つのクラスがあるアプリを作ろうとしています。Controlller(メインクラス)、SerialHandler、およびNetBeansGuiBuilderで作成されたJFrameフォームであるMainWindow。
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()から呼び出す場合です。できます。
ここで、メイン内でメインウィンドウを宣言すると、mainWindowインスタンスはanotherMethod()から表示されません。
mainWindowオブジェクトをmainの外部で宣言し、そのメソッドをmainコンテキストから使用しようとすると、mainWindowオブジェクトが非静的コンテキストの外部で宣言されているため、使用できません。
誰かが私を助けてくれますか、少なくとも私を正しい方向に向けることができますか?
ありがとう!