我用服務器和客戶端(多客戶端)編寫卡片遊戲。如果遊戲開始或遊戲關閉,我需要每秒檢查一次。所以我寫了一個java.swing.timer來檢查它。但是它並沒有在遊戲開始時設置JPanel的可見性。 (對不起,我英文不好)Java搖擺計時器沒有設置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()'。 –