L11cs2110fa08 - GUI Dynamics Lecture 11 CS2110 Fall 2008...

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

View Full Document Right Arrow Icon
GUI Dynamics Lecture 11 CS2110 – Fall 2008
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 GUI Statics and GUI Dynamics Statics: what’s drawn on the screen Components buttons, labels, lists, sliders, menus, . .. Containers: components that contain other components frames, panels, dialog boxes, . .. Layout managers: control placement and sizing of components Dynamics: user interactions Events button-press, mouse-click, key- press, . .. Listeners: an object that responds to an event Helper classes Graphics , Color , Font , FontMetrics , Dimension , . ..
Background image of page 2
3 Dynamics Overview 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
Background image of page 3

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

View Full DocumentRight Arrow Icon
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 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
Background image of page 4
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 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
6 The Java Event Model Timeline User or program does something to a component clicks on a button, resizes a window, . ..
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/01/2010 for the course CS 2110 at Cornell University (Engineering School).

Page1 / 20

L11cs2110fa08 - GUI Dynamics Lecture 11 CS2110 Fall 2008...

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

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