I'm trying to remove a JButton from a JPanel but my code does not work. Any help is appreciated.
This is the JButton. Problem is the line 25 I guess.
class SquareButton extends JButton implements MouseListener {
String text = null;
GameArea ga = null;
public SquareButton(GameArea ga) {
this.ga = ga;
setPreferredSize(new Dimension(30, 30));
setOpaque(false);
addMouseListener(this);
setFont(new Font("Verdana", Font.BOLD, 15));
setHorizontalAlignment(CENTER);
setVerticalAlignment(CENTER);
}
public Insets getInsets() {
return new Insets(0,0,0,0);
}
public void mouseClicked(MouseEvent e) {
if(e.getButton() == 3) {
changeIcon();
}
else if(e.getButton() == 1) {
ga.remove(this);
ga.add(new SquareLabel());
revalidate();
}
}
private void changeIcon() {
if(text == null) {
text = "!";
setText(text);
} else {
text = null;
setText("");
}
}
public void mouseEntered(MouseEvent arg0) {}
public void mouseExited(MouseEvent arg0) {}
public void mousePressed(MouseEvent arg0) {}
public void mouseReleased(MouseEvent arg0) {}
}
Part of the GameArea:
public GameArea(int gameWidth, int gameHeight, int mines) {
squares = new SquareButton[gameWidth][gameHeight];
mineField = new boolean[gameWidth][gameHeight];
hintNumbers = new int[gameWidth][gameHeight];
setLayout(new BorderLayout(0, 0));
status = new JPanel();
gameArea = new JPanel();
gameArea.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
add(status,BorderLayout.NORTH);
add(gameArea,BorderLayout.CENTER);
setSize(300, 330);
setVisible(true);
for(int i = 0; i < gameWidth; i++) {
for(int j = 0; j < gameHeight; j++) {
squares[i][j] = new SquareButton(this);
gameArea.add(squares[i][j]);
}
}
randMines(mines, gameWidth, gameHeight);
calculateHints(mineField, gameWidth, gameHeight);
}