我正在尝试创建扫雷,并且我很早就设法让我的 JButton 数组返回 void 而不是 JButton,因此我无法对其执行任何操作。
这是代码:(当我想删除按钮时,错误发生在最后一行)
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Game extends JFrame implements ActionListener
{
JButton[][] buttons;
int rows;
int cols;
int x;
int y;
public Game(int rows, int cols)
{
setTitle("Minesweeper");
setSize(500, 500);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.rows = rows;
this.cols = cols;
setLayout(new GridLayout(rows, cols));
buttons(rows, cols);
}
public void buttons(int tableX, int tableY)
{
buttons = new JButton[tableX][tableY];
for (x = 0; x < tableX; x++)
{
for (y = 0; y < tableY; y++)
{
buttons[x][y] = new JButton();
buttons[x][y].setActionCommand("Pressed");
buttons[x][y].addActionListener(this);
this.add(buttons[x][y]);
}
}
}
public void actionPerformed(ActionEvent e)
{
for (x = 0; x < rows; x++)
{
for (y = 0; y < cols; y++)
{
if (e.getActionCommand().equals("Pressed"))
{
buttons[x][y] = setVisible(false);
}
}
}
}
}