1. Trang chủ
  2. » Công Nghệ Thông Tin

Lecture An introduction to computer science using java (2nd Edition): Chapter 11 - S.N. Kamin, D. Mickunas, E. Reingold

11 78 0
Tài liệu được quét OCR, nội dung có thể không chính xác

Đ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

Thông tin cơ bản

Định dạng
Số trang 11
Dung lượng 146,76 KB

Nội dung

Chapter 11 - Java AWT part I: Mouse events (Optional). In this chapter we will: introduce event driven programming, show how a program can respond to mouse events (e.g. clicks and mouse movements), demonstrate how to implement a listener interface, show how mouse events can be used to build highly interactive programs.

Trang 2

Chapter Preview

In this chapter we will:

introduce event driven programming

show how a program can respond to mouse

events (e.g clicks and mouse movements) demonstrate how to implement a listener Interface

show how mouse events can be used to build

Trang 3

Abstract Windowing Toolkit

(AWT)

¢ Part of the Java distribution, but not part of the Java language itself

¢ AWT Is library of classes and methods used by Java programmers that supported are by the basic Java runtime libraries

¢ Even though this chapter is labeled optional, itis not possible to be a complete Java

Trang 4

Java Events

Events are occurrences outside of the

program to which the program must respond (e.g user key press)

Event managers are methods that first catch the event

Listener classes send messages to an event manager requesting to be notified when a

particular message occurs

Trang 5

Creating Mouse Click Listeners

1 Import the java.awt.event package

2 Inthe class header, add the words

implements MouseListener

3 Send the addMouseListerner(this) message to the mouse event manager

(should be done in the constructor method)

4 Define methods mouseClicked,

Trang 6

Mouse Click Listener Template

import java.awt.event.*;

public class classname implements MouseListener {

// include manager addMouseListener(this); // in some initialization method

Trang 7

Finding Mouse Location

¢ Inside mouseClicked the argument e of type

MouseE vent can be used to find the location of the mouse cursor

¢ The calle.getX(_) returns the value of the

horizontal coordinate of the mouse cursor

position

¢ The calle.getY(_ ) returns the value of the

vertical coordinate of the mouse cursor

Trang 8

Creating Mouse Motion Listeners

1 2

Import the java.awt.event package

In the class header, add the words

implements MouseMotionListener Send the message

addMouseMotionListerner(this) to the mouse event manager (should be

done in the constructor method)

Define methods mouseMoved and

Trang 9

Mouse Motion Listener Template

import java.awt.event.*;

public class classname implements MouseMotionListener {

// include manager addMouseListener(this); // in some initialization method

Trang 10

Mouse Motion

* mouseMoved will be called each time the

mouse makes a significantly large movement * mouseDragged will be called instead if thre

Trang 11

Listening to All Mouse Events

¢ Two catch all seven mouse events use the

following class header

public class classname implements

MouseListener, MouseMotionListener {

¢ You then need to define all seven mouse

methods mouseMoved, mouseDragged,

Ngày đăng: 11/01/2020, 18:47