Question

When I click the button, I hope the background of the JButton will change its color. When I try to compile, an error appears

class, interface, or enum expected

Anyone can help me to figure out what's the problem?

@Override
public void actionPerformed(ActionEvent e) {
    count ++;
    JButton b = (JButton) e.getSource();
    boolean BlackTurn = true;
    for(int i = 0; i < rows; i++) {
        for(int j = 0; j < column; j++) {
            if(b == gameBoard[i][j] && gameBoard[i][j].getBackground() == Color.green) {
                gameBoard[i][j].setBackground(Color.black);
            }
        }
    }
}
Was it helpful?

Solution

This is most probably a problem of missing curly bracket or missing semicolon:
the error message you get refers to some missing (understand incorrect) class (or interface, or enum) declaration.

If you were to post the whole content of the class declaration enclosing that method you showed us, I'm pretty sure we could find that missing character in a matter of seconds ;)


Also, since I'm at it, I prefer to use Color.GREEN (and Color.BLACK, Color.RED, etc.) instead of Color.green, because these aliases respect the convention that states that constants must be upper-case.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top