Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1799363
  • 博文数量: 787
  • 博客积分: 10000
  • 博客等级: 上将
  • 技术积分: 5015
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-22 15:17
文章分类

全部博文(787)

文章存档

2008年(787)

我的朋友

分类:

2008-09-25 16:11:11

JAVA鼠标事件[@more@]

2-1-1:MouseListener的使用:
-----------------------------------------------------------------
          MouseListener              mouseClicked(MouseEvent e)
                                     mouseEntered(MouseEvent e)
                                     mouseExited(MouseEvent e)
                                     mousePressed(MouseEvent e)
                                     mouseReleased(MouseEvent e)
-----------------------------------------------------------------
MouseDemo1.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*我们继承WindowAdapter抽象类并实现MouseListener interface,
 *因此我们必须把MouseListener中的5个方法都实现,如果不想实现
 *可用匿名内部类的方法编写处理程序。
 */
public class MouseDemo1 extends WindowAdapter implements MouseListener{
    JFrame f=null;
    JButton b1=null;
    JLabel label=null;
   
    public MouseDemo1(){
       f=new JFrame("MouseDemo1");
       Container contentPane=f.getContentPane();
       contentPane.setLayout(new GridLayout(2,1));
       b1=new JButton("按钮");
       label=new JLabel("起始状态,还没有鼠标事件",JLabel.CENTER);
       b1.addMouseListener(this);
       contentPane.add(label);
       contentPane.add(b1);
       f.pack();
       f.show();
       f.addWindowListener(this);
    }
    public void mousePressed(MouseEvent e)
              {label.setText("你已经压下鼠标按钮");}
    public void mouseReleased(MouseEvent e)
              {label.setText("你已经放开鼠标按钮");}
    public void mouseEntered(MouseEvent e)
             {label.setText("鼠标光标进入按钮"); }
    public void mouseExited(MouseEvent e)
             {label.setText("鼠标光标离开按钮");}
    public void mouseClicked(MouseEvent e)
             {label.setText("你已经按下按钮");}
    public void windowClosing(WindowEvent e)
             {System.exit(0);}
    public static void main(String[] args)
             { new MouseDemo1();}
}


2-1-2:MouseMotionListener的使用:
    下面是讨论MouseMotionListener的使用时机,它提供的下面的两个方法,可让你随时掌握鼠标的坐标,并处理拖曳鼠标的操作。
-----------------------------------------------------------------------
          MouseMotionListener               mouseDragged(MouseEvent e)
                                            mouseMoved(MouseEvent e)
-----------------------------------------------------------------------
下面的范例让你知道鼠标在JFrame上的坐标,并拖曳出直线来。
例子:MouseDemo3.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
/*为了达到画线的功能,我们分别implements MouseListener与MouseMotionListener.
 */
public class MouseDemo3 extends JFrame implements MouseListener,MouseMotionListener{
     int flag;//flag=1代表Mouse Moved,flag=2代表Mouse Dragged
     int x=0;
     int y=0;
     int startx,starty,endx,endy;//起始坐标与终点坐标
     public MouseDemo3(){
        Container contentPane=getContentPane();
        contentPane.addMouseListener(this);
        contentPane.addMouseMotionListener(this);
        setSize(300,300);
        show();
        addWindowListener(
           new WindowAdapter(){
              public void windowClosing(WindowEvent e){
                 System.exit(0);
              }
              }
        );       
     } 
     /*由mousePressed(),mouseReleased()取得示拖曳的开始与结束坐标*/
     public void mousePressed(MouseEvent e){
        startx=e.getX();
        starty=e.getY();
     }
     public void mouseReleased(MouseEvent e){
        endx=e.getX();
        endy=e.getY();
     }
     public void mouseEntered(MouseEvent e){     }    
     public void mouseExited(MouseEvent e){     }
     public void mouseClicked(MouseEvent e){     }
     /*mouseMoved(),mouseDragged()取得鼠标移动的每一个坐标,并调用repaint()方法*/
     public void mouseMoved(MouseEvent e){
        flag=1;
        x=e.getX();
        y=e.getY();
        repaint();
     }
     public void mouseDragged(MouseEvent e){
        flag=2;
        x=e.getX();
        y=e.getY();
        repaint();
     }
     public void update(Graphics g){
          g.setColor(this.getBackground());
          g.fillRect(0,0,getWidth(),getHeight());
          paint(g);
     }
     public void paint(Graphics g){
          g.setColor(Color.black);
          if (flag==1){
             g.drawString("鼠标坐标:("+x+","+y+")",10,50);
             g.drawLine(startx,starty,endx,endy);
          }
          if (flag==2){
             g.drawString("拖曳鼠标价坐标:("+x+","+y+")",10,50);
             g.drawLine(startx,starty,x,y);
          }
     }
     public static void main(String[] args){
          new MouseDemo3();
     }
}



--------------------next---------------------

阅读(678) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~