Accessing Swing Components of Another Class

I have two classes gameWindowand gameEngine. The main method is in the class gameWindow, as well as the swinging GUI code. Now I want to access swing's components gameEngine. How to do it? I always get an error cannot find symbolwhen I try this is normal. I tried to make the components public, but no luck.

In addition, I tried to create an instance gameWindow, but it did not help either. It compiled without any errors, but I had a BIG error at runtime (which I don’t even see, the command prompt scrolls to the limit).

CFM !!

PS: I do not think that posting the code here will help.

+3
source share
3 answers

gameWindow gameEngine, . , GameWindow GameEngine. - :

 public class GameEngine{
      GameWindow window;

      public GameEngine(GameWindow gm){
           window = gm;
      }
      //rest of your code
 }

 public class GameWindow(){

     //At the point where you create the GameEngine
     GameEngine ge = new GameEngine(this);
     //rest of your code


}

GameEngine GameWindow, .

, . GameEngine GameWindow , . , - Observer, GameWindow GameEngine, . GameEngine Swing GameWindow.

+2

gameEngine contructor getter/setter.

, gameWindow, gameEngine, , gameWindow.

gameEngine:


gameWindow theMainWindow = null; public gameEngine(gameWindow mainWindow) { theMainWindow = mainWindow; }
+1

The answer to Vincent is correct, but includes creating an instance of GameWindow, which, as stated in the message, gives an error. I would advise redirecting the error stack trace to a file, read it and fix the runtime error at runtime, and then follow its instructions.

+1
source

Source: https://habr.com/ru/post/1772893/


All Articles