import java.awt.*;
import java.awt.event.*;
class GfxTest extends MouseAdapter{
int x1,x2,y1,y2;
Frame f=new Frame();
public static void main(String[] args) {
new GfxTest().a();
}
public void a(){
f.setSize(300, 300);
f.setVisible(true);
f.addMouseListener(new GfxTest());
}
public void mousePressed(MouseEvent e){
x1=e.getX();
y1=e.getY();
}
public void mouseReleased(MouseEvent e){
x2=e.getX();
y2=e.getY();
f.getGraphics().drawLine(x1, y1, x2, y2);
}
}
请问大家这段代码可以编译通过,但是运行后画直线就报错了,为什么?
------解决方案--------------------
public void a(){
f.setSize(300, 300);
f.setVisible(true);
f.addMouseListener(this);//改成this
}