读书人

java中鼠标事件解决方案

发布时间: 2012-03-17 19:06:28 作者: rapoo

java中鼠标事件
import java.awt.*;
import java.awt.event.*;

import javax.swing.SwingUtilities;
class WinB extends Frame implements MouseListener,MouseMotionListener
{ Button button;
TextField text;
int x, y;
boolean move=false;
WinB()
{ button=new Button("用鼠标拖动我");
text=new TextField("用鼠标拖动我",8);
button.addMouseListener(this);
button.addMouseMotionListener(this);
text.addMouseListener(this);
text.addMouseMotionListener(this);
addMouseMotionListener(this);
setLayout(new FlowLayout());
add(button);
add(text);
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e){
System.exit(0);
}
});
setBounds(10,10,350,300);
setVisible(true);
validate();
}
public void mousePressed(MouseEvent e){}
public void mouseReleased(MouseEvent e)
{ move=false;

}
public void mouseEntered(MouseEvent e){}
public void mouseExited(MouseEvent e){}
public void mouseClicked(MouseEvent e){}
public void mouseMoved(MouseEvent e){}
public void mouseDragged(MouseEvent e)
{ Component com=null;
if(e.getSource() instanceof Component)
{ if(com!=this)
move=true;
e=SwingUtilities.convertMouseEvent(com, e, this);
if(move)
{ x=e.getX();
y=e.getY();
int w=com.getSize().width;
int h=com.getSize().height;
com.setLocation(x-w/2, y-h/2);
}
}
}
}
public class MouseMotionExa {

/**
* @param args
*/
public static void main(String[] args) {
new WinB();
// TODO Auto-generated method stub

}

}


上述代码是实现通过鼠标移动文本框和按钮,编译通过了,可是用鼠标拖动时开始报错,if(e.getSource() instanceof Component) 我把这句改成if(e.e.getSource()==com)没有错误了,但是鼠标事件不发生,该句判断鼠标事件的对象的类是否是Component,这句话主要起什么作用?

[解决办法]
查看了API,类的关系如下:
java.lang.Object
java.util.EventObject
java.awt.AWTEvent
java.awt.event.ComponentEvent
所以,Component com = e.getComponent(),才对。
修改的WinB的代码如下:

Java code
package demo;import java.awt.Button;import java.awt.Component;import java.awt.FlowLayout;import java.awt.Frame;import java.awt.TextField;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.MouseMotionListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.SwingUtilities;public class WinB extends Frame implements MouseListener, MouseMotionListener {    Button button;    TextField text;    int x, y;    boolean move = false;    WinB() {    button = new Button("用鼠标拖动我");    text = new TextField("用鼠标拖动我", 8);    button.addMouseListener(this);    button.addMouseMotionListener(this);    text.addMouseListener(this);    text.addMouseMotionListener(this);    addMouseMotionListener(this);    setLayout(new FlowLayout());    add(button);    add(text);    addWindowListener(new WindowAdapter() {        public void windowClosing(WindowEvent e) {        System.exit(0);        }    });    setBounds(10, 10, 350, 300);    setVisible(true);    validate();    }    public void mousePressed(MouseEvent e) {    }    public void mouseReleased(MouseEvent e) {    move = false;    }    public void mouseEntered(MouseEvent e) {    }    public void mouseExited(MouseEvent e) {    }    public void mouseClicked(MouseEvent e) {    }    public void mouseMoved(MouseEvent e) {    }    public void mouseDragged(MouseEvent e) {    Component com = e.getComponent();    if (com instanceof Component) {        if (com != this)        move = true;        e = SwingUtilities.convertMouseEvent(com, e, this);        if (move) {        x = e.getX();        y = e.getY();        int w = com.getSize().width;        int h = com.getSize().height;        com.setLocation(x - w / 2, y - h / 2);        }    }    }} 

读书人网 >J2SE开发

热点推荐