遇到的小问题,有点意思啊。估计很多初学者都会遇到的,大家都看看.

DESK12345 2007-12-18 10:31:31
import java.awt.*;
import java.awt.event.*;
public class DrawPictures extends Frame {
public Graphics g;
public int xp, yp, x, y;
public DrawPictures(String title){
super(title);
setVisible(true);
setSize(500,500);
g=this.getGraphics();
//g.setColor(Color.red);
addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(xp,yp,x,y);
xp=x; yp=y;
}
});
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
xp=e.getX(); yp=e.getY();
}
});


}
public static void main(String[] args){
new DrawPictures("画布");
}
}


//下面这个程序和上面的一样,只是将那两个监听程序换了。
import java.awt.*;
import java.awt.event.*;
public class DrawPictures extends Frame {
public Graphics g;
public int xp, yp, x, y;
public DrawPictures(String title){
super(title);
setVisible(true);
setSize(500,500);
g=this.getGraphics();
//g.setColor(Color.red);
addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent e){
xp=e.getX(); yp=e.getY();
}
});

addMouseMotionListener(new MouseMotionAdapter(){
public void mouseDragged(MouseEvent e){
x=e.getX(); y=e.getY();
g.drawLine(xp,yp,x,y);
xp=x; yp=y;
}
});



}
public static void main(String[] args){
new DrawPictures("画布");
}
}

//我觉得应该是现有pessed 再有drag啊?但是?
//帮个忙啊
...全文
135 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
永远的明日 2007-12-19
  • 打赏
  • 举报
回复
两个都行的呀````

setVisible(true)应该放到g=this.getGraphics()之前

要是还没可见,执行g=this.getGraphics(),g==null
DESK12345 2007-12-19
  • 打赏
  • 举报
回复
还有啊 ,就是将
setVisible(true);
setSize(500,500);
写在监听的后面也不行啊, 都他妈的郁闷死我了
DESK12345 2007-12-19
  • 打赏
  • 举报
回复
我又试了一次好像是没什么关系了,但是上回我测试的时候就是有关啊,我把那两个掉了个,就可以运行了。这次....
郁闷啊
DESK12345 2007-12-19
  • 打赏
  • 举报
回复
有关系啊,下面这个程序编译没有错,但是运行的话就不能实现画笔的功能啊。。。你试试先
feng_cs 2007-12-19
  • 打赏
  • 举报
回复
都是监听,放哪没什么关系,只要能捕获就行了啊。

62,623

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧