Presentation is loading. Please wait.

Presentation is loading. Please wait.

Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components.

Similar presentations


Presentation on theme: "Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components."— Presentation transcript:

1 Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components component: an object with visual representation Swing containers: JFrame, JApplet, JPanel AWT containers: Frame, Applet, Panel

2 Week 6: Basic GUI Programming Concepts in Java Java components: GUI elements such as buttons and scrollbars Windows: controls Unix: widgets Swing components: JButton, JScrollbar AWT components: Button, Scrollbar

3 Week 6: Basic GUI Programming Concepts in Java AWT(JDK 1.0, 1.1): java.awt.* - heavyweight components that rely on native GUI system Swing(Java 2): javax.swing.* - lightweight components that do not rely on native GUI

4 Week 6: Basic GUI Programming Concepts in Java basic programming steps - declare a container and components - add components to a container - register event listeners (i.e. objects that encapsulate event handlers) with the components and the container - create event listeners API documentation: inheritance hierarchy - java.awt.Container - java.awt.Component - javax.swing.JComponent (and subclasses) - javax.swing.JFrame - javax.swing.JApplet

5 Week 6: Basic GUI Programming Concepts in Java event-driven programming - a piece of code (i.e. event handler ) is attached to a GUI component - an event handler is executed when an event is activated / fired l Java: The Delegation Event Model

6 Week 6: Event Handling event source: a GUI component that generates / fires an event event: a user interaction(e.g. a click on the button) event listener: an object that has encapsulated event handlers to react to an event

7 Week 6: Event Handling l the delegation event model - a GUI element “delegates” the processing of an event to another piece of code - the event source generates/fires an event and “sends” it to event listeners

8 Week 6: Event Handling l the delegation event model - event listeners must be registered with an event source in order to receive notification Example: JButtonDemo.java, JButtonDemo2.java

9 Week 6: Event Handling programming procedure - write a class that implements an Listener interface - create an instance of this class (i.e. an event listener) - register the listener with a GUI component: add Listener ( )

10 Week 6: Event Handling l a listener interface has a list of standard event handlers (i.e. methods) API documentation - java.awt.event - event classes - listener interfaces - adapter classes

11 Week 6: Event Handling different ways of coding the event listeners - use of another top-level class: JButtonDemo - anonymous inner classes: JButtonDemo2, JFrameDemo, JFrameDemo2 - use of an adapter class: JFrameDemo4 - use of an existing class: JFrameDemo5

12 Week 6: Handling of Mouse Events MouseListener mousePressed( ), mouseClicked( ) mouseReleased( ), mouseEntered( ) mouseExited( ) MouseMotionListener mouseDragged( ), mouseMoved( ) l MouseAdapter, MouseMotionAdapter Example: MouseDemo.java

13 Week 6: Event Handling l one listener for multiple event sources Example: ActionApplet2.java Example: Time32Applet.java (Deitel & Deitel, p. 349/405)


Download ppt "Week 6: Basic GUI Programming Concepts in Java Example: JFrameDemo.java container : a screen window/applet window/panel that groups and arranges components."

Similar presentations


Ads by Google