【发布时间】:2018-11-26 08:50:05
【问题描述】:
我正在编写扫雷,我想在点击地雷后在地雷单元格上放置椭圆形。但就目前而言,我只想在所有单元格上放置椭圆以进行检查。我已经写了下面的代码。但是当我运行程序时,按钮上没有椭圆。我看不出原因。如果我能得到一些建议,我将不胜感激。
public class Cell extends JButton{
...
public void painComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.orange);
g.drawOval(0,0,25,25);
}
public void draw() {
repaint();
}
...
}
public class Grid extends JPanel implements MouseListener{
...
public Grid(){
this.setLayout(new GridLayout(20,20));
cells = new Cell[20][20];
for(int i=0; i<20; i++) {
for(int j=0; j<20; j++) {
cells[i][j] = new Cell(i,j);
cells[i][j].addMouseListener(this);
cells[i][j].draw();
this.add(cells[i][j]);
}
}
plantMines();
setVisible(true);
}
...
}
【问题讨论】:
-
你有没有在方法中添加任何 System.out.println(...) 语句来查看它是否执行?不要假设代码会执行!
-
为什么不直接使用图片?
-
@MadProgrammer “为什么不简单地使用图像?” 更简单的是使用文本。 This example 使用文本表示游戏状态(地雷数量、已识别地雷等)。