Notes
Slide Show
Outline
1
CS110 Lecture 26
Thursday, May 6, 2004
  • This lecture not delivered from slides
  • Announcements
    • final exam Thursday, May 20, 8:00 AM McCormack, Floor 01, Room 0608
    • wise1 due next Tuesday
  • Agenda
    • Questions (WISE and otherwise)
    • GUI programming
    • Interfaces
2
GUI Programming
  • Fun
    • some introductory courses start here
    • requires hardware support (PCs)
    • maybe more in CS110 in time
  • GUI syntax: what windows look like - containers and components
  • GUI semantics: how windows behave – event driven programming
  • System does lots of work for you
  • Learn the APIs, use software tools
3
10/joi
4
GUI Syntax
(what windows look like)
  • AWT (Abstract Windowing Toolkit)
  • Abstract class Container
    • subclasses: Panel, Window, Frame
    • methods: add, setLayout
  • Abstract class Component
    • subclasses: Button, Checkbox, Textfield,
  • Up to date Java GUI programmers use swing:
    • classes: JButton, JTextfield,…
  • or they use GUI builders
5
Building window look and feel
  • class JOIPanel extends Applet extends Panel
  • init method creates a button and adds it to this Panel, sets font:
  • 38  button = new Button( "Press Me" );
  • 39  this.add( button );
  • 40  font = new Font("Garamond",          Font.BOLD, 48);
6
Building window behavior
  • Add a listener to the button
  • 43  button.addActionListener(                      new JOIButtonListener( this ) );
  • When button is pressed, system sends an  actionPerformed message to the listener
  • To see what happens, look at method actionPerformed in class JOIButtonListener
7
JOIButtonListener
  • // constructor remembers the Panel
  • 27 public JOIButtonListener                     ( JOIPanel panel )
  • 28 {
  • 29    this.panel = panel;
  • 30 }
  • // send panel a changeMessage message now!
  • 41 public void actionPerformed              ( ActionEvent e )
  • 42 {
  • 43    panel.changeMessage();
  • 44 }
8
Changing the message
  • In JOIPanel: change the message and ask the system to repaint:
  • 51 public void changeMessage()
  • 52 {
  • 53   currentMessage =
  • 54      currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1;
  • 55   this.repaint();
  • 56 }
9
Changing the message
  • repaint( ) (which is really super.repaint( )) invokes paint( )
  • A Graphics object is like a programmable pen or paintbrush


  • 67 public void paint(Graphics g)
  • 68 {
  • 69    g.setColor(Color.black);
  • 70    g.setFont(font);
  • 71    g.drawString(currentMessage, 40, 75);
  • 72 }
10
Running the program
  • as an application
  • > java JOIPanel
  • public static void main( String[] args )
  • {
  •    Terminal t     = new Terminal();
  •    Frame frame    = new Frame();
  •    JOIPanel panel = new JOIPanel();
  •    panel.init();
  •    frame.add(panel);
  •    frame.setSize(400,120);
  •    frame.show();
  •    t.readLine("return to close window ");
  •    System.exit(0);
  • }
11
Running the program
  • as an applet from a browser: file joi.html
  • <html>
  • <body>
  • <applet code="JOIPanel.class"                height=100 width=400>
  • </applet>
  • </body>
  • </html>
12
Interfaces
  • An interface is like a very abstract class
    • no fields
    • only abstract methods
  • A class that implements an interface promises to implement its abstract methods


  • public class JOIButtonListener   implements ActionListener


13
 
14
All together now
  • public class JOIApplet extends Applet   implements ActionListener
  • { ...


  • public void actionPerformed                     ( ActionEvent e )
  • {
  •   currentMessage =    currentMessage.equals(MESSAGE1) ? MESSAGE2 : MESSAGE1;
  • this.repaint();
  • }
15
Interfaces in Collection framework
  • Inheritance hierarchy (classes)
  • java.lang.Object                       java.util.AbstractMap                        java.util.TreeMap
  • TreeMap implements interfaces:
    • Cloneable, Map, Serializable, SortedMap
  • AbstractMap implements Map
  • Map foo = new TreeMap( ) // legal
16
Juno 10 has a GUI
  • Juno CLI prompts for input when it wants it       => the program is in control
  • Juno GUI sits waiting for user input from anywhere mouse can go, then takes action                                                               => the user is in control (event driven)
  • > java Juno –g
  • .
17