Transcript Document

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
Lecture 26
1
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
Lecture 26
2
10/joi
Lecture 26
3
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
Lecture 26
4
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
39
40
button = new Button( "Press Me" );
this.add( button );
font = new Font("Garamond",
Font.BOLD, 48);
Lecture 26
5
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
Lecture 26
6
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 }
Lecture 26
7
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 }
Lecture 26
8
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 }
Lecture 26
9
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);
}
Lecture 26
10
Running the program
• as an applet from a browser: file joi.html
<html>
This file is written in html
(“hypertext markup language”),
<body>
not Java. Browsers understand html.
<applet code="JOIPanel.class"
height=100 width=400>
</applet>
main method
never runs.
Execution starts with init.
</body>
</html>
Lecture 26
11
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
Lecture 26
12
Lecture 26
13
All together now
public class JOIApplet extends Applet
implements ActionListener
{ ...
public void actionPerformed
( ActionEvent e )
{
currentMessage =
currentMessage.equals(MESSAGE1) ?
MESSAGE2 : MESSAGE1;
this.repaint();
}
Lecture 26
14
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
Lecture 26
15
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
.
Lecture 26
16
java Juno -e -g
Lecture 26
17