11 Graphical User Interfaces- Dynamics

11 Graphical User Interfaces- Dynamics - 9/30/2010 GUI...

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

View Full Document Right Arrow Icon
9/30/2010 1 GUI DYNAMICS Lecture 11 CS2110 – Fall 2009 GUI Statics and GUI Dynamics ± Statics: what’s drawn on the screen ± Components ² buttons, labels, lists, sliders, menus, . .. ± Containers components ± Dynamics: user interactions ± Events ± button-press, mouse-click, key-press, . .. ± Listeners: an object that d t 2 Containers: components that contain other components ² frames, panels, dialog boxes, . .. ± Layout managers: control placement and sizing of components responds to an event ± Helper classes ± Graphics, Color, Font, FontMetrics, Dimension, . .. Dynamics Overview 3 ± Dynamics = causing and responding to actions ± What actions? ² Called events: mouse clicks, mouse motion, dragging, keystrokes ² We would like to write code (a handler ) that is invoked when an event occurs so that the program can respond appropriately ² In Java, you can intercept events by providing an object that “hears” the event a listener ± What objects do we need to know about? ± Events ± Event listeners import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Intro extends JFrame { private int count = 0; private JButton myButton = new JButton( "Push Me!" ); private JLabel label = new JLabel( "Count: " + count ); public Intro() { setDefaultCloseOperation( EXIT_ON_CLOSE ); setLayout( new FlowLayout(FlowLayout. LEFT )); //set layout manager add( myButton ); //add components Brief Example Revisited 4 ); add( label ); label .setPreferredSize( new Dimension(60, 10)); myButton .addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { count ++; label .setText( "Count: " + count ); } }); pack(); setVisible( true ); } public static void main(String[] args) { new Intro(); } } Brief Example Revisited import javax.swing.*; import java.awt.*; import java.awt.event.*; public class Intro extends JFrame { private int count = 0; private JButton myButton = new JButton( "Push Me!" ); private JLabel label = new JLabel("Count: " + count); public Intro() { setDefaultCloseOperation( EXIT_ON_CLOSE ); setLayout(new FlowLayout(FlowLayout. LEFT )); //set layout manager add(label); label.setPreferredSize(new Dimension(60, 10)); myButton .addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { count ++; label .setText( "Count: " + count ); } }); pack(); setVisible(true); } public static void main(String[] args) { new Intro(); } } The Java Event Model 6 ± Timeline ± User or program does something to a component ² clicks on a button, resizes a window, . .. ± Java issues an event object describing the event ± A special type of object (a listener) “hears” the event ² Th li t h th d th t “h dl ” th The listener has a method that “handles” the event ² The handler does whatever the programmer programmed ± What you need to understand ± Events : How components issue events ± Listeners : How to make an object that listens for events ± Handlers : How to write a method that responds to an event
Background image of page 1

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

View Full DocumentRight Arrow Icon
9/30/2010 2 Events: How your application learns when something interesting happens ± Basic idea: You register a listener and Java calls it ± The argument is an “event”: a normal Java object ± Events are normally created by the Java runtime system ± ActionEvent ±
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 5

11 Graphical User Interfaces- Dynamics - 9/30/2010 GUI...

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

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