Java 鼠标事件分析
e.getSource()就是触发时间的控件。
import java.awt.*;import java.awt.event.*;import javax.swing.*;public class Example6_10{JFrame win;JTextField text;public Example6_10(){win=new JFrame("事件适配器及内部类示例");text=new JTextField(30);}public void inFrame(){JLabel label=new JLabel("单击并拖曳鼠标");win.add(label,BorderLayout.NORTH);win.add(text,BorderLayout.SOUTH);win.addMouseMotionListener(new MyMouseMotionListener());win.setSize(300,200);win.setVisible(true);}class MyMouseMotionListener extends MouseMotionAdapter{public void mouseDragged(MouseEvent e){String s;s="Mouse dragging:x="+e.getX()+"Y="+e.getY();text.setText(s);}}public static void main(String[] args){Example6_10 w=new Example6_10();w.inFrame();}}
而适配器通常使用匿名内部类实现的。
this.addMouseListener(new MouseAdapter(){
public void mousePressed(MouseEvent me){
txtStatus.setText("按下");
}
public void mouseReleased(MouseEvent me){
txtStatus.setText("释放");
}
});
在这个匿名类中就不需要那些空方法了,当然用这种方法,implements 后面的东西也可以不要了。
当然如果你对Java刚入门,对继承接口之类的还不了解,则不一定明白我在说什么。
至于main函数中的 MouseEventDemo med=new MouseEventDemo();
new MouseEventDemo是声明了一个新的对象,并把它赋给了med引用,这样你可以用这个引用调用这个类中的方法。例如这个类有继承自JFrame的setTitle方法,你可以med.setTitle("dfdfd");
光一个new MouseEventDemo则没有被引用罢了,但本质上他们都调用了这个类的构造函数。只不过当构造函数结束后,如果是单线程,后者会被垃圾回收,前者不会。因为还有对象引用存在,这在你看到垃圾回收机制后才能明白,如果向这里一样GUI内部的多线程机制,则在那个线程结束后,后者会被垃圾回收,前者不会。
当然对于这个程序而言加个引用没多大意义。
总之,你的路还长着呢。。。。。。
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class MouseEventDemo extends JFrame implements MouseListener,
MouseMotionListener {
int intX, intY;
JPanel pnlMain;
JLabel lblX, lblY, lblStatus;
JTextField txtX, txtY, txtStatus;
GridLayout glMain;
public MouseEventDemo() {
pnlMain = new JPanel();
getContentPane().add(pnlMain);
glMain = new GridLayout(3, 2);
pnlMain.setLayout(glMain);
lblX = new JLabel("当前鼠标X坐标:");
lblY = new JLabel("当前鼠标Y坐标:");
lblStatus = new JLabel("当前鼠标状态:");
txtX = new JTextField(5);
txtY = new JTextField(5);
txtStatus = new JTextField(5);
pnlMain.add(lblX);
pnlMain.add(txtX);
pnlMain.add(lblY);
pnlMain.add(txtY);
pnlMain.add(lblStatus);
pnlMain.add(txtStatus);
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
setTitle("好啊");
setSize(250, 150);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void mouseClicked(MouseEvent me) { //MouseListener中的方法,必须要实现
//你继承了MouseListener, MouseMotionListener 这两个接口,必须要实现它里面定义的方法,哪怕是个空,没有就会报错,编译通不过
//这几个方法都是要实现的,只是你自己没有写而已
}// 这是空操作为什么啊,有什么用?
public void mousePressed(MouseEvent me) {
txtStatus.setText("按下");
}
public void mouseEntered(MouseEvent me) {
txtStatus.setText("移入");
}
public void mouseExited(MouseEvent me) {
txtStatus.setText("移出");
}
public void mouseReleased(MouseEvent me) {
txtStatus.setText("释放");
}
public void mouseMoved(MouseEvent me) {
intX = me.getX();
intY = me.getY();
txtX.setText(String.valueOf(intX));
txtY.setText(String.valueOf(intY));
}
public void mouseDragged(MouseEvent me) {
txtStatus.setText("拖我干什么?");
}// 这也是空操作为什么啊,有什么用?
public static void main(String[] args) {
MouseEventDemo med = new MouseEventDemo();// 这是整个程序的入口可是它这形式怎么和一般的不一样啊?好像我见过的形如:new
// MouseEventDemo();这样的形式更多点吧,这和上面的这种形式有什么区别?
//你new 了一个对象,但med从来没用过,留着也没用,不如直接用 new MouseEventDemo();
}
}
这两个空操作是implements MouseListener,
MouseMotionListener接口中定义的抽象方法
接口中的抽象方法必须在接口的实现类中实现,而这两个方法跟这个程序的实现没有什么关系,所以就没有定义该方法的实现。
MouseEventDemo med=new MouseEventDemo();//这是整个程序的入口可是它这形式怎么和一般的不一样啊?好像我见过的形如:new MouseEventDemo();这样的形式更多点吧,这和上面的这种形式有什么区别?
一个在栈中给了个引用而已,没有其他区别