Java Crawl mouse events and mouse wheel events sample _java

Source: Internet
Author: User
Tags stub

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 ...");
}
}
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.