[英]Java swing timer does not set visibility of JPanel
我用服务器和客户端(多客户端)编写纸牌游戏。 而且我需要每秒检查一次游戏是否启动或关闭。 所以我写了一个java.swing.timer来检查它。 但是,它不会在游戏启动时设置JPanel的可见性。 (对不起,英语不好)
那就是代码段
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;
}
然后启动游戏,方法checkIfGameIsOpen()返回true。 然后关闭游戏方法checkIfGameIsClose()返回true。
mainGamePanel是Panel,游戏开始时应显示。
问题和可能的解决方案:
revalidate()
和repaint()
。 第一个重新验证,告诉容器及其布局管理器重新布局所有包含的组件,第二个重新绘制,请求JVM重新绘制容器及其所有子组件。 这将有助于消除“脏”像素的容器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.