I want to update a variable every time I click on a JLabel. However, I get an error because the variable is not declared locally.
Timer picTimer = new Timer(1000, new ActionListener(){
int oldrr=0;
int oldrc=0 ;
@Override
public void actionPerformed(ActionEvent e) {
arrayWM[oldrr][oldrc].setIcon(null);
Random random = new Random();
arrayWM[oldrr][oldrc].setIcon(null);
int rr = random.nextInt(3 - 0 + 1) + 0;
int rc = random.nextInt(3 - 0 + 1) + 0;
oldrr = rr;
oldrc = rc;
arrayWM[rr][rc].setIcon(new ImageIcon("img/one.jpg"));
int score=0;
arrayWM[rr][rc].addMouseListener(new MouseAdapter(){
@Override public void mouseClicked( MouseEvent e){
score++;
lblTimer.setText(String.valueOf(score));
}
}
);
}
});
1 个答案:
答案 0 :(得分:0)
像这样改变
final int[] score = {0};
和
score[0]++;