Java by API/java.awt.event/MouseEvent
Версия от 17:43, 31 мая 2010; (обсуждение)
Содержание
MouseEvent: getClickCount()
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JFrame;
import javax.swing.JList;
import javax.swing.JScrollPane;
public class MainClass {
public static void main(final String args[]) {
final String labels[] = { "A", "B", "C", "D", "E" };
JFrame frame = new JFrame("Selecting JList");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JList jlist = new JList(labels);
JScrollPane scrollPane1 = new JScrollPane(jlist);
frame.add(scrollPane1);
MouseListener mouseListener = new MouseAdapter() {
public void mouseClicked(MouseEvent mouseEvent) {
JList theList = (JList) mouseEvent.getSource();
if (mouseEvent.getClickCount() == 2) {
int index = theList.locationToIndex(mouseEvent.getPoint());
if (index >= 0) {
Object o = theList.getModel().getElementAt(index);
System.out.println("Double-clicked on: " + o.toString());
}
}
}
};
jlist.addMouseListener(mouseListener);
frame.setSize(350, 200);
frame.setVisible(true);
}
}
MouseEvent: getComponent()
import java.awt.ruponent;
import java.awt.GridLayout;
import java.awt.KeyboardFocusManager;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Focus Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ActionListener actionListener = new ActionFocusMover();
MouseListener mouseListener = new MouseEnterFocusMover();
frame.setLayout(new GridLayout(3, 3));
for (int i = 1; i < 10; i++) {
JButton button = new JButton(Integer.toString(i));
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
if ((i % 2) != 0) {
button.setFocusable(false);
}
frame.add(button);
}
frame.setSize(300, 200);
frame.setVisible(true);
}
}
class ActionFocusMover implements ActionListener {
public void actionPerformed(ActionEvent actionEvent) {
KeyboardFocusManager manager = KeyboardFocusManager
.getCurrentKeyboardFocusManager();
manager.focusNextComponent();
}
}
class MouseEnterFocusMover extends MouseAdapter {
public void mouseEntered(MouseEvent mouseEvent) {
Component component = mouseEvent.getComponent();
if (!component.hasFocus()) {
component.requestFocusInWindow();
}
}
}
MouseEvent: getModifiers()
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;
public class MainClass {
public static void main(String args[]) {
JFrame frame = new JFrame("Button Sample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
JButton button = new JButton("Select Me");
ActionListener actionListener = new ActionListener() {
public void actionPerformed(ActionEvent actionEvent) {
System.out.println("I was selected.");
}
};
MouseListener mouseListener = new MouseAdapter() {
public void mousePressed(MouseEvent mouseEvent) {
int modifiers = mouseEvent.getModifiers();
if ((modifiers & InputEvent.BUTTON1_MASK) == InputEvent.BUTTON1_MASK) {
System.out.println("Left button pressed.");
}
if ((modifiers & InputEvent.BUTTON2_MASK) == InputEvent.BUTTON2_MASK) {
System.out.println("Middle button pressed.");
}
if ((modifiers & InputEvent.BUTTON3_MASK) == InputEvent.BUTTON3_MASK) {
System.out.println("Right button pressed.");
}
}
public void mouseReleased(MouseEvent mouseEvent) {
if (SwingUtilities.isLeftMouseButton(mouseEvent)) {
System.out.println("Left button released.");
}
if (SwingUtilities.isMiddleMouseButton(mouseEvent)) {
System.out.println("Middle button released.");
}
if (SwingUtilities.isRightMouseButton(mouseEvent)) {
System.out.println("Right button released.");
}
System.out.println();
}
};
button.addActionListener(actionListener);
button.addMouseListener(mouseListener);
frame.add(button, BorderLayout.SOUTH);
frame.setSize(300, 100);
frame.setVisible(true);
}
}
MouseEvent: getX()
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JPanel {
JButton button = new JButton("I Move");
public MainClass() {
setLayout(null);
add(button);
button.setSize(button.getPreferredSize());
button.setLocation(20, 20);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
button.setLocation(e.getX(), e.getY());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("MoveButton");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(250, 200);
frame.setLocation(200, 200);
frame.setContentPane(new MainClass());
frame.setVisible(true);
}
}
MouseEvent.getY()
import java.awt.event.*;
import javax.swing.*;
public class MainClass extends JPanel {
JButton button = new JButton("I Move");
public MainClass() {
setLayout(null);
add(button);
button.setSize(button.getPreferredSize());
button.setLocation(20, 20);
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
button.setLocation(e.getX(), e.getY());
}
});
}
public static void main(String[] args) {
JFrame frame = new JFrame("MoveButton");
frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
frame.setSize(250, 200);
frame.setLocation(200, 200);
frame.setContentPane(new MainClass());
frame.setVisible(true);
}
}
MouseEvent.MOUSE_CLICKED
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.ru/javaexamples2.
*/
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ruponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** A program that displays all the event that occur in its window */
public class EventTestPane extends JPanel {
/** The constructor: register the event types we are interested in */
public EventTestPane() {
// We"re interested in all types of events
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
| AWTEvent.FOCUS_EVENT_MASK | AWTEvent.ruPONENT_EVENT_MASK
| AWTEvent.WINDOW_EVENT_MASK);
this.setPreferredSize(new Dimension(500, 400));
}
/**
* Display mouse events that don"t involve mouse motion. The mousemods()
* method prints modifiers, and is defined below. The other methods return
* additional information about the mouse event. showLine() displays a line
* of text in the window. It is defined at the end of this class, along with
* the paintComponent() method.
*/
public void processMouseEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
type = "MOUSE_PRESSED";
break;
case MouseEvent.MOUSE_RELEASED:
type = "MOUSE_RELEASED";
break;
case MouseEvent.MOUSE_CLICKED:
type = "MOUSE_CLICKED";
break;
case MouseEvent.MOUSE_ENTERED:
type = "MOUSE_ENTERED";
break;
case MouseEvent.MOUSE_EXITED:
type = "MOUSE_EXITED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
// When the mouse enters the component, request keyboard focus so
// we can receive and respond to keyboard events
if (e.getID() == MouseEvent.MOUSE_ENTERED)
requestFocus();
}
/**
* Display mouse moved and dragged mouse event. Note that MouseEvent is the
* only event type that has two methods, two EventListener interfaces and
* two adapter classes to handle two distinct categories of events. Also, as
* seen in init(), mouse motion events must be requested separately from
* other mouse event types.
*/
public void processMouseMotionEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_MOVED:
type = "MOUSE_MOVED";
break;
case MouseEvent.MOUSE_DRAGGED:
type = "MOUSE_DRAGGED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
}
/**
* Return a string representation of the modifiers for a MouseEvent. Note
* that the methods called here are inherited from InputEvent.
*/
protected String mousemods(MouseEvent e) {
int mods = e.getModifiers();
String s = "";
if (e.isShiftDown())
s += "Shift ";
if (e.isControlDown())
s += "Ctrl ";
if ((mods & InputEvent.BUTTON1_MASK) != 0)
s += "Button 1 ";
if ((mods & InputEvent.BUTTON2_MASK) != 0)
s += "Button 2 ";
if ((mods & InputEvent.BUTTON3_MASK) != 0)
s += "Button 3 ";
return s;
}
/**
* Display keyboard events.
*
* Note that there are three distinct types of key events, and that key
* events are reported by key code and/or Unicode character. KEY_PRESSED and
* KEY_RELEASED events are generated for all key strokes. KEY_TYPED events
* are only generated when a key stroke produces a Unicode character; these
* events do not report a key code. If isActionKey() returns true, then the
* key event reports only a key code, because the key that was pressed or
* released (such as a function key) has no corresponding Unicode character.
* Key codes can be interpreted by using the many VK_ constants defined by
* the KeyEvent class, or they can be converted to strings using the static
* getKeyText() method as we do here.
*/
public void processKeyEvent(KeyEvent e) {
String eventtype, modifiers, code, character;
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
eventtype = "KEY_PRESSED";
break;
case KeyEvent.KEY_RELEASED:
eventtype = "KEY_RELEASED";
break;
case KeyEvent.KEY_TYPED:
eventtype = "KEY_TYPED";
break;
default:
eventtype = "UNKNOWN";
}
// Convert the list of modifier keys to a string
modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
// Get string and numeric versions of the key code, if any.
if (e.getID() == KeyEvent.KEY_TYPED)
code = "";
else
code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) + " ("
+ e.getKeyCode() + ")";
// Get string and numeric versions of the Unicode character, if any.
if (e.isActionKey())
character = "";
else
character = "Character=" + e.getKeyChar() + " (Unicode="
+ ((int) e.getKeyChar()) + ")";
// Display it all.
showLine(eventtype + ": " + modifiers + " " + code + " " + character);
}
/**
* Display keyboard focus events. Focus can be permanently gained or lost,
* or temporarily transferred to or from a component.
*/
public void processFocusEvent(FocusEvent e) {
if (e.getID() == FocusEvent.FOCUS_GAINED)
showLine("FOCUS_GAINED" + (e.isTemporary() ? " (temporary)" : ""));
else
showLine("FOCUS_LOST" + (e.isTemporary() ? " (temporary)" : ""));
}
/** Display Component events. */
public void processComponentEvent(ComponentEvent e) {
switch (e.getID()) {
case ComponentEvent.ruPONENT_MOVED:
showLine("COMPONENT_MOVED");
break;
case ComponentEvent.ruPONENT_RESIZED:
showLine("COMPONENT_RESIZED");
break;
case ComponentEvent.ruPONENT_HIDDEN:
showLine("COMPONENT_HIDDEN");
break;
case ComponentEvent.ruPONENT_SHOWN:
showLine("COMPONENT_SHOWN");
break;
}
}
/** Display Window events. Note the special handling of WINDOW_CLOSING */
public void processWindowEvent(WindowEvent e) {
switch (e.getID()) {
case WindowEvent.WINDOW_OPENED:
showLine("WINDOW_OPENED");
break;
case WindowEvent.WINDOW_CLOSED:
showLine("WINDOW_CLOSED");
break;
case WindowEvent.WINDOW_CLOSING:
showLine("WINDOW_CLOSING");
break;
case WindowEvent.WINDOW_ICONIFIED:
showLine("WINDOW_ICONIFIED");
break;
case WindowEvent.WINDOW_DEICONIFIED:
showLine("WINDOW_DEICONIFIED");
break;
case WindowEvent.WINDOW_ACTIVATED:
showLine("WINDOW_ACTIVATED");
break;
case WindowEvent.WINDOW_DEACTIVATED:
showLine("WINDOW_DEACTIVATED");
break;
}
// If the user requested a window close, quit the program.
// But first display a message, force it to be visible, and make
// sure the user has time to read it.
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
showLine("WINDOW_CLOSING event received.");
showLine("Application will exit in 5 seconds");
// Force the updates to appear now.
update(this.getGraphics());
// Wait five seconds
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
;
}
// Exit now
System.exit(0);
}
}
/** The list of lines to display in the window */
protected Vector lines = new Vector();
/** Add a new line to the list of lines, and request redisplay */
protected void showLine(String s) {
if (lines.size() == 20)
lines.removeElementAt(0);
lines.addElement(s);
repaint();
}
/** This method repaints the text in the window */
public void paintComponent(Graphics g) {
for (int i = 0; i < lines.size(); i++)
g.drawString((String) lines.elementAt(i), 20, i * 16 + 50);
}
public boolean isOpaque() {
return false;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(new EventTestPane(), BorderLayout.CENTER);
// Finally, set the size of the main window, and pop it up.
frame.setSize(600, 400);
frame.setVisible(true);
}
}
MouseEvent.MOUSE_ENTERED
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.ru/javaexamples2.
*/
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ruponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** A program that displays all the event that occur in its window */
public class EventTestPane extends JPanel {
/** The constructor: register the event types we are interested in */
public EventTestPane() {
// We"re interested in all types of events
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
| AWTEvent.FOCUS_EVENT_MASK | AWTEvent.ruPONENT_EVENT_MASK
| AWTEvent.WINDOW_EVENT_MASK);
this.setPreferredSize(new Dimension(500, 400));
}
/**
* Display mouse events that don"t involve mouse motion. The mousemods()
* method prints modifiers, and is defined below. The other methods return
* additional information about the mouse event. showLine() displays a line
* of text in the window. It is defined at the end of this class, along with
* the paintComponent() method.
*/
public void processMouseEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
type = "MOUSE_PRESSED";
break;
case MouseEvent.MOUSE_RELEASED:
type = "MOUSE_RELEASED";
break;
case MouseEvent.MOUSE_CLICKED:
type = "MOUSE_CLICKED";
break;
case MouseEvent.MOUSE_ENTERED:
type = "MOUSE_ENTERED";
break;
case MouseEvent.MOUSE_EXITED:
type = "MOUSE_EXITED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
// When the mouse enters the component, request keyboard focus so
// we can receive and respond to keyboard events
if (e.getID() == MouseEvent.MOUSE_ENTERED)
requestFocus();
}
/**
* Display mouse moved and dragged mouse event. Note that MouseEvent is the
* only event type that has two methods, two EventListener interfaces and
* two adapter classes to handle two distinct categories of events. Also, as
* seen in init(), mouse motion events must be requested separately from
* other mouse event types.
*/
public void processMouseMotionEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_MOVED:
type = "MOUSE_MOVED";
break;
case MouseEvent.MOUSE_DRAGGED:
type = "MOUSE_DRAGGED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
}
/**
* Return a string representation of the modifiers for a MouseEvent. Note
* that the methods called here are inherited from InputEvent.
*/
protected String mousemods(MouseEvent e) {
int mods = e.getModifiers();
String s = "";
if (e.isShiftDown())
s += "Shift ";
if (e.isControlDown())
s += "Ctrl ";
if ((mods & InputEvent.BUTTON1_MASK) != 0)
s += "Button 1 ";
if ((mods & InputEvent.BUTTON2_MASK) != 0)
s += "Button 2 ";
if ((mods & InputEvent.BUTTON3_MASK) != 0)
s += "Button 3 ";
return s;
}
/**
* Display keyboard events.
*
* Note that there are three distinct types of key events, and that key
* events are reported by key code and/or Unicode character. KEY_PRESSED and
* KEY_RELEASED events are generated for all key strokes. KEY_TYPED events
* are only generated when a key stroke produces a Unicode character; these
* events do not report a key code. If isActionKey() returns true, then the
* key event reports only a key code, because the key that was pressed or
* released (such as a function key) has no corresponding Unicode character.
* Key codes can be interpreted by using the many VK_ constants defined by
* the KeyEvent class, or they can be converted to strings using the static
* getKeyText() method as we do here.
*/
public void processKeyEvent(KeyEvent e) {
String eventtype, modifiers, code, character;
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
eventtype = "KEY_PRESSED";
break;
case KeyEvent.KEY_RELEASED:
eventtype = "KEY_RELEASED";
break;
case KeyEvent.KEY_TYPED:
eventtype = "KEY_TYPED";
break;
default:
eventtype = "UNKNOWN";
}
// Convert the list of modifier keys to a string
modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
// Get string and numeric versions of the key code, if any.
if (e.getID() == KeyEvent.KEY_TYPED)
code = "";
else
code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) + " ("
+ e.getKeyCode() + ")";
// Get string and numeric versions of the Unicode character, if any.
if (e.isActionKey())
character = "";
else
character = "Character=" + e.getKeyChar() + " (Unicode="
+ ((int) e.getKeyChar()) + ")";
// Display it all.
showLine(eventtype + ": " + modifiers + " " + code + " " + character);
}
/**
* Display keyboard focus events. Focus can be permanently gained or lost,
* or temporarily transferred to or from a component.
*/
public void processFocusEvent(FocusEvent e) {
if (e.getID() == FocusEvent.FOCUS_GAINED)
showLine("FOCUS_GAINED" + (e.isTemporary() ? " (temporary)" : ""));
else
showLine("FOCUS_LOST" + (e.isTemporary() ? " (temporary)" : ""));
}
/** Display Component events. */
public void processComponentEvent(ComponentEvent e) {
switch (e.getID()) {
case ComponentEvent.ruPONENT_MOVED:
showLine("COMPONENT_MOVED");
break;
case ComponentEvent.ruPONENT_RESIZED:
showLine("COMPONENT_RESIZED");
break;
case ComponentEvent.ruPONENT_HIDDEN:
showLine("COMPONENT_HIDDEN");
break;
case ComponentEvent.ruPONENT_SHOWN:
showLine("COMPONENT_SHOWN");
break;
}
}
/** Display Window events. Note the special handling of WINDOW_CLOSING */
public void processWindowEvent(WindowEvent e) {
switch (e.getID()) {
case WindowEvent.WINDOW_OPENED:
showLine("WINDOW_OPENED");
break;
case WindowEvent.WINDOW_CLOSED:
showLine("WINDOW_CLOSED");
break;
case WindowEvent.WINDOW_CLOSING:
showLine("WINDOW_CLOSING");
break;
case WindowEvent.WINDOW_ICONIFIED:
showLine("WINDOW_ICONIFIED");
break;
case WindowEvent.WINDOW_DEICONIFIED:
showLine("WINDOW_DEICONIFIED");
break;
case WindowEvent.WINDOW_ACTIVATED:
showLine("WINDOW_ACTIVATED");
break;
case WindowEvent.WINDOW_DEACTIVATED:
showLine("WINDOW_DEACTIVATED");
break;
}
// If the user requested a window close, quit the program.
// But first display a message, force it to be visible, and make
// sure the user has time to read it.
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
showLine("WINDOW_CLOSING event received.");
showLine("Application will exit in 5 seconds");
// Force the updates to appear now.
update(this.getGraphics());
// Wait five seconds
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
;
}
// Exit now
System.exit(0);
}
}
/** The list of lines to display in the window */
protected Vector lines = new Vector();
/** Add a new line to the list of lines, and request redisplay */
protected void showLine(String s) {
if (lines.size() == 20)
lines.removeElementAt(0);
lines.addElement(s);
repaint();
}
/** This method repaints the text in the window */
public void paintComponent(Graphics g) {
for (int i = 0; i < lines.size(); i++)
g.drawString((String) lines.elementAt(i), 20, i * 16 + 50);
}
public boolean isOpaque() {
return false;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(new EventTestPane(), BorderLayout.CENTER);
// Finally, set the size of the main window, and pop it up.
frame.setSize(600, 400);
frame.setVisible(true);
}
}
MouseEvent.MOUSE_EXITED
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.ru/javaexamples2.
*/
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ruponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** A program that displays all the event that occur in its window */
public class EventTestPane extends JPanel {
/** The constructor: register the event types we are interested in */
public EventTestPane() {
// We"re interested in all types of events
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
| AWTEvent.FOCUS_EVENT_MASK | AWTEvent.ruPONENT_EVENT_MASK
| AWTEvent.WINDOW_EVENT_MASK);
this.setPreferredSize(new Dimension(500, 400));
}
/**
* Display mouse events that don"t involve mouse motion. The mousemods()
* method prints modifiers, and is defined below. The other methods return
* additional information about the mouse event. showLine() displays a line
* of text in the window. It is defined at the end of this class, along with
* the paintComponent() method.
*/
public void processMouseEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
type = "MOUSE_PRESSED";
break;
case MouseEvent.MOUSE_RELEASED:
type = "MOUSE_RELEASED";
break;
case MouseEvent.MOUSE_CLICKED:
type = "MOUSE_CLICKED";
break;
case MouseEvent.MOUSE_ENTERED:
type = "MOUSE_ENTERED";
break;
case MouseEvent.MOUSE_EXITED:
type = "MOUSE_EXITED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
// When the mouse enters the component, request keyboard focus so
// we can receive and respond to keyboard events
if (e.getID() == MouseEvent.MOUSE_ENTERED)
requestFocus();
}
/**
* Display mouse moved and dragged mouse event. Note that MouseEvent is the
* only event type that has two methods, two EventListener interfaces and
* two adapter classes to handle two distinct categories of events. Also, as
* seen in init(), mouse motion events must be requested separately from
* other mouse event types.
*/
public void processMouseMotionEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_MOVED:
type = "MOUSE_MOVED";
break;
case MouseEvent.MOUSE_DRAGGED:
type = "MOUSE_DRAGGED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
}
/**
* Return a string representation of the modifiers for a MouseEvent. Note
* that the methods called here are inherited from InputEvent.
*/
protected String mousemods(MouseEvent e) {
int mods = e.getModifiers();
String s = "";
if (e.isShiftDown())
s += "Shift ";
if (e.isControlDown())
s += "Ctrl ";
if ((mods & InputEvent.BUTTON1_MASK) != 0)
s += "Button 1 ";
if ((mods & InputEvent.BUTTON2_MASK) != 0)
s += "Button 2 ";
if ((mods & InputEvent.BUTTON3_MASK) != 0)
s += "Button 3 ";
return s;
}
/**
* Display keyboard events.
*
* Note that there are three distinct types of key events, and that key
* events are reported by key code and/or Unicode character. KEY_PRESSED and
* KEY_RELEASED events are generated for all key strokes. KEY_TYPED events
* are only generated when a key stroke produces a Unicode character; these
* events do not report a key code. If isActionKey() returns true, then the
* key event reports only a key code, because the key that was pressed or
* released (such as a function key) has no corresponding Unicode character.
* Key codes can be interpreted by using the many VK_ constants defined by
* the KeyEvent class, or they can be converted to strings using the static
* getKeyText() method as we do here.
*/
public void processKeyEvent(KeyEvent e) {
String eventtype, modifiers, code, character;
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
eventtype = "KEY_PRESSED";
break;
case KeyEvent.KEY_RELEASED:
eventtype = "KEY_RELEASED";
break;
case KeyEvent.KEY_TYPED:
eventtype = "KEY_TYPED";
break;
default:
eventtype = "UNKNOWN";
}
// Convert the list of modifier keys to a string
modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
// Get string and numeric versions of the key code, if any.
if (e.getID() == KeyEvent.KEY_TYPED)
code = "";
else
code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) + " ("
+ e.getKeyCode() + ")";
// Get string and numeric versions of the Unicode character, if any.
if (e.isActionKey())
character = "";
else
character = "Character=" + e.getKeyChar() + " (Unicode="
+ ((int) e.getKeyChar()) + ")";
// Display it all.
showLine(eventtype + ": " + modifiers + " " + code + " " + character);
}
/**
* Display keyboard focus events. Focus can be permanently gained or lost,
* or temporarily transferred to or from a component.
*/
public void processFocusEvent(FocusEvent e) {
if (e.getID() == FocusEvent.FOCUS_GAINED)
showLine("FOCUS_GAINED" + (e.isTemporary() ? " (temporary)" : ""));
else
showLine("FOCUS_LOST" + (e.isTemporary() ? " (temporary)" : ""));
}
/** Display Component events. */
public void processComponentEvent(ComponentEvent e) {
switch (e.getID()) {
case ComponentEvent.ruPONENT_MOVED:
showLine("COMPONENT_MOVED");
break;
case ComponentEvent.ruPONENT_RESIZED:
showLine("COMPONENT_RESIZED");
break;
case ComponentEvent.ruPONENT_HIDDEN:
showLine("COMPONENT_HIDDEN");
break;
case ComponentEvent.ruPONENT_SHOWN:
showLine("COMPONENT_SHOWN");
break;
}
}
/** Display Window events. Note the special handling of WINDOW_CLOSING */
public void processWindowEvent(WindowEvent e) {
switch (e.getID()) {
case WindowEvent.WINDOW_OPENED:
showLine("WINDOW_OPENED");
break;
case WindowEvent.WINDOW_CLOSED:
showLine("WINDOW_CLOSED");
break;
case WindowEvent.WINDOW_CLOSING:
showLine("WINDOW_CLOSING");
break;
case WindowEvent.WINDOW_ICONIFIED:
showLine("WINDOW_ICONIFIED");
break;
case WindowEvent.WINDOW_DEICONIFIED:
showLine("WINDOW_DEICONIFIED");
break;
case WindowEvent.WINDOW_ACTIVATED:
showLine("WINDOW_ACTIVATED");
break;
case WindowEvent.WINDOW_DEACTIVATED:
showLine("WINDOW_DEACTIVATED");
break;
}
// If the user requested a window close, quit the program.
// But first display a message, force it to be visible, and make
// sure the user has time to read it.
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
showLine("WINDOW_CLOSING event received.");
showLine("Application will exit in 5 seconds");
// Force the updates to appear now.
update(this.getGraphics());
// Wait five seconds
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
;
}
// Exit now
System.exit(0);
}
}
/** The list of lines to display in the window */
protected Vector lines = new Vector();
/** Add a new line to the list of lines, and request redisplay */
protected void showLine(String s) {
if (lines.size() == 20)
lines.removeElementAt(0);
lines.addElement(s);
repaint();
}
/** This method repaints the text in the window */
public void paintComponent(Graphics g) {
for (int i = 0; i < lines.size(); i++)
g.drawString((String) lines.elementAt(i), 20, i * 16 + 50);
}
public boolean isOpaque() {
return false;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(new EventTestPane(), BorderLayout.CENTER);
// Finally, set the size of the main window, and pop it up.
frame.setSize(600, 400);
frame.setVisible(true);
}
}
MouseEvent.MOUSE_PRESSED
// This example is from the book _Java AWT Reference_ by John Zukowski.
// Written by John Zukowski. Copyright (c) 1997 O"Reilly & Associates.
// You may study, use, modify, and distribute this example for any purpose.
// This example is provided WITHOUT WARRANTY either expressed or
import java.awt.AWTEvent;
import java.awt.AWTEventMulticaster;
import java.awt.ruponent;
import java.awt.Frame;
import java.awt.ItemSelectable;
import java.awt.SystemColor;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.MouseEvent;
class ItemEventComponent extends Component implements ItemSelectable {
boolean selected;
int i = 0;
ItemListener itemListener = null;
ItemEventComponent() {
enableEvents(AWTEvent.MOUSE_EVENT_MASK);
}
public Object[] getSelectedObjects() {
Object o[] = new Object[1];
o[0] = new Integer(i);
return o;
}
public void addItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.add(itemListener, l);
}
public void removeItemListener(ItemListener l) {
itemListener = AWTEventMulticaster.remove(itemListener, l);
}
public void processEvent(AWTEvent e) {
if (e.getID() == MouseEvent.MOUSE_PRESSED) {
if (itemListener != null) {
selected = !selected;
i++;
itemListener.itemStateChanged(new ItemEvent(this, ItemEvent.ITEM_STATE_CHANGED,
getSelectedObjects(), (selected ? ItemEvent.SELECTED : ItemEvent.DESELECTED)));
}
}
}
}
public class MainClass extends Frame implements ItemListener {
MainClass() {
super("Listening In");
ItemEventComponent c = new ItemEventComponent();
add(c, "Center");
c.addItemListener(this);
c.setBackground(SystemColor.control);
setSize(200, 200);
}
public void itemStateChanged(ItemEvent e) {
Object[] o = e.getItemSelectable().getSelectedObjects();
Integer i = (Integer) o[0];
System.out.println(i);
}
public static void main(String args[]) {
MainClass f = new MainClass();
f.show();
}
}
MouseEvent.MOUSE_RELEASED
/*
* Copyright (c) 2000 David Flanagan. All rights reserved.
* This code is from the book Java Examples in a Nutshell, 2nd Edition.
* It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied.
* You may study, use, and modify it for any non-commercial purpose.
* You may distribute it non-commercially as long as you retain this notice.
* For a commercial use license, or to purchase the book (recommended),
* visit http://www.davidflanagan.ru/javaexamples2.
*/
import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.event.ruponentEvent;
import java.awt.event.FocusEvent;
import java.awt.event.InputEvent;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JPanel;
/** A program that displays all the event that occur in its window */
public class EventTestPane extends JPanel {
/** The constructor: register the event types we are interested in */
public EventTestPane() {
// We"re interested in all types of events
this.enableEvents(AWTEvent.MOUSE_EVENT_MASK
| AWTEvent.MOUSE_MOTION_EVENT_MASK | AWTEvent.KEY_EVENT_MASK
| AWTEvent.FOCUS_EVENT_MASK | AWTEvent.ruPONENT_EVENT_MASK
| AWTEvent.WINDOW_EVENT_MASK);
this.setPreferredSize(new Dimension(500, 400));
}
/**
* Display mouse events that don"t involve mouse motion. The mousemods()
* method prints modifiers, and is defined below. The other methods return
* additional information about the mouse event. showLine() displays a line
* of text in the window. It is defined at the end of this class, along with
* the paintComponent() method.
*/
public void processMouseEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_PRESSED:
type = "MOUSE_PRESSED";
break;
case MouseEvent.MOUSE_RELEASED:
type = "MOUSE_RELEASED";
break;
case MouseEvent.MOUSE_CLICKED:
type = "MOUSE_CLICKED";
break;
case MouseEvent.MOUSE_ENTERED:
type = "MOUSE_ENTERED";
break;
case MouseEvent.MOUSE_EXITED:
type = "MOUSE_EXITED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
// When the mouse enters the component, request keyboard focus so
// we can receive and respond to keyboard events
if (e.getID() == MouseEvent.MOUSE_ENTERED)
requestFocus();
}
/**
* Display mouse moved and dragged mouse event. Note that MouseEvent is the
* only event type that has two methods, two EventListener interfaces and
* two adapter classes to handle two distinct categories of events. Also, as
* seen in init(), mouse motion events must be requested separately from
* other mouse event types.
*/
public void processMouseMotionEvent(MouseEvent e) {
String type = null;
switch (e.getID()) {
case MouseEvent.MOUSE_MOVED:
type = "MOUSE_MOVED";
break;
case MouseEvent.MOUSE_DRAGGED:
type = "MOUSE_DRAGGED";
break;
}
showLine(mousemods(e) + type + ": [" + e.getX() + "," + e.getY() + "] "
+ "num clicks = " + e.getClickCount()
+ (e.isPopupTrigger() ? "; is popup trigger" : ""));
}
/**
* Return a string representation of the modifiers for a MouseEvent. Note
* that the methods called here are inherited from InputEvent.
*/
protected String mousemods(MouseEvent e) {
int mods = e.getModifiers();
String s = "";
if (e.isShiftDown())
s += "Shift ";
if (e.isControlDown())
s += "Ctrl ";
if ((mods & InputEvent.BUTTON1_MASK) != 0)
s += "Button 1 ";
if ((mods & InputEvent.BUTTON2_MASK) != 0)
s += "Button 2 ";
if ((mods & InputEvent.BUTTON3_MASK) != 0)
s += "Button 3 ";
return s;
}
/**
* Display keyboard events.
*
* Note that there are three distinct types of key events, and that key
* events are reported by key code and/or Unicode character. KEY_PRESSED and
* KEY_RELEASED events are generated for all key strokes. KEY_TYPED events
* are only generated when a key stroke produces a Unicode character; these
* events do not report a key code. If isActionKey() returns true, then the
* key event reports only a key code, because the key that was pressed or
* released (such as a function key) has no corresponding Unicode character.
* Key codes can be interpreted by using the many VK_ constants defined by
* the KeyEvent class, or they can be converted to strings using the static
* getKeyText() method as we do here.
*/
public void processKeyEvent(KeyEvent e) {
String eventtype, modifiers, code, character;
switch (e.getID()) {
case KeyEvent.KEY_PRESSED:
eventtype = "KEY_PRESSED";
break;
case KeyEvent.KEY_RELEASED:
eventtype = "KEY_RELEASED";
break;
case KeyEvent.KEY_TYPED:
eventtype = "KEY_TYPED";
break;
default:
eventtype = "UNKNOWN";
}
// Convert the list of modifier keys to a string
modifiers = KeyEvent.getKeyModifiersText(e.getModifiers());
// Get string and numeric versions of the key code, if any.
if (e.getID() == KeyEvent.KEY_TYPED)
code = "";
else
code = "Code=" + KeyEvent.getKeyText(e.getKeyCode()) + " ("
+ e.getKeyCode() + ")";
// Get string and numeric versions of the Unicode character, if any.
if (e.isActionKey())
character = "";
else
character = "Character=" + e.getKeyChar() + " (Unicode="
+ ((int) e.getKeyChar()) + ")";
// Display it all.
showLine(eventtype + ": " + modifiers + " " + code + " " + character);
}
/**
* Display keyboard focus events. Focus can be permanently gained or lost,
* or temporarily transferred to or from a component.
*/
public void processFocusEvent(FocusEvent e) {
if (e.getID() == FocusEvent.FOCUS_GAINED)
showLine("FOCUS_GAINED" + (e.isTemporary() ? " (temporary)" : ""));
else
showLine("FOCUS_LOST" + (e.isTemporary() ? " (temporary)" : ""));
}
/** Display Component events. */
public void processComponentEvent(ComponentEvent e) {
switch (e.getID()) {
case ComponentEvent.ruPONENT_MOVED:
showLine("COMPONENT_MOVED");
break;
case ComponentEvent.ruPONENT_RESIZED:
showLine("COMPONENT_RESIZED");
break;
case ComponentEvent.ruPONENT_HIDDEN:
showLine("COMPONENT_HIDDEN");
break;
case ComponentEvent.ruPONENT_SHOWN:
showLine("COMPONENT_SHOWN");
break;
}
}
/** Display Window events. Note the special handling of WINDOW_CLOSING */
public void processWindowEvent(WindowEvent e) {
switch (e.getID()) {
case WindowEvent.WINDOW_OPENED:
showLine("WINDOW_OPENED");
break;
case WindowEvent.WINDOW_CLOSED:
showLine("WINDOW_CLOSED");
break;
case WindowEvent.WINDOW_CLOSING:
showLine("WINDOW_CLOSING");
break;
case WindowEvent.WINDOW_ICONIFIED:
showLine("WINDOW_ICONIFIED");
break;
case WindowEvent.WINDOW_DEICONIFIED:
showLine("WINDOW_DEICONIFIED");
break;
case WindowEvent.WINDOW_ACTIVATED:
showLine("WINDOW_ACTIVATED");
break;
case WindowEvent.WINDOW_DEACTIVATED:
showLine("WINDOW_DEACTIVATED");
break;
}
// If the user requested a window close, quit the program.
// But first display a message, force it to be visible, and make
// sure the user has time to read it.
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
showLine("WINDOW_CLOSING event received.");
showLine("Application will exit in 5 seconds");
// Force the updates to appear now.
update(this.getGraphics());
// Wait five seconds
try {
Thread.sleep(5000);
} catch (InterruptedException ie) {
;
}
// Exit now
System.exit(0);
}
}
/** The list of lines to display in the window */
protected Vector lines = new Vector();
/** Add a new line to the list of lines, and request redisplay */
protected void showLine(String s) {
if (lines.size() == 20)
lines.removeElementAt(0);
lines.addElement(s);
repaint();
}
/** This method repaints the text in the window */
public void paintComponent(Graphics g) {
for (int i = 0; i < lines.size(); i++)
g.drawString((String) lines.elementAt(i), 20, i * 16 + 50);
}
public boolean isOpaque() {
return false;
}
public static void main(String[] args) {
JFrame frame = new JFrame();
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.getContentPane().add(new EventTestPane(), BorderLayout.CENTER);
// Finally, set the size of the main window, and pop it up.
frame.setSize(600, 400);
frame.setVisible(true);
}
}