CS2_35_GUI_Events_Parts1_2

CS2_35_GUI_Events_Parts1_2 - CS2 Module 35 Category:...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: CS2 Module 35 Category: Elements of Java Topic: GUI Events Objectives Events Interclass Communication CS 2 Introduction to Object Oriented Programming Module 35 Elements of Java GUI Events Nota Bene This lecture presentation contains a number of "hidden" slides. Feel free to read, study and learn from them on your own! Today's Plan for Fun Events What is an event? Simple (?) Example Swing Components JFrames JComponents An example Swing Component Design (MVC/UI-Delegate) Events Behind the scenes, the Java runtime environment is monitoring many things When any of a number of things happen an event is said to occur. Sometimes the terminology is an event gets fired" Examples of the types of things that can "fire" events Pressing a key on the keyboard Clicking on a component (like a button) Entering a component with the mouse pointer Have a timer "time-out" Events Moving the mouse around any reasonably complicated GUI can literally cause hundreds if not thousands of events to occur Events will be ignored except for the ones that you tell Java that you are interested in doing something about Java maintains a data structure of all the events that you have decided to handle and looks up events and does what you tell it to do. Remember this??? import java.awt.*; public class HelloGUI { public static void main (String[ ] arg) { System.out.println("About to make GUI"); Frame f = new Frame ("Hello GUIs"); f.setSize( 200, 200 ); f.show(); System.out.println("Finished making GUI"); }// main }// class HelloGUI What didn't work??? Making it work Determine which event occurs when the "Close the Window" button is pressed The API is your friend The lecture notes are your friend Hint: In this case it's an event called "Window Closing" You decide what class is going to handle this event It might be the actual class which has the window It can be any other class Write the method (and class?) that will handle the event. When this event occurs, Java is going to go to the class that you identify as the event handler or Listener . It will look for a method called: public void windowClosing(WindowEvent e) Java will report an error to you if this class doesn't have this method. How might the designers of Java be guaranteed that you will implement this method? An Interface!!! // Note: found in java.awt.event public interface WindowListener extends EventListener { void windowActivated(WindowEvent e); void windowClosed(WindowEvent e); void windowClosing(WindowEvent e); void windowDeactivated(WindowEvent e); void windowDeiconified(WindowEvent e); void windowIconified(WindowEvent e); void windowOpened(WindowEvent e); } So we could write a class like this import java.awt.*; import java.awt.event.*; public class Handler implements WindowListener { public void windowActivated(WindowEvent e) {} public void windowClosed(WindowEvent e) {} public void windowClosing(WindowEvent e)...
View Full Document

Page1 / 146

CS2_35_GUI_Events_Parts1_2 - CS2 Module 35 Category:...

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

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