我把书上的一个程序改了一下,但是运行不出那种效果来了,是哪里的错误?
这个程序要实现的功能:鼠标按下去的位置作为起始点,鼠标释放是的位置作为终止点,并在鼠标释放时画线。原程序:
import java.awt.*;
import java.awt.event.*;
public class DrawLine
{
Frame f=new Frame("IT人资讯交流网");
public static void main(String []args)
{
new DrawLine().init();
}
public void init()
{
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(new MouseAdapter()
{
int orgx;
int orgy;
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseReleased(MouseEvent e)
{
Graphics g=f.getGraphics();
g.setColor(Color.red);
g.drawLine(orgx,orgy,e.getX(),e.getY());
}
});
}
}
我改后的程序:
import java.awt.*;
import java.awt.event.*;
public class DrawLine extends MouseAdapter
{
private Frame f;
private Graphics g;
private int orgx;
private int orgy;
public DrawLine()
{
f=new Frame("IT人资讯交流网");
f.setSize(300,300);
f.setVisible(true);
f.addMouseListener(this);
}
public void mousePressed(MouseEvent e)
{
orgx=e.getX();
orgy=e.getY();
}
public void mouseRleased(MouseEvent e)
{
g=f.getGraphics();
g.setColor(Color.red);
g.drawLine(orgx,orgy,e.getX(),e.getY());
}
public static void main(String args[])
{
new DrawLine();
}
}
谁能告诉我,我改的程序哪里有错,谢谢!