L19cs211sp07-6up - GUI Statics and GUI Dynamics GUI...

Info icon This preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
GUI Dynamics Lecture 19 CS211 – Spring 2007 2 GUI Statics and GUI Dynamics y 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 y 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 , ... 3 Dynamics Overview y Dynamics = causing and responding to actions What actions? Called events Need to write code that “understands” what to do when an event occurs In Java, you specify what happens by providing an object that “hears” the event In other languages, you specify what happens in response to an event by providing a function y What objects do we need? Events Event listeners 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(JFrame. 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) { try { UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ()); } catch (Exception exc) {} new Intro(); } } Brief Example Revisited 5 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(JFrame. 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) { try { UIManager. setLookAndFeel (UIManager. getSystemLookAndFeelClassName ()); } catch (Exception exc) {} new Intro(); } } Brief Example Revisited 6 The Java Event Model y Timeline User (or program) does something to a component clicks on a button, resizes a window, ...
Image of page 1

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

View Full Document Right Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern