lec9 - COMP201 Java Programming Part II: GUI Programming...

Info iconThis preview shows pages 1–10. Sign up to view the full content.

View Full Document Right Arrow Icon
COMP201 Java Programming Part II: GUI Programming Topic 9: Event Handling Reading: Chapter 8
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 9 / Slide 2 Outline Introduction Two examples Adapter Classes The AWT event hierarchy Individual events: K eyEvent s, MouseEvent s Separating GUI with Appl. Code
Background image of page 2
COMP201 Topic 9 / Slide 3 Event Handling Basics Java automatically generates event objects when Mouse or button clicked Menu, checkbox, or text selected Keyboard typed Scrollbar adjusted ….. It is up to the programmer to decide whether to do anything or what to do when an event happens (Programmer can create new event types.)
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 9 / Slide 4 Event Handling Basics The Java AWT event delegation model Event source : an object that generates events In order to capture interesting events and act accordingly, we need to create listener s A listener must be registered with an event source in order to listen for events produced there. A listener must implement a listener interface, which decides the response to an event. An event source can have multiple listeners and vice versa. When an event occurs, event source sends event objects to all registered listeners. Listeners use information encapsulated in the event objects to determine what to do.
Background image of page 4
COMP201 Topic 9 / Slide 5 Event Handling Basics Event Listener Any objects of class that implements an appropriate listener interface class Listener implements ActionListener { actionPerformed(Event evt) {…} } Event Source Has to register its own listeners Source.addActionL istener(Listener) Event Object evt
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 9 / Slide 6 Example 1 A button that beeps when clicked ( Beeper.java ) Create a GUI with one button class BeeperFrame extends JFrame { public BeeperFrame() { setSize( 300, 200); button = new JButton("Click Me"); getContentPane().add(button, BorderLayout.CENTER); } private JButton button; } Javax.swing.JButton has five constructors Java.awt.Container void add(Component) Adding button directly onto contentPane. Bad programming style.
Background image of page 6
COMP201 Topic 9 / Slide 7 Example 1 When a button is clicked, an ActionEvent object is produced ( java.awt.event.ActionEvent ). Listener for an ActionEvent : an object of a class that implements the ActionListener Interface class ClickListener implements ActionListener { public void actionPerformed (ActionEvent e) { Toolkit.getDefaultToolkit().beep(); } }
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
COMP201 Topic 9 / Slide 8 Example 1 Create a listener object and register it with the button: class BeeperFrame extends JFrame { public BeeperFrame() { button = new JButton("Click Me"); button . addActionListener ( new ClickListener()); } } The driver class: public class Beeper { public static void main( String [] args) { BeeperFrame frame = new BeeperFrame(); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.show(); } } // Beeper1.java
Background image of page 8
COMP201 Topic 9 / Slide 9 Example 2 Background color changes when we click on the buttons (run program). Create three buttons
Background image of page 9

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 10
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/14/2010 for the course COMP COMP 201 taught by Professor Nil during the Spring '02 term at HKUST.

Page1 / 55

lec9 - COMP201 Java Programming Part II: GUI Programming...

This preview shows document pages 1 - 10. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online