0

我现在已经搜索了几个小时,似乎无法找到答案。

我导入 javax.swing.* 以便我可以在我的程序中使用 Timer,但是当 Timer 被导入并且似乎正在运行时,intellij 无法解决 Timer 的其他方法,我得到以下错误: 错误图片 输入图像描述这里

这是我的代码:

`

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();
}

`

4

1 回答 1

2

请注意,您是在 Board 类声明级别调用 Timer 类方法,而不是从 Board 类方法中调用。这些是非法的 Java 语句和错误消息的实际原因。您应该将这些调用封装在一个新的 Board 类方法中 - 比如说 initTimer() - 并在需要时调用此方法。

另一方面,clockTimer 变量声明和初始化都可以。

于 2016-03-17T18:49:49.007 回答