0

I'm stuck on this since almost 5 hours and I cannot find any answer.

Here is the piece of code you need.

Client.java

public class Client {

    private GUI     _GUI = new GUI();

    public Client() {

    }

    public void Run() throws Exception {

        _GUI.Run();
        System.out.println("before");
        while (_GUI.isOver() == false)
        {
            System.out.println("azerty");
        }
        System.out.println("after");
        _GUI.Stop();
    }
}

GUI.java

public class GUI {

    final static String STARTPANEL      = "startpanel";
    final static String LOBBYPANEL      = "lobbypanel";
    final static String GAMEPANEL       = "gamepanel";
    final static String ENDPANEL        = "endpanel";

    private JFrame      _MainWindow     = new JFrame();
    private CardLayout  _MainLayout     = new CardLayout();
    private JPanel      _MainPanel      = new JPanel(_MainLayout);

    public GUI() {
        InitWindow();
        InitPanelLayout();

    }

    public void Run() {

    }

    public void Stop() {
        _MainWindow.dispose();
    }

    private void InitWindow() {
        _MainWindow.setTitle("Mille Borne");
        _MainWindow.setSize(1020, 920);
        _MainWindow.setResizable(false);
        _MainWindow.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
        _MainWindow.setLocationRelativeTo(null);
        _MainWindow.setVisible(true);
    }

    private void InitPanelLayout() {
        _MainWindow.add(_MainPanel);

        //_MainPanel.add(STARTPANEL);
        //_MainPanel.add(LOBBYPANEL);
        //_MainPanel.add(GAMEPANEL);
        //_MainPanel.add(ENDPANEL);
    }

    public boolean isOver() {
        if (_MainWindow.isVisible() == true) {
            return (false);
        }
        return (true);
    }

}

If I remove this line in the Client.java

System.out.println("azerty");

The program is not exiting the loop, the "after" is not displayed.

Can someone explain me what is going on ?

Thanks for your help !

sshmaxime
  • 499
  • 5
  • 15
  • My bad, this question is a duplicate. – sshmaxime Dec 14 '17 at 22:21
  • Even after fixing it, the code is wrong/bad and should be scrapped. Much better to use listeners and events know when the GUI is no longer running. Easiest to display the GUI as a *modal* JDialog and not a JFrame as this will pause the calling code until the GUI is no longer visible. If you must use a JFrame then attach a WindowListener to it. – Hovercraft Full Of Eels Dec 14 '17 at 22:34
  • Yeah, you are right. I'm beginning in Java, sorry for the bad programming style. As you can probably see, I'm from C/C++. – sshmaxime Dec 15 '17 at 00:27

0 Answers0