简体   繁体   中英

Java swing timer does not set visibility of JPanel

I write a card game with server and client (multi client). And I need to check every second if game is started or game is shut down. So I write a java.swing.timer to check it. But it doesn't set visibility of JPanel when game is started. (Sorry for bad English)

That is code segment

public void checkGameState() {

    ActionListener action;

    timer = new Timer(delay, (ActionEvent e) -> {
        try {
            boolean isEnd = checkIfGameIsClose();
            if (isEnd == true) {
                System.out.println("---->"+isEnd);
                mainGamePanel.setVisible(false);
                waitRoomPanel.setVisible(true);
                availability_button.setText("Ready");
                availability_button.setBackground(Color.red);
                availability_status = false;
            }

        } catch (IOException ex) {
            Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
        }

        try {
            boolean isStart = checkIfGameIsOpen();
            if (isStart == true) {
                System.out.println("---->"+isStart);
                mainGamePanel.setVisible(true);
                waitRoomPanel.setVisible(false);
                clientConnectToServer.isEndTheGame();
            }
        } catch (IOException ex) {
            Logger.getLogger(ClientFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    });
    timer.start();


}

public boolean checkIfGameIsClose() throws IOException {
    boolean isClosedGame = clientConnectToServer.checkifGameIsEnd();
    System.out.println(isClosedGame + " ---> Close");
    return isClosedGame;
}

public boolean checkIfGameIsOpen() throws IOException {
    boolean gameIsStart = clientConnectToServer.startGame();
    System.out.println(gameIsStart + "---> open");
    return gameIsStart;
}

Then game is started method checkIfGameIsOpen() return true. Then game is shut down method checkIfGameIsClose() return true.

mainGamePanel is Panel what should appear when game is started.

Problems and possible solutions:

  1. You change the visibility state of JPanels but don't notify the container that holds them that you've done this. You should call revalidate() and repaint() on this container after making all such changes. The first, revalidate, tells the container and its layout managers to re-layout all contained components, and the second, repaint, requests that the JVM re-draw the container and all its children. This will help rid the container of "dirty" pixels.
  2. Much better would be to use a CardLayout to swap visibility, then you wouldn't need what I mention above. The tutorial can be found here: CardLayout tutorial .
  3. You don't tell us if your debug println's are showing.
  4. Your current solution is to use intermittent polling of the server's game state, a brittle kludge solution. Much better is to use a notification solution where the server notifies all clients of changes in game state, and then the clients can act on this notification.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM