我一直在学习 Java 的艺术与科学 文本和 SEE CS106A 类(class)。在引入交互式图形程序之前,一切都进行得很顺利。以下代码直接取自文本,将无法编译:
/*
* 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 工作正常。据我所知,这个方法should be valid .
我是不是做错了什么? ACM 文档和教科书是否过时?如何在此处添加鼠标监听器?
最佳答案
事实证明,在 CS106A 的第一个作业中使用的库 karel.jar
干扰了 addMouseListeners()
方法。从源中删除 karel.jar
可解决问题。
关于java - 在 acm.program.GraphicsProgram 中找不到符号方法 addMouseListeners(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22510890/