Unit 12 1 Event-Driven Programming • Learning Outcomes o Extend the example programs to write more interesting GUI o Use nested classes and adapter classes to write medium-sized applications. • Example 1: Handling Button Events • Example 2: Handling Mouse Events • Example 3: Handling Keyboard Events • Self-check Exercise 1 • Adapter Classes • Example 4: Handling Window Events • Example 5: Handling Text Field Events • Self-check Exercise 2 • Exercises
15
Embed
Unit 121 Event-Driven Programming Learning Outcomes oExtend the example programs to write more interesting GUI oUse nested classes and adapter classes.
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Unit 12 1
Event-Driven Programming
• Learning Outcomeso Extend the example programs to write more interesting GUI
o Use nested classes and adapter classes to write medium-sized applications.
• Example 1: Handling Button Events
• Example 2: Handling Mouse Events
• Example 3: Handling Keyboard Events
• Self-check Exercise 1
• Adapter Classes
• Example 4: Handling Window Events
• Example 5: Handling Text Field Events
• Self-check Exercise 2
• Exercises
Unit 12 2
Handling Button Events
• This example builds on Example 2 of the preceding section.
• Notice that when the button is pushed in that example, nothing happens.
• We will add some code to respond to the button pushes.
• When the mouse is pushed it generates and ActionEvent.
• Thus, we will be implementing the
corresponding ActionListener interface.
• ActionListener consists of the method:
Unit 12 3
Example 1: Button Events 1 import java.awt.*; 2 import java.awt.event.*; 3 class ButtonEventTest extends AddingComponents 4 implements ActionListener{ 5 private int sum; 6 public ButtonEventTest() { 7 button.addActionListener(this); 8 } 9 public void actionPerformed(ActionEvent ae) {10 sum += 1;11 textField.setText(sum+"");12 Toolkit.getDefaultToolkit().beep();13 }14 public static void main(String args []) {15 new ButtonEventTest();16 }17 }
Unit 12 4
Handling Mouse Events
• This example illustrates how mouse events can be responded to.
• It also shows how a single listener can register with many sources.
• The event listener in this case will implement the MouseListener
interface.
• MouseListener consists of five methods:
• The program is given in the following page.
Unit 12 5
Example 2: Mouse Events 1 import java.awt.*; import java.awt.event.*; 2 public class MouseEventTest extends ButtonEventTest{ 3 public MouseEventTest(){ 4 class LightUpListener extends MouseAdapter { 5 public void mouseEntered(MouseEvent e) { 6 Component c = (Component)e.getSource(); 7 c.setBackground(Color.green); 8 } 9 public void mouseExited(MouseEvent e) {10 Component c = (Component)e.getSource();11 c.setBackground(Color.red);12 }13 }14 MouseListener listener = new LightUpListener();15 button.addMouseListener(listener);16 textField.addMouseListener(listener);17 cp.addMouseListener(listener);18 }19 public static void main(String[] args) {20 new MouseEventTest();21 }22 }
Unit 12 6
Handling Keyboard Events
• This example illustrates how keyboard events can be responded to.
• To receive KeyEvent, a component must have keyboard focus.
• We will be implementing the KeyListener interface.
• KeyListener consists of three methods:
• Notice that when you press a key, at least two events are generated.
Unit 12 7
Example 3: Keyboard Events
1 import java.awt.*; import java.awt.event.*; 2 import javax.swing.JApplet; 3 public class KeyEventTest extends JApplet implements KeyListener{ 4 private String msg = ""; 5 private int startX = 10, startY = 10; 6 public void keyPressed(KeyEvent ke){ 7 showStatus("Key Down"); 8 } 9 public void keyReleased(KeyEvent ke){showStatus("Key Up"); }10 public void keyTyped(KeyEvent ke){11 msg += ke.getKeyChar();12 repaint();13 }14 public void init(){15 requestFocus();16 addKeyListener(this);17 }18 public void paint(Graphics g){19 g.drawString(msg,startX,startY);20 }21 }
Unit 12 8
Introduction to Adapter Classes
• From previous examples, listener interfaces can have several
methods.
• A particular listener may not be
interested in all the methods.
• Nevertheless, the listener must implement
all methods in the interface.
• Java provides adapter classes for
implementing handlers selectively.
• Adapter classes provide empty implementations for the handlers.
• Most listener interfaces with two or more methods have matching
adapter classes.
Unit 12 9
Handling Window Events
• This example shows how window events can be handled.
• The listener should implement the WindowListener interface.
• WindowListener consists of seven methods:
• We will extend the corresponding WindowAdapter in this example.
Unit 12 10
Example 4: Window Events 1 import javax.swing.*;import java.awt.event.*; 2 class WindowEventTest extends JFrame{ 3 private String msg = "Are you sure you want to Quit Window?"; 4 public WindowEventTest() { 5 super("Window Event Test"); setSize(300,300); 6 addWindowListener(new WindowAdapter(){ 7 public void windowClosing(WindowEvent we) { 8 WindowEventTest obj = WindowEventTest.this; 9 int result = JOptionPane.showConfirmDialog(obj, msg);10 if (result == JOptionPane.YES_OPTION)11 System.exit(0);12 else {13 int keepOpen = WindowConstants.DO_NOTHING_ON_CLOSE;14 setDefaultCloseOperation(keepOpen);15 }16 }});17 }18 public static void main(String args [] ) {19 WindowEventTest wt = new WindowEventTest();20 wt.setVisible(true);21 }22 }
Unit 12 11
Handling Text Field Events
• This example shows how texfield events are generated and handled.
• It also illustrates the use of multiple handlers.
• Two text fields are shown handling an ActionEvent in different ways.
• The program implements Celcius to Fahrenheit temperature conversions.