swing鼠标事件分析
阅前声明: http://blog.csdn.net/heimaoxiaozi/archive/2007/01/19/1487884.aspx
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();
}
}
本文地址:http://www.45fan.com/dnjc/72993.html