principles of
play

Principles of Many different types of events occur when user - PDF document

GUI Events Principles of Many different types of events occur when user interacts with a GUI Computer Science II Information about any event is stored in object of a subclass of AWTEvent Three parts to event-handling mechanism


  1. GUI Events Principles of  Many different types of events occur when user interacts with a GUI Computer Science II  Information about any event is stored in object of a subclass of AWTEvent  Three parts to event-handling mechanism – Event source • Component with which user interacts – Event object • Info about event that occurred: reference to the event source, Prof. Nadeem Abdul Hamid event-specific information used by the listener to process it – Event listener CSC 121A – Spring 2005 • Object that is notified when an event occurs • Particular method receives an event object when the listener Lecture Slides 5 - Event Handling is notified of the event and Exception Control Structures CSC 121A - Berry College - Spring 2005 2 Object Event Handling ActionEvent EventObject  For each event-object type, there is usually ContainerEvent AdjustmentEvent a corresponding event-listener interface AWTEvent FocusEvent  Each interface specifies one or more event- ItemEvent handling methods PaintEvent TextEvent  When event occurs, GUI component WindowEvent notifies registered listeners by calling ComponentEvent appropriate event-handling method InputEvent KeyEvent MouseEvent MouseWheelEvent CSC 121A - Berry College - Spring 2005 3 CSC 121A - Berry College - Spring 2005 4 <<interface>> Adapter Classes Common Event- ActionListener <<interface>> Listener AdjustmentListener  Many listener interfaces specify multiple <<interface>> Interfaces ComponentListener methods (e.g. MouseListener ) <<interface>>  Sometimes application only needs a single ContainerListener handler method and would just define <<interface>> FocusListener empty bodies for other methods specified <<interface>> <<interface>> WindowListener in the interface ItemListener  Instead of defining empty bodies oneself, a <<interface>> KeyListener handler class can just extend one of the <<interface>> adapter classes MouseListener – Adapter class implements an interface and <<interface>> MouseMotionListener provides a default implementation (i.e. empty <<interface>> method body) for methods of the interface TextListener CSC 121A - Berry College - Spring 2005 5 CSC 121A - Berry College - Spring 2005 6 1

  2. Subclassing JPanel for Mouse Adapter Classes and Interfaces Drawing  ComponentAdapter ComponentListener  Uses a JPanel as dedicated draw area  ContainerAdapter ContainerListener  Extends an adapter class for event handling  FocusAdapter FocusListener  KeyAdapter KeyListener  MouseAdapter MouseListener  lec05/Painter.java  MouseMotionAdapter MouseMotionListener  lec05/PaintPanel.java  WindowAdapter WindowListener  Example: lec05/MouseDetails.java lec05/MouseDetailFrame.java CSC 121A - Berry College - Spring 2005 7 CSC 121A - Berry College - Spring 2005 8 Key Event Handling Component Layout  Key events generated when keys on  Provided to arrange GUI components in a keyboard are pressed and released container for presentation  KeyListener interface declares methods  Three ways to arrange components – keyPressed – Absolute positioning: set layout to null , specify absolute position of each component w.r.t. – keyReleased upper-left corner of container – keyTyped – Layout managers: simpler and faster but not as – Each receives a KeyEvent object as argument much control over positioning  Example: – Visual programming using IDE design tool – lec05/KeyDemo.java – lec05/KeyDemoFrame.java CSC 121A - Berry College - Spring 2005 9 CSC 121A - Berry College - Spring 2005 10 Layout Managers Complex Layouts Using JPanels  FlowLayout  A JPanel is a container and thus may have – Default for JPanel components, including other JPanels, added – Places components left-to-right in order they are to it added  Create complex layouts using nested  BorderLayout JPanels and layout managers – Default for JFrame and other windows – Arranges components in five areas: NORTH, SOUTH, EAST, WEST, CENTER  Example – Limits container to having 5 components – lec05/PanelDemo.java  GridLayout – lec05/PanelFrame.java – Arranges components in rows and columns CSC 121A - Berry College - Spring 2005 11 CSC 121A - Berry College - Spring 2005 12 2

  3. Exception Handling Try-Catch Blocks  Exception: indication of a problem during  A try block encloses code that might throw an exception and code that should not execute if the program’s execution exception occurs  Exception handling  A catch block takes an exception parameter – Allows programmers to remove error-handling followed by code that catches (i.e. receives) and code from “main line” of program’s execution handles the exception – Enables writing robust and fault-tolerant – Executes only when an exception arises somewhere in the programs try block – At least one catch or finally block must immediately  Exceptions surface from follow the try block – Explicitly mentioned code in a try block  After exception is handled, control resumes after – Calls to other methods (may be deeply nested) the last catch block – Java Virtual Machine (JVM) as it executes  Uncaught exceptions (i.e. with no matching catch block) cause the program to terminate  Example: lec05/ExceptionTest.java CSC 121A - Berry College - Spring 2005 13 CSC 121A - Berry College - Spring 2005 14 Java Exception Hierarchy Types of Exceptions Object  Unchecked – derived from Throwable RunTimeException  Checked – must be caught by a method or else must be listed in a throws clause of the Error Exception method (e.g. IOException)  Errors are liked unchecked exceptions RunTimeException LinkageError – Usually, should not be caught and do not ThreadDeath ArithmeticException require listing in a throws clause OutOfMemoryError NullPointerException ClassNotFoundException IOException CSC 121A - Berry College - Spring 2005 15 CSC 121A - Berry College - Spring 2005 16 Exception Example  lec05/ExceptionTest2.java – Defining and throwing user-defined exceptions – Optional finally block: executed no matter what happens – Multiple catch blocks matching exception type CSC 121A - Berry College - Spring 2005 17 3

Recommend


More recommend