In the application I'm writing, I temporarily added a JLabel type field to the menu bar which displays which player's turn it is. Unfortunately, but despite many attempts, I am not able to refresh the player number (it should be Turn of Player 1 or Turn of Player 2).
I recently added an ActionListener, but it didn't help, maybe I'm doing something wrong?
Below are code snippets:
public class Bar extends JMenuBar implements ActionListener {
(...)
static boolean isPlayer1Move = movement.isPlayer1Move();
static JLabel whichPlayer = new JLabel("[Turn of Player " + checkWhichPlayer(isPlayer1Move) + "]");
public Bar() {
(...)
}
public static int checkWhichPlayer(boolean isPlayer1Move) {
return isPlayer1Move ? 1 : 2;
}
public void actionPerformed(ActionEvent event) {
whichPlayer.setText("");
SwingUtilities.invokeLater(new Runnable() {
public void run() {
String result = "[Turn of Player " + checkWhichPlayer(isPlayer1Move) + "]";
whichPlayer.setText(result);
}
});
}
Here's my game:
State of new game.
After a few moves, the Turn of Player 1 caption does not change.