Java Tutorial/SWT/MouseEvent
Содержание
- 1 Add Mouse Down, Up and Move event
- 2 Composite: intercept mouse events (drag a button with the mouse)
- 3 Detect mouse enter, exit and hover events
- 4 extends MouseTrackAdapter
- 5 Print mouse state and button (down, move, up)
- 6 Using MouseListener
- 7 Using MouseListener, MouseMoveListener, and MouseTrackListener
- 8 Using MouseMoveListener
- 9 Using MouseTrackListener
Add Mouse Down, Up and Move event
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class MouseEventDownUpMove {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); Listener listener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MouseDown: System.out.println("down:" + event); break; case SWT.MouseMove: System.out.println("move:"+event); break; case SWT.MouseUp: System.out.println("Up:"+event); break; } } }; shell.addListener(SWT.MouseDown, listener); shell.addListener(SWT.MouseUp, listener); shell.addListener(SWT.MouseMove, listener); shell.setSize(300, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Composite: intercept mouse events (drag a button with the mouse)
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
/*
* Composite example snippet: intercept mouse events (drag a button with the mouse) * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ */
import org.eclipse.swt.SWT; import org.eclipse.swt.graphics.Point; import org.eclipse.swt.graphics.Rectangle; import org.eclipse.swt.layout.FillLayout; import org.eclipse.swt.widgets.Button; import org.eclipse.swt.widgets.ruposite; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class DragButtonMouseEvents {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); final Composite composite = new Composite(shell, SWT.NONE); composite.setEnabled(false); composite.setLayout(new FillLayout()); Button button = new Button(composite, SWT.PUSH); button.setText("Button"); composite.pack(); composite.setLocation(10, 10); final Point[] offset = new Point[1]; Listener listener = new Listener() { public void handleEvent(Event event) { switch (event.type) { case SWT.MouseDown: Rectangle rect = composite.getBounds(); if (rect.contains(event.x, event.y)) { Point pt1 = composite.toDisplay(0, 0); Point pt2 = shell.toDisplay(event.x, event.y); offset[0] = new Point(pt2.x - pt1.x, pt2.y - pt1.y); } break; case SWT.MouseMove: if (offset[0] != null) { Point pt = offset[0]; composite.setLocation(event.x - pt.x, event.y - pt.y); } break; case SWT.MouseUp: offset[0] = null; break; } } }; shell.addListener(SWT.MouseDown, listener); shell.addListener(SWT.MouseUp, listener); shell.addListener(SWT.MouseMove, listener); shell.setSize(300, 300); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Detect mouse enter, exit and hover events
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class MouseEventEnterExit {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setSize(100, 100); shell.addListener(SWT.MouseEnter, new Listener() { public void handleEvent(Event e) { System.out.println("ENTER"); } }); shell.addListener(SWT.MouseExit, new Listener() { public void handleEvent(Event e) { System.out.println("EXIT"); } }); shell.addListener(SWT.MouseHover, new Listener() { public void handleEvent(Event e) { System.out.println("HOVER"); } }); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
extends MouseTrackAdapter
<source lang="java">
import org.eclipse.swt.SWT; import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseTrackAdapter; import org.eclipse.swt.layout.GridLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Label; import org.eclipse.swt.widgets.Shell; public class MouseTrackAdapter {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); Label label = new Label(shell, SWT.SHADOW_IN | SWT.CENTER); shell.setLayout(new GridLayout()); MouseTrackAdapter listener = new MouseEnterExitListener(); label.setText("Point your cursor here ..."); label.setBounds(30, 30, 200, 30); label.addMouseTrackListener(listener); shell.setSize(260, 120); shell.open(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } }
} class MouseEnterExitListener extends MouseTrackAdapter {
public void mouseEnter(MouseEvent e) { System.out.println("Cursor enters the label"); } public void mouseExit(MouseEvent arg0) { System.out.println("Cursor leaves the label"); }
}</source>
Print mouse state and button (down, move, up)
<source lang="java">
/*******************************************************************************
* Copyright (c) 2000, 2004 IBM Corporation and others. * All rights reserved. This program and the accompanying materials * are made available under the terms of the Eclipse Public License v1.0 * which accompanies this distribution, and is available at * http://www.eclipse.org/legal/epl-v10.html * * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/
/*
* Control example snippet: print mouse state and button (down, move, up) * * For a list of all SWT example snippets see * http://www.eclipse.org/swt/snippets/ * * @since 3.1 */
import org.eclipse.swt.SWT; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Event; import org.eclipse.swt.widgets.Listener; import org.eclipse.swt.widgets.Shell; public class MouseButtonState {
public static void main(String[] args) { Display display = new Display(); final Shell shell = new Shell(display); Listener listener = new Listener() { public void handleEvent(Event e) { String string = "Unknown"; switch (e.type) { case SWT.MouseDown: string = "DOWN"; break; case SWT.MouseMove: string = "MOVE"; break; case SWT.MouseUp: string = "UP"; break; } string += ": button: " + e.button + ", "; string += "stateMask=0x" + Integer.toHexString(e.stateMask); if ((e.stateMask & SWT.BUTTON1) != 0) string += " BUTTON1"; if ((e.stateMask & SWT.BUTTON2) != 0) string += " BUTTON2"; if ((e.stateMask & SWT.BUTTON3) != 0) string += " BUTTON3"; if ((e.stateMask & SWT.BUTTON4) != 0) string += " BUTTON4"; if ((e.stateMask & SWT.BUTTON5) != 0) string += " BUTTON5"; System.out.println(string); } }; shell.addListener(SWT.MouseDown, listener); shell.addListener(SWT.MouseMove, listener); shell.addListener(SWT.MouseUp, listener); shell.pack(); shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) display.sleep(); } display.dispose(); }
}</source>
Using MouseListener
<source lang="java">
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseListener; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MouseListenerUsing {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); shell.addMouseListener(new MouseListener() { public void mouseDoubleClick(MouseEvent arg0) { System.out.println("mouseDoubleClick"); } public void mouseDown(MouseEvent arg0) { System.out.println("mouseDown"); } public void mouseUp(MouseEvent arg0) { System.out.println("mouseUp"); } }); // Display the window shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Using MouseListener, MouseMoveListener, and MouseTrackListener
- SWT divides mouse-related activity into three separate listener interfaces.
- MouseListener interface receives notification of mouse click events.
- MouseTrackListener receives notification when the mouse enters, exits, or hovers over the associated widget.
- MouseMoveListener receives notification each time the mouse moves.
Using MouseMoveListener
<source lang="java">
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseMoveListener; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MouseMoveListenerUsing {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); shell.addMouseMoveListener(new MouseMoveListener() { public void mouseMove(MouseEvent arg0) { System.out.println(arg0.x); } }); // Display the window shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>
Using MouseTrackListener
<source lang="java">
import org.eclipse.swt.events.MouseEvent; import org.eclipse.swt.events.MouseTrackListener; import org.eclipse.swt.layout.RowLayout; import org.eclipse.swt.widgets.Display; import org.eclipse.swt.widgets.Shell; public class MouseTrackListenerUsing {
public static void main(String[] args) { Display display = new Display(); Shell shell = new Shell(display); shell.setLayout(new RowLayout()); shell.addMouseTrackListener(new MouseTrackListener() { public void mouseEnter(MouseEvent arg0) { System.out.println("mouseEnter"); } public void mouseExit(MouseEvent arg0) { System.out.println("mouseExit"); } public void mouseHover(MouseEvent arg0) { System.out.println("mouseHover"); } }); // Display the window shell.open(); while (!shell.isDisposed()) { if (!display.readAndDispatch()) { display.sleep(); } } display.dispose(); }
}</source>