简体   繁体   English

Java Swing计时器未设置JPanel的可见性

[英]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. 所以我写了一个java.swing.timer来检查它。 But it doesn't set visibility of JPanel when game is started. 但是,它不会在游戏启动时设置JPanel的可见性。 (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. 然后启动游戏,方法checkIfGameIsOpen()返回true。 Then game is shut down method checkIfGameIsClose() return true. 然后关闭游戏方法checkIfGameIsClose()返回true。

mainGamePanel is Panel what should appear when game is started. mainGamePanel是Panel,游戏开始时应显示。

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. 您更改了JPanels的可见性状态,但不通知已保存它们的容器。 You should call revalidate() and repaint() on this container after making all such changes. 进行所有此类更改后,应在此容器上调用revalidate()repaint() 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. 第一个重新验证,告诉容器及其布局管理器重新布局所有包含的组件,第二个重新绘制,请求JVM重新绘制容器及其所有子组件。 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. 更好的方法是使用CardLayout交换可见性,那么您将不需要我上面提到的内容。 The tutorial can be found here: CardLayout tutorial . 可以在这里找到该教程: CardLayout教程
  3. You don't tell us if your debug println's are showing. 您没有告诉我们您的调试println是否正在显示。
  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. 更好的方法是使用通知解决方案,其中服务器将游戏状态的更改通知所有客户端,然后客户端可以对此通知采取行动。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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