Download Part3 - Virginia Tech

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Introduction To Abstract
Windowing Toolkit (AWT)
Part 3 – AWT
Marc Abrams
Virginia Tech CS Dept
courses.cs.vt.edu/wwwtut/
9/21/99
www.cs.vt.edu/wwtut/
1
Abstract Windowing Toolkit (AWT)

AWT classes form 3 categories:

Graphics



Components



GUI components: buttons, menus, frames (window),
panel (w/o window), dialog (window)
Container class contains components, layout managers
Layout Managers


9/21/99
colors, fonts, images, polygons, …
draws stuff, defines events
Gives rule for graphical layout of components in a container
Ex: layout components in a grid
www.cs.vt.edu/wwtut/
2
Graphics classes of the
java.awt package

9/21/99
Show Fig. 19-1 on transparency
www.cs.vt.edu/wwtut/
3
Component and layout classes
of the java.awt package

9/21/99
Show Fig. 19-2 on transparency
www.cs.vt.edu/wwtut/
4
GUI Components


Button
Canvas


Checkbox


Roll your own GUI components
Maintains boolean state
Checkboxgroup

9/21/99
Combines checkboxes into radio buttons
www.cs.vt.edu/wwtut/
5
GUI Components (cont.)





Choice
Menu of options drops down from button
Currently selected option is button label
Label

9/21/99
Displays 1 line of text (read-only)
www.cs.vt.edu/wwtut/
6
GUI Components (cont.)

List




9/21/99
Displays list of strings as N rows
Adds scrollbar if necessary
Allows single and multiple selection
Method to return selected item indexes
www.cs.vt.edu/wwtut/
7
GUI Components (cont.)

Scrollbar


You specify orientation
(horz, vert, and min/max range)
Event returned specifies


9/21/99
#lines or pages to scroll, or
absolute position to scroll to
www.cs.vt.edu/wwtut/
8
GUI Components (cont.)

TextComponent




TextField



Displays text
Can be editable (getText() returns text)
Can be selectable
One line TextComponent
setEchoCharacter() allows password entry
TextArea

9/21/99
Multiline TextComponent
www.cs.vt.edu/wwtut/
9
GUI Components (cont.)

Window

Top level window without







9/21/99
borders,
menubar
show() makes window visible
toFront(), toBack()
pack() resizes window to fit components in it
dispose() frees resources when window is not needed
Example use: pop-up menu
www.cs.vt.edu/wwtut/
10
GUI Components (cont.)

Dialog



Frame


A window for dialog box
Can be modal
A window with
title, menubar, icon, cursor
Panel


9/21/99
Alternative to window
e.g., for display of Applet in Web browser
www.cs.vt.edu/wwtut/
11
AWT in JDK1.0 vs JDK1.1

JDK1.1 makes several changes:


Some method names change to use set/get style
of Java beans
You can use 1.0 methods in 1.1, but


9/21/99
compiler will tell you new names
the old names won’t work in future JDK releases
www.cs.vt.edu/wwtut/
12
Java GUI components in Motif

9/21/99
Show Fig. 5-3 on transparency
www.cs.vt.edu/wwtut/
13
Java GUI components in
Windows


Show Fig. 5-4 on transparency
Swing offers pluggable look-and-feel, so
appearance is platform-independent
9/21/99
www.cs.vt.edu/wwtut/
14
Example 5-1:
Information Dialog

Code:


Class InfoDialog
Class MultiLineLabel (used in InfoDialog)

9/21/99
www.cs.vt.edu/wwtut/
15
Example 5-1:
Information Dialog

Demonstrates:



9/21/99
Creating a window with message and button
Layout Managers for Containers
Hierarchical relationship of components
www.cs.vt.edu/wwtut/
16
Classes used by Example 5-1
Button (String)
Object
Panel
Container
Component
resize()
show()
hide()
add(String, Component)
setLayout(LayoutManager)
Window
pack()
dispose()
Dialog(Frame parent,
String title,
boolean modal)
resize
Frame(String title)

9/21/99
www.cs.vt.edu/wwtut/
17
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1
import java.awt.*;
public class InfoDialog extends Dialog {
}
9/21/99
www.cs.vt.edu/wwtut/
18
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1 – Main()
public static void main(String[] args) {
Frame f = new Frame("InfoDialog Test");
f.resize(100, 100);
f.show();
InfoDialog d = new InfoDialog(f, "Help",
"The host you are trying to contact\n" +
"is not currently responding.\n" +
"Please try again later.");
d.show();
}
}
9/21/99
www.cs.vt.edu/wwtut/
19
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1
import java.awt.*;
public class InfoDialog extends Dialog {
protected Button button;
protected MultiLineLabel label;
public InfoDialog(Frame parent, String title,
String message)
{
// Create a dialog with the specified title
super(parent, title, false);
// Create and use a BorderLayout manager
// with specified margins
this.setLayout(new BorderLayout(15, 15));
9/21/99
www.cs.vt.edu/wwtut/
20
BorderLayout Manager
import java.awt.*;
import java.applet.Applet;
public class buttonDir
extends Applet {
public void init() {
setLayout(new BorderLayout());
add("North", new Button("North"));
add("South", new Button("South"));
add("East",
new Button("East"));
add("West",
new Button("West"));
add("Center", new Button("Center"));
}
} //BorderLayout(15, 15)produces gaps between regions!
9/21/99
www.cs.vt.edu/wwtut/
21
Layout Manger Classes
BorderLayout
FlowLayout
Object
LayoutManager
GridLayout
GridBackLayout
Layout Manager arranges components (Button, Dialog, List, …)
within containers (Panel, Dialog, Window, Frame) - not Button, List, …
9/21/99
www.cs.vt.edu/wwtut/
22
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Recall…
import java.awt.*;
public class InfoDialog extends Dialog {
protected Button button;
protected MultiLineLabel label;
public InfoDialog(Frame parent, String title,
String message)
{
// Create a dialog with the specified title
super(parent, title, false);
// Create and use a BorderLayout manager
// with specified margins
this.setLayout(new BorderLayout(15, 15));
9/21/99
www.cs.vt.edu/wwtut/
23
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1 (cont.)
// Create the message component and add it to the window
label = new MultiLineLabel(message, 20, 20);
this.add("Center", label);
// Create an Okay button in a Panel;
// add the Panel to the window
// Use a FlowLayout to center the button
// and give it margins.
button = new Button("Okay");
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.CENTER, 15, 15));
p.add(button);
this.add("South", p);
this.pack(); // resize window
}
9/21/99
www.cs.vt.edu/wwtut/
24
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1 (cont.)
// Pop down the window when the button is clicked.
// Java 1.0 event handling
public boolean action(Event e, Object arg)
{
if (e.target == button ) {
this.hide();
this.dispose();
return true;
} else return false;
}
button = new Button("Okay");
9/21/99
www.cs.vt.edu/wwtut/
25
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 5-1 (cont.)
// When the window gets the keyboard focus,
// give it to button.
// This allows keyboard shortcuts to pop down the dialog.
public boolean gotFocus(Event e, Object arg) {
button.requestFocus();
return true;
}
versus
9/21/99
www.cs.vt.edu/wwtut/
26
Java 1.1 Event Handling
java.awt.event
9/21/99
www.cs.vt.edu/wwtut/
27
Java 1.1 Event Handling
Java Object
addListener()
Listener Object
Event
(WindowListener)
windowClosing
windowClosing()
OR
Event
Java Object
implements ?Listener
windowClosing()
9/21/99
www.cs.vt.edu/wwtut/
28
Table 7-6

Java1.1 Event Types, Listeners, Listener Methods
ActionListener
AdjustmentListener
ComponentListener
ContainerListener
FocusListener
ItemListener
KeyListener
MouseListener
MouseMotionListener
TextListener
WindowListener
9/21/99
www.cs.vt.edu/wwtut/
29
Table 7-7

AWT Components and the Java 1.1 Events
They Generate

9/21/99
Show diagram on transparency
www.cs.vt.edu/wwtut/
30
http://www.ora.com/info/java/qref11/java.awt.event.MouseMotionListener.html
INTERFACE
java.awt.event.MouseMotionListener
Java in a Nutshell Online Quick Reference for Java 1.1
public abstract interface MouseMotionListener extends
EventListener {
// Public Instance Methods
public abstract void mouseDragged(MouseEvent e);
public abstract void mouseMoved(MouseEvent e);
}
9/21/99
www.cs.vt.edu/wwtut/
31
http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html
INTERFACE
java.awt.event.MouseListener
Java in a Nutshell Online Quick Reference for Java 1.1
public abstract interface MouseListener extends
EventListener {
// Public Instance Methods
public abstract void mouseClicked(MouseEvent e);
public abstract void mouseEntered(MouseEvent e);
public abstract void mouseExited(MouseEvent e);
public abstract void mousePressed(MouseEvent e);
public abstract void mouseReleased(MouseEvent e);
}
9/21/99
www.cs.vt.edu/wwtut/
32
Illustration of event handling:
Scribble Applet
Run the applet
9/21/99
www.cs.vt.edu/wwtut/
33
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 7-2: Scribble Applet
import java.applet.*; import java.awt.*; import java.awt.event.*;
public class Scribble2 extends Applet
implements MouseListener, MouseMotionListener {
private int last_x, last_y;
public void init() {
//
//
//
//
Tell this applet what MouseListener and MouseMotionListener
objects to notify when mouse and mouse motion events occur.
Since we implement the interfaces ourself,
our own methods are called.
this.addMouseListener(this);
this.addMouseMotionListener(this);
}
9/21/99
www.cs.vt.edu/wwtut/
34
http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html
Recall…
public abstract interface MouseListener extends
EventListener {
// Public Instance Methods
public abstract void mouseClicked(MouseEvent e);
public abstract void mouseEntered(MouseEvent e);
public abstract void mouseExited(MouseEvent e);
public abstract void mousePressed(MouseEvent e);
public abstract void mouseReleased(MouseEvent e);
}
So our code must implement *all* these methods…
9/21/99
www.cs.vt.edu/wwtut/
35
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 7-2 cont
// A method from the MouseListener interface.
// Invoked when the user presses a mouse button.
public void mousePressed(MouseEvent e) {
last_x = e.getX();
last_y = e.getY();
}
// A method from the MouseMotionListener interface.
// Invoked when the user drags the mouse with a button pressed.
public void mouseDragged(MouseEvent e) {
Graphics g = this.getGraphics();
int x = e.getX(), y = e.getY();
g.drawLine(last_x, last_y, x, y);
last_x = x; last_y = y;
}
9/21/99
www.cs.vt.edu/wwtut/
36
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 7-2 cont
// The
public
public
public
public
other, unused methods of the MouseListener interface.
void mouseReleased(MouseEvent e) {;}
void mouseClicked(MouseEvent e) {;}
void mouseEntered(MouseEvent e) {;}
void mouseExited(MouseEvent e) {;}
// The other method of the MouseMotionListener interface.
public void mouseMoved(MouseEvent e) {;}
}
9/21/99
www.cs.vt.edu/wwtut/
37
Next example… 4.8


Animation!
Click here to run this applet
9/21/99
www.cs.vt.edu/wwtut/
38
Example 4-8 (Animator)

Illustrates




Animation
Reading images via URLs
Threads
Class Animator includes


9/21/99
array of images
thread w/run method that displays images
www.cs.vt.edu/wwtut/
39
Animator Applet (HTML file)
<HTML>
<HEAD><TITLE>The Animator Applet</TITLE></HEAD>
<BODY>
<P>Here's a simple animation.</P>
<APPLET code="Animator.class" width=50 height=50>
<PARAM name="basename" value="images/circle">
<PARAM name="num_images" value="20">
</APPLET>
</BODY>
</HTML>
9/21/99
www.cs.vt.edu/wwtut/
40
CLASS: java.lang.Thread
Java in a Nutshell Online Quick Reference for Java 1.1
Availability: JDK 1.0
public class Thread extends Object implements Runnable {
// Public Constructors
public Thread();
public Thread(Runnable target);
public
public
public
public
public
void destroy();
final native boolean isAlive();
void run();
synchronized native void start();
final void stop();
}
9/21/99
www.cs.vt.edu/wwtut/
41
INTERFACE: java.lang.Runnable
Java in a Nutshell Online Quick Reference for Java 1.1
Availability: JDK 1.0
public abstract interface Runnable {
// Public Instance Methods
public abstract void run();
}
9/21/99
www.cs.vt.edu/wwtut/
42
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 4-8
/**
* This applet displays an animation. It doesn't handle errors while
* loading images. It doesn't wait for all images to be loaded before
* starting the animation.
**/
import
import
import
import
These problems will be addressed later.
java.applet.*;
java.awt.*;
java.net.*;
java.util.*;
public class Animator extends Applet implements Runnable {
protected Image[] images;
protected int current_image;
9/21/99
www.cs.vt.edu/wwtut/
43
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 4-8 cont
//
//
//
//
Read the basename and num_images parameters.
Then read in the images, using the specified base name.
For example, if basename is images/anim, read images/anim0,
images/anim1, etc. These are relative to the current document URL.
public void init() {
String basename = this.getParameter("basename");
int num_images;
try { num_images =
Integer.parseInt(this.getParameter("num_images"));
} catch (NumberFormatException e) {num_images = 0;}
images = new Image[num_images];
for(int i = 0; i < num_images; i++) {
Read Image
images[i] = this.getImage(
this.getDocumentBase(), basename + i);
}}
9/21/99
www.cs.vt.edu/wwtut/
44
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 4-8 cont
// This is the thread that runs the animation, and the methods
// that start it and stop it.
private Thread animator_thread = null;
public void start() {
if (animator_thread == null) {
animator_thread = new Thread(this);
animator_thread.start();
}
}
public void stop() {
if ((animator_thread != null) && animator_thread.isAlive())
animator_thread.stop();
// We do this so the garbage collector can reclaim the Thread object.
// Otherwise it might sit around in the Web browser for a long time.
animator_thread = null;
}
9/21/99
www.cs.vt.edu/wwtut/
45
//
//
//
//
//
//
This example is from the book "Java in a Nutshell, Second Edition".
Written by David Flanagan. Copyright (c) 1997 O'Reilly & Associates.
You may distribute this source code for non-commercial purposes only.
You may study, modify, and use this example for any purpose, as long as
this notice is retained. Note that this example is provided "as is",
WITHOUT WARRANTY of any kind either expressed or implied.
Ex. 4-8 cont
// This is the body of the thread--the method that does the animation.
public void run() {
while(true) {
if (++current_image >= images.length) current_image = 0;
this.getGraphics().drawImage(images[current_image],0,0,this);
this.getToolkit().sync(); // Force it to be drawn *now*.
try { Thread.sleep(200); } catch (InterruptedException e) {;}
}
}
}
Question: Why not simply extend class Thread?
9/21/99
www.cs.vt.edu/wwtut/
46