I have the class GridButtonPanel
such here is and I have a JLabel like this in createNavPanel
void:
public JPanel box;
public JLabel cpLabel;
n = new JPanel(new BorderLayout());
box = new JPanel();
box.setLayout(new BoxLayout(box, BoxLayout.Y_AXIS));
cpLabel = gui.currentPlayerLabel(game.currentPlayer);
box.add(cpLabel);
n.add(box, BorderLayout.WEST);
Of course I have some other thing here too...
The gui.currentPlayer
is this:
JLabel currentPlayerLabel(int cp) {
JLabel l = new JLabel();
switch(cp){
case 1 : l.setText("Current Player: Player 1");
l.setForeground(Color.green);
break;
case 2 : l.setText("Current Player: Player 2");
l.setForeground(Color.blue);
break;
}
return l;
}
And I add this all things to main JFrame like this:
f.add(createNavPanel(), BorderLayout.NORTH);
And I have an other class, the gui
. Here I make some things to work, and I want to update the cpLabel
. I tried like this:
private void changePlayerLabel(){
int cp = game.currentPlayer;
switch(cp){
case 1 :
gpanel.cpLabel.setText("Current Player: Player 1");
gpanel.cpLabel.setForeground(Color.green);
break;
case 2 : gpanel.cpLabel.setText("Current Player: Player 2");
gpanel.cpLabel.setForeground(Color.blue);
break;
}
}
But this isn't working in this case. How can I do this?