Bài giảng Công nghệ Java: Chương 10 - Lê Nhật Tùng

20 8 0
Bài giảng Công nghệ Java: Chương 10 - Lê Nhật Tùng

Đang tải... (xem toàn văn)

Tài liệu hạn chế xem trước, để xem đầy đủ mời bạn chọn Tải xuống

Thông tin tài liệu

– To detect when the user clicks an onscreen button (or does the keyboard equivalent), a program must have an object that implements the ActionListener interface. – The program must r[r]

(1)

CÔNG NGHỆ JAVA

CH10 Handling Mouse and

Keyboard Events - SWING components

Giảng viên: Lê Nhật Tùng

(2)

Topics in This Section

Basic of Event Handling

General asynchronous event-handling strategy Event-handling options

Handling events with separate listeners

Handling events by implementing interfaces Handling events with named inner classes

Handling events with anonymous inner classes

The standard AWT listener types Subtleties with mouse events

(3)

Basic of Event Handling

Every time the user types a character or pushes a mouse button, an event occurs

Events: Objects that describe what happened

Event sources: The generator of an event

(4)

Delegation Model of Event

An event can be sent to many event handlers. Event handlers register with components when

(5)

A Listener Example

public class TestButton {

JFrame frame;

JButton button;

public TestButton() {

frame = new JFrame("Test");

button = new JButton("Press Me!");

button.setActionCommand("ButtonPressed");

// register event listener for button

button.addActionListener(new ButtonHandler());

frame.add(button, BorderLayout.CENTER);

}

public void launchFrame() {

frame.pack();

(6)

A Listener Example (Contd.)

class ButtonHandler implements ActionListener {

public void actionPerformed(ActionEvent e) {

System.out.println("Action occurred");

System.out.println("Button’s command is: " +

e.getActionCommand()); }

(7)

Event Categories

(8)

Listener Type

Some Events and Their Associated Event Listeners:

Act that Results in the Event Listener Type User clicks a button, presses Enter while

typing in a text field, or chooses a menu item

ActionListener

User closes a frame (main window) WindowListener User presses a mouse button while the

cursor is over a component MouseListener

(9)

Listeners

•ActionListener Interface:

–Has only one method: actionPerformed(ActionEvent)

–To detect when the user clicks an onscreen button (or does the keyboard equivalent), a program must have an object that implements the ActionListener interface.

–The program must register this object as an action listener on the button (the event source), using the

addActionListener() method.

(10)

Listeners (Contd.)

MouseListener interface:

To detect the mouse clicking, a program must have an object that implements the

MouseListener interface.

This interface includes several events including

mouseEntered, mouseExited, mousePressed,

mouseReleased, and mouseClicked.

(11)

Listeners (Contd.)

Implementing Multiple Interfaces:

A class can be declared with Multiple Interfaces by using comma separation:

implements MouseListener, MouseMotionListener

Listening to Multiple Sources:

Multiple listeners cause unrelated parts of a program to react to the same event.

(12)

General Strategy

Determine what type of listener is of interest 11 standard AWT listener types.

ActionListener, ItemListener, KeyListener,

MouseListener, MouseMotionListener, TextListener, AdjustmentListener, ComponentListener,

ContainerListener, FocusListener, WindowListener

Define a class of that type

Implement interface (KeyListener, MouseListener, …) Extend class (KeyAdapter, MouseAdapter, etc.)

Register an object of your listener class with the component

comp.addXxxListener(new MyListenerClass());

(13)(14)

Separate Listener: Simple Case

Listener does not need to call any methods

of the window to which it is attached public class MouseClickFrame extends JFrame {

public MouseClickFrame(String title) {

super(title);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addMouseListener(new ClickListener());

setSize(300, 400);

setVisible(true);

}

public static void main(String[] args) {

new MouseClickFrame("Mouse Click");

(15)

Separate Listener: Simple Case

class ClickListener extends MouseAdapter {

private int radius = 25;

public void mousePressed(MouseEvent event) { JFrame comp = (JFrame) event.getSource(); Graphics g = comp.getGraphics();

g.fillOval(event.getX() - radius,

event.getY() - radius,

2 * radius, * radius); }

(16)

Separate Listener: Simple Case (Cont')

•Register an object of ClickListener for frame

•Define a class ClickListener extends

MouseAdapter class and override mousePress()

method.

•General event Handling :

–Call event.getSource() to obtain a reference to window or GUI component from which event originated –Cast result to type of interest

–Call methods on that reference

(17)

MouseListener and MouseAdapter

public interface MouseListener {

public void mouseClicked(MouseEvent e);

public void mousePressed(MouseEvent e);

public void mouseReleased(MouseEvent e);

public void mouseEntered(MouseEvent e);

public void mouseExited(MouseEvent e); }

public abstract class MouseAdapter implements MouseListener {

public void mouseClicked(MouseEvent e) {}

public void mousePressed(MouseEvent e) {}

public void mouseReleased(MouseEvent e) {}

public void mouseEntered(MouseEvent e) {}

(18)

Implementing a Listener Interface

class ClickListener implements MouseListener {

private int radius = 25;

public void mousePressed(MouseEvent event) { JFrame app = (JFrame) event.getSource(); Graphics g = app.getGraphics();

g.fillOval(event.getX() - radius,

event.getY() - radius,

2 * radius, * radius); }

public void mouseClicked(MouseEvent e) { }

public void mouseReleased(MouseEvent e) { }

public void mouseEntered(MouseEvent e) { }

(19)

Implementing a Listener Interface

public class MouseClickFrame extends JFrame {

public MouseClickFrame(String title) {

super(title);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

addMouseListener(new ClickListener());

setSize(300, 400);

setVisible(true);

}

public static void main(String[] args) {

new MouseClickFrame("Mouse Click");

(20)

Adapters vs Interfaces: Method Signature Errors

•What if you goof on the method signature?

–public void mousepressed(MouseEvent e)

–public void mousePressed()

•Interfaces

–Compile time error

•Adapters

–No compile time error, but nothing happens at run time when you press the mouse

•Solution for adapters: @Override annotation

–Whenever you think you are overriding a method, put

Ngày đăng: 09/03/2021, 05:55

Tài liệu cùng người dùng

  • Đang cập nhật ...

Tài liệu liên quan