//MouseMotionEvent1.java
//鼠标移动焦点
//2009-11-15
//<applet code=MouseMotionEvent1.class width=200 height=100>
//</applet>
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class MouseMotionEvent1 extends JApplet
{
JTextArea txt1=new JTextArea(15,15);
JTextField txt2=new JTextField();
class KeyTry implements MouseMotionListener
{
public void mouseDragged(MouseEvent e){
txt1.setText("paramString="+e.paramString());
txt2.setText("the position:X:"+e.getX()+"Y"+e.getY());
}
public void mouseMoved(MouseEvent e){
txt1.setText("paramString="+e.paramString());
txt2.setText("the position:X:"+e.getX()+",Y"+e.getY());
}
}
KeyTry keyTry=new KeyTry();
public void init(){
Container cp=getContentPane();
cp.setLayout(new FlowLayout());
cp.addMouseMotionListener(keyTry); //Applet鼠标焦点
//小应用程序也是构件,此事件是添加给applet容器的。txt里并没有。
txt1.addMouseMotionListener(keyTry); //txt1构件鼠标焦点
txt1.setLineWrap(true); //txt1自动换行
cp.add(txt1);
cp.add(txt2);
}
}