Java Crawl mouse events and wheel events
Copy Code code as follows:
Package demo;
Import java.awt.event.MouseEvent;
Import Java.awt.event.MouseListener;
Import java.awt.event.MouseWheelEvent;
Import Java.awt.event.MouseWheelListener;
Import Javax.swing.JButton;
Import Javax.swing.JFrame;
Import Javax.swing.JPanel;
public class Mousedemo extends JFrame implements mouselistener,mousewheellistener{
static int chang=400,kuang=400;
Public Mousedemo () {
JPanel j=new JPanel ();
JButton jb=new JButton ("click");
J.add (JB);
This.add (j);
This.setvisible (TRUE);
This.setsize (Chang,kuang);
Jb.addmouselistener (this);
This.addmousewheellistener (this);
}
public void mouseclicked (MouseEvent e) {
Left-click event
if (E.getbutton () ==mouseevent.button1)
System.out.println ("Zuo Ji Shi Jian ...");
The pulley presses the event
if (E.getbutton () ==mouseevent.button2)
System.out.println ("Zhong Ji Shi Jian ...");
Right-click events
if (E.getbutton () ==mouseevent.button3)
System.out.println ("You Ji Shi Jian ...");
}
public static void Main (string[] args) {
TODO auto-generated Method Stub
New Mousedemo ();
}
@Override
public void mouseentered (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mouseexited (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mousepressed (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
public void mousereleased (MouseEvent e) {
TODO auto-generated Method Stub
}
@Override
Zooming in and out of a window using wheel events
public void mousewheelmoved (Mousewheelevent e) {
TODO auto-generated Method Stub
if (E.getwheelrotation () ==1) {
chang+=10;
kuang+=10;
This.setsize (Chang,kuang);
This.validate ();
System.out.println ("pulley forward ....") ");
}
if (E.getwheelrotation () ==-1) {
chang-=10;
kuang-=10;
This.setsize (Chang,kuang);
This.validate ();
System.out.println ("pulley backwards ...");
}
}
}