In an application I am building, so far I have three classes for the user interface. I have a FrameGUI class which extends JFrame and acts as a container for two other classes I have created: StartScreenGUI and MainGameGUI - both of which extend JPanel and hold different buttons depending on the job of the interface.
FrameGUI (very simplified version):
class FrameGUI extends JFrame
{
private Container contentPane;
private StartScreenGUI screenFirst;
private MainGameGUI gameScreen;
public FrameGUI()
{
gameScreen = new MainGameGUI();
screenFirst = new StartScreenGUI();
contentPane.add(screenFirst);
}
public void swapScreen(MainGameGUI gameScreen)
{
contentPane.remove(screenFirst);
contentPane.add(gameScreen);
}
}
The problem is, in the StartScreenGUI class there is a button Continue that when clicked I want to call the swapScreen method so the user can progress to the next screen. However it cannot access the method as it isn't "aware" of the instance of the FrameGUI class defined in the main method.
Is there a way around this or an entirely different approach I could take that is much better? If possible I would like to avoid having multiple frames and just closing the top most frame when it is finished with.