[英]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: 问题和可能的解决方案:
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.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.