您的位置:首页 > 产品设计 > UI/UE

java语言gui编程之内部类和鼠标事件经典2之两个问题的解决

2015-03-21 20:12 567 查看
/*
 时间:2015年3月21日20:06:23
 程序目的:对于上一个程序中出现的两个问题
  注: TestMouseMotion.java使用了未经检查或不安全的操作。
  注: 有关详细信息, 请使用 -Xlint:unchecked 重新编译。
 和
  需要完整的罗列MouseMotionListener中所有方法,
  该程序做出调整。
  加入泛型之后,不需要强制转换了。
  使用适配器MoseAdapter,不需要完整罗列所有方法,这是因为在MouseAdapter类中,已经为我们提供
  所有方法的空的实现。所以我们需要做的只是需要重写覆盖其中某个我们所需要的方法就行了
  
*/
import java.awt.*;
import java.awt.event.*;
import java.util.*;
public class TestMouseMotionGenric { public static void main(String[] args) {  new MyFrame("Drawing..."); }}
class MyFrame extends Frame { ArrayList<Point> points = null; public MyFrame(String title) {  super(title);  points = new ArrayList<Point>();  setLayout(null);  setBounds(300, 300, 500, 600);  addMouseMotionListener(new MyAdapter(this));  setVisible(true); }  public void paint(Graphics g) {  Iterator<Point> it = points.iterator();    while (it.hasNext()) {      //Point p = (Point)it.next();      Point p = it.next();   Color c = g.getColor();   g.setColor(Color.PINK);   g.fillOval(p.x, p.y, 5, 5);   g.setColor(c);  } }  public void addPoint(Point p) {  points.add(p); }}
class MyAdapter extends MouseMotionAdapter { private int num = 0; private MyFrame mf = null;  public MyAdapter(MyFrame mf) {  this.mf = mf; }   public void mouseMoved(MouseEvent e) {  mf.addPoint(e.getPoint());  if (num++ > 5) {   mf.repaint();   } }}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: