package three.day.frame;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.HeadlessException;
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.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class MouseDemo extends JFrame implements MouseListener,MouseMotionListener
{
private JLabel lbX;
private JLabel lbY;
private JLabel lbTip;
private JTextField tfX;
private JTextField tfY;
/**
* @param title
* @throws HeadlessException
*/
public MouseDemo(String title) throws HeadlessException {
super(title);
lbX = new JLabel("X:");
lbY = new JLabel("Y:");
lbTip = new JLabel("Mouse Event");
tfX = new JTextField(5);
tfY = new JTextField(5);
Container panel = getContentPane();
panel.setLayout(new FlowLayout());
panel.add(lbX);
panel.add(tfX);
panel.add(lbY);
panel.add(tfY);
panel.add(lbTip);
setBounds(300,200,400,300);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
addMouseListener(this);
addMouseMotionListener(this);
setVisible(true);
}
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
new MouseDemo("Mouse Demo");
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("鼠标点击了");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("鼠标进入了窗口");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("鼠标离开了窗口");
tfX.setText(String.valueOf(0));
tfY.setText(String.valueOf(0));
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("鼠标按着了");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("拖动鼠标后,释放了鼠标");
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("拖动鼠标");
tfX.setText(String.valueOf(e.getX()));
tfY.setText(String.valueOf(e.getY()));
}
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
lbTip.setText("鼠标移动了");
tfX.setText(String.valueOf(e.getX()));
tfY.setText(String.valueOf(e.getY()));
}
}