4

我一直在学习 Java 文本的艺术与科学和 SEE CS106A 课程。在引入交互式图形程序之前,一切都进行得很顺利。以下代码直接取自文本,无法编译:

/*
 * File: DrawLines.java
 * -----------------------
 * This program allows a user to draw lines to the canvas.
 */

import acm.graphics.*;
import acm.program.*;
import java.awt.event.*;

public class DrawLines extends GraphicsProgram {

    public void run() {
        addMouseListeners();
    }

    /** Called on mouse press to record the coordinates of the click */
    public void mousePressed(MouseEvent e) {
        double x = e.getX();
        double y = e.getY();
        line = new GLine(x, y, x, y);
        add(line);
    }

    /** Called on mouse drag to reposition the object */
    public void mouseDragged(MouseEvent e) {
        double x = e.getX();
        double y = e.getY();
        line.setEndPoint(x, y);
    }

    private GLine line;

}

它在第 14 行失败并出现cannot find symbol: method addMouseListeners()错误。没有该方法调用的 ACM ConsolePrograms 和 GraphicsPrograms 工作正常。据我所知,这种方法应该是有效的。

我在这里做错了吗?ACM 文档和教科书是否已过时?如何在此处添加鼠标侦听器?

4

1 回答 1

1

事实证明karel.jar,在 CS106A 的第一次分配中使用的库会干扰该addMouseListeners()方法。karel.jar从源中删除可以解决问题。

于 2014-03-21T04:57:41.533 回答