Home>

The event source for mouse events is often related to the container,When the mouse enters the container, leaves the container,Or mouse events occur when you click or drag the mouse in the container.The Java language provides two interfaces for handling mouse events:mouselistener and mousemotionlistener interfaces.

mouselistener interface

The mouselistener interface can handle 5 types of mouse events:press the mouse,Release the mouse,Click the mouse, enter the mouse, and exit the mouse.The corresponding methods are:

(1) getx ():the x coordinate of the mouse

(2) gety ():the y coordinate of the mouse

(3) getmodifiers ():Get the left or right mouse button.

(4) getclickcount ():the number of times the mouse has been clicked.

(5) getsource ():Get the event source where the mouse occurred.

(6) addmouselistener (monitor):add a monitor.

(7) removemouselistener (monitor):remove the monitor.

The methods to implement the mouselistener interface are:

(1) mousepressed (mouseevent e);

(2) mousereleased (mouseevent e);

(3) mouseentered (mouseevent e);

(4) mouseexited (mouseevent e);

(5) mouseclicked (mouseevent e);

[Example] The applet sets a text area.Used to record a series of mouse events.When the mouse enters the applet window,The text area displays "Mouse comes in";when the mouse leaves the window, the text area displays "Mouse away";when the mouse is pressed,The text area displays "mouse down". When the mouse is double-clicked,The text area displays "Double Mouse Click";and the coordinates of the mouse are displayed.The program also displays a red circle,When clicking the mouse,The radius of the circle is constantly increasing.

import java.applet. *;
import javax.swing. *;
import java.awt. *;
import java.awt.event. *;
class mypanel extends jpanel {
  public void print (int r) {
    graphics g=getgraphics ();
    g.clearrect (0,0, this.getwidth (), this.getheight ());
    g.setcolor (color.red);
    g.filloval (10,10, r, r);
  }
}
class mywindow extends jframe implements mouselistener {
  jtextarea text;
  mypanel panel;
  int x, y, r=10;
  int mouseflg=0;
  static string mousestates []={"Mouse button pressed", "Mouse released", "Mouse comes in", "Mouse goes away", "Double mouse click"};
  mywindow (string s) {
    super (s);
    container con=this.getcontentpane ();
    con.setlayout (new gridlayout (2,1));
    this.setsize (200,300);
    this.setlocation (100,100);
    panel=new mypanel ();
    con.add (panel);
    text=new jtextarea (10,20);
    text.setbackground (color.blue);
    con.add (text);
    addmouselistener (this);
    this.setvisible (true);
    this.pack ();
  }
  public void paint (graphics g) {
    r=r + 4;
    if (r>80) {
      r=10;
    }
    text.append (mousestates [mouseflg] + ", the position is:" + x + "," + y + "\ n");
    panel.print (r);
  }
  public void mousepressed (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    mouseflg=0;
    repaint ();
  }
  public void mouserelease (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    mouseflg=1;
    repaint ();
  }
  public void mouseentered (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    mouseflg=2;
    repaint ();
  }
  public void mouseexited (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    mouseflg=3;
    repaint ();
  }
  public void mouseclicked (mouseevent e) {
    if (e.getclickcount () == 2) {
      x=e.getx ();
      y=e.gety ();
      mouseflg=4;
      repaint ();
    }
    else {}
  }
}
public class example6_8 extends applet {
  public void init () {
    mywindow mywnd=new mywindow ("Mouse event signaler");
  }
}

Mouse events can occur on any component:mouse enter, mouse exit, mouse down, etc.For example, add a button to the above program,And add a mouse monitor to the button object,Modify the init () method in the above program to the following form,That is to say all mouse events on the button.

jbutton button;
public void init () {
  button=new jbutton ("Button events can also occur with mouse events");
  r=10;
  text=new jtextarea (15,20);
  add (button);
  add (text);
  button.addmouselistener (this);
}

If the program wants to know further whether the left or right mouse button was pressed or clicked,The left or right mouse button can be determined by the constants button1_mask and button3_mask in the inputevent class. For example, the following expression determines whether the right mouse button was pressed or clicked:

 e.getmodifiers () == inputevent. button3_mask

mousemotionlistener interface

The mousemotionlistener interface handles two events of dragging the mouse and moving the mouse.

To register a monitor:

addmousemotionlistener (monitor)

There are two interface methods to be implemented:

(1) mousedragged (mouseevent e)

(2) mousemoved (mouseevent e)

[Example] An application program whose scroll bar changes synchronously with the display window.The window has a square,Drag the block with the mouse,Or click the window with your mouse,The square changes the display position,The sliders of the corresponding horizontal and vertical scroll bars also change their position in the scroll bar.Conversely, move the slider of the scroll bar,The display position of the block in the window will also change.

import javax.swing. *;
import java.awt. *;
import java.awt.event. *;
class mywindow extends jframe {
  public mywindow (string s) {
    super (s);
    container con=this.getcontentpane ();
    con.setlayout (new borderlayout ());
    this.setlocation (100,100);
    jscrollbar xaxis=new jscrollbar (jscrollbar.horizontal, 50,1,0,100);
    jscrollbar yaxis=new jscrollbar (jscrollbar.vertical, 50,1,0,100);
    mylistener listener=new mylistener (xaxis, yaxis, 238,118);
    jpanel scrolledcanvas=new jpanel ();
    scrolledcanvas.setlayout (new borderlayout ());
    scrolledcanvas.add (listener, borderlayout.center);
    scrolledcanvas.add (xaix, borderlayout.south);
    scrolledcanvas.add (yaix, borderlayout.east);
    con.add (scrolledcanvas, borderlayout.north);
    this.setvisible (true);
    this.pack ();
  }
  public dimension getpreferredsize () {
    return new dimension (500,300);
  }
}
class mylistener extends jcomponent implements mouselistener, mousemotionlistener, adjustmentlistener {
  private int x, y;
  private jscrollbar xscrollbar;
  private jscrollbar yscrollbar;
  private void updatescrollbars (int x, int y) {
    int d;
    d=(int) (((float) x/(float) getsize (). width) * 100.0);
    xscrollbar.setvalue (d);
    d=(int) (((float) y/(float) getsize (). height) * 100.0);
    yscrollbar.setvalue (d);
  }
  public mylistener (jscrollbar xaxis, jscrollbar yaxis, int x0, int y0) {
    xscrollbar=xaxis;
    yscrollbar=yaxis;
    x=x0;
    y=y0;
    xscrollbar.addadjustmentlistener (this);
    yscrollbar.addadjustmentlistener (this);
    this.addmouselistener (this);
    this.addmousemotionlistener (this);
  }
  public void paint (graphics g) {
    g.setcolor (getbackground ());
    dimension size=getsize ();
    g.fillrect (0,0, size.width, size.height);
    g.setcolor (color.blue);
    g.fillrect (x, y, 50,50);
  }
  public void mouseentered (mouseevent e) {}
  public void mouseexited (mouseevent e) {}
  public void mouseclicked (mouseevent e) {}
  public void mouserelease (mouseevent e) {}
  public void mousemoved (mouseevent e) {}
  public void mousepressed (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    updatescrollbars (x, y);
    repaint ();
  }
  public void mousedragged (mouseevent e) {
    x=e.getx ();
    y=e.gety ();
    updatescrollbars (x, y);
    repaint ();
  }
  public void adjustmentvaluechanged (adjustmentevent e) {
    if (e.getsource () == xscrollbar)
      x=(int) ((float) (xscrollbar.getvalue ()/100.0) * getsize (). width);
    else if (e.getsource () == yscrollbar)
      y=(int) ((float) (yscrollbar.getvalue ()/100.0) * getsize (). height);
    repaint ();
  }
}
public class example6_9 {
  public static void main () {
    mywindow mywindow=new mywindow ("Scroll bar signalling program");
  }
}

In the example above,If you only need to slide the slider,Change the display position of the content,You can simply use the jscrollpane scroll panel. If this is the case,Regarding the creation and control of scroll bars,Implemented directly by jscrollpane.See the following definition of mywindow:

class mywindow extends jframe {
  public mywindow (string s) {
    super (s);
    container con=this.getcontentpane ();
    con.setlayout (new borderlayout ());
    this.setlocaltion (100,100);
    mylistener listener=new mylistener ();
    listener.setpreferredsize (new dimension (700,700));
    jscrollpane scrolledcanvas=new jscrollpane (listener);
    this.add (scrolledcanvas, borderlayout.center);
    this.setvisible (true);
    this.pack ();
  }
  public dimension getpreferredsize () {
    return new dimension (400,400);
  }
}

The shape of the mouse pointer can also be controlled by the program. The setcursor () method can set the shape of the mouse pointer.For example, the code setcursor (cursor.getpredefinedcursor (cursor.wait_cursor)).

  • Previous Page jump and pop-up modal of ViewController in iOS development
  • Next Implementation method of jQuery table row up, down and top