我一直在寻找几个小时,似乎无法找到答案。
我导入javax.swing。*以便我可以在我的程序中使用Timer,但是当导入Timer并且似乎正在运行时,intellij无法解析Timer的其他方法,并且我得到以下错误: Errors picture enter image description here
这是我的代码:
`
import java.util.Random;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.*;
public class Board
{
Random rn = new Random();
private SquareType[][] squares;
private int height;
private int width;
public Poly falling;
private int fallingX;
private int fallingY;
public Board(final int height, final int width) {
this.width = width;
this.height = height;
squares = new SquareType[height][width];
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
squares[i][j] = SquareType.EMPTY;
}
}
}
public int getHeight() {
return height;
}
public int getWidth() {
return width;
}
public SquareType whichSquareType(int height, int width) {
//Takes in two integers one for height and one for width and returns
// the SquareType of the particular cell
return squares[height][width];
}
public void randomizeBoard() {
SquareType[] myTypes = SquareType.values();
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
squares[i][j] = myTypes[rn.nextInt(myTypes.length)];
}
}
}
public int getFallingX() {
return fallingX;
}
public int getFallingY() {
return fallingY;
}
public Poly getFalling() {
return falling;
}
final Action doOneStep = new AbstractAction()
{
public void actionPerformed(ActionEvent e) {
}
};
final Timer clockTimer = new Timer(500, doOneStep);
clockTimer.setCoalesce(true);
clockTimer.start();
}
`
答案 0 :(得分:2)
请注意,您在Board类声明级别调用Timer类方法,而不是在Board类方法中调用。这些是非法的Java语句和错误消息的实际原因。您应该将这些调用封装在一个新的Board类方法中 - 例如initTimer() - 并在需要时调用此方法。
另一方面,clockTimer变量声明和初始化都可以。