9/21/99 introduction to abstract windowing toolkit (awt) part 3 – awt marc abrams virginia tech cs...

Post on 24-Dec-2015

236 Views

Category:

Documents

2 Downloads

Preview:

Click to see full reader

TRANSCRIPT

9/21/99 www.cs.vt.edu/wwtut/ 1

Introduction To Abstract Windowing Toolkit (AWT)

Part 3 – AWTMarc Abrams

Virginia Tech CS Deptcourses.cs.vt.edu/wwwtut/

9/21/99 www.cs.vt.edu/wwtut/ 2

Abstract Windowing Toolkit (AWT)

AWT classes form 3 categories: Graphics

colors, fonts, images, polygons, … draws stuff, defines events

Components GUI components: buttons, menus, frames (window),

panel (w/o window), dialog (window) Container class contains components, layout managers

Layout Managers Gives rule for graphical layout of components in a

container Ex: layout components in a grid

9/21/99 www.cs.vt.edu/wwtut/ 3

Graphics classes of the java.awt package

Show Fig. 19-1 on transparency

9/21/99 www.cs.vt.edu/wwtut/ 4

Component and layout classes of the java.awt package

Show Fig. 19-2 on transparency

9/21/99 www.cs.vt.edu/wwtut/ 5

GUI Components Button Canvas

Roll your own GUI components Checkbox

Maintains boolean state Checkboxgroup

Combines checkboxes into radio buttons

9/21/99 www.cs.vt.edu/wwtut/ 6

GUI Components (cont.) Choice

Menu of options drops down from button Currently selected option is button label

Label Displays 1 line of text (read-only)

9/21/99 www.cs.vt.edu/wwtut/ 7

GUI Components (cont.) List

Displays list of strings as N rows Adds scrollbar if necessary Allows single and multiple selection Method to return selected item indexes

9/21/99 www.cs.vt.edu/wwtut/ 8

GUI Components (cont.) Scrollbar

You specify orientation(horz, vert, and min/max range)

Event returned specifies #lines or pages to scroll, or absolute position to scroll to

9/21/99 www.cs.vt.edu/wwtut/ 9

GUI Components (cont.) TextComponent

Displays text Can be editable (getText() returns text) Can be selectable

TextField One line TextComponent setEchoCharacter() allows password entry

TextArea Multiline TextComponent

9/21/99 www.cs.vt.edu/wwtut/ 10

GUI Components (cont.) Window

Top level window without 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

9/21/99 www.cs.vt.edu/wwtut/ 11

GUI Components (cont.) Dialog

A window for dialog box Can be modal

Frame A window with

title, menubar, icon, cursor Panel

Alternative to window e.g., for display of Applet in Web browser

9/21/99 www.cs.vt.edu/wwtut/ 12

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 compiler will tell you new names the old names won’t work in future JDK releases

9/21/99 www.cs.vt.edu/wwtut/ 13

Java GUI components in Motif Show Fig. 5-3 on transparency

9/21/99 www.cs.vt.edu/wwtut/ 14

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/ 15

Example 5-1: Information Dialog

Code: Class InfoDialog Class MultiLineLabel (used in InfoDialog)

9/21/99 www.cs.vt.edu/wwtut/ 16

Example 5-1: Information Dialog

Demonstrates: Creating a window with message and

button Layout Managers for Containers Hierarchical relationship of components

9/21/99 www.cs.vt.edu/wwtut/ 17

Classes used by Example 5-1

Windowpack()

dispose()

Componentresize()show()hide()

Button (String) PanelObject

Containeradd(String, Component)

setLayout(LayoutManager)

resize

Dialog(Frame parent, String title, boolean modal)

Frame(String title)

9/21/99 www.cs.vt.edu/wwtut/ 18

Ex. 5-1import java.awt.*;

public class InfoDialog extends Dialog {

}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 19

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();

}}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 20

Ex. 5-1import 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 titlesuper(parent, title, false);

// Create and use a BorderLayout manager// with specified marginsthis.setLayout(new BorderLayout(15, 15));

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 21

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/ 22

Layout Manger Classes

BorderLayout

FlowLayout

GridLayout

GridBackLayout

Object LayoutManager

Layout Manager arranges components (Button, Dialog, List, …)

within containers (Panel, Dialog, Window, Frame) - not Button, List, …

9/21/99 www.cs.vt.edu/wwtut/ 23

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 titlesuper(parent, title, false);

// Create and use a BorderLayout manager// with specified marginsthis.setLayout(new BorderLayout(15, 15));

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 24

Ex. 5-1 (cont.)// Create the message component and add it to the windowlabel = 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}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 25

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;

}

// 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.

button = new Button("Okay");

9/21/99 www.cs.vt.edu/wwtut/ 26

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;

}

// 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.

versus

9/21/99 www.cs.vt.edu/wwtut/ 27

Java 1.1 Event Handling

java.awt.event

9/21/99 www.cs.vt.edu/wwtut/ 28

Java 1.1 Event Handling

Java Object

addListener()

Listener Object(WindowListener)

windowClosing()

Event

windowClosing

Java Objectimplements ?Listener

windowClosing()

OR

Event

9/21/99 www.cs.vt.edu/wwtut/ 29

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/ 30

Table 7-7 AWT Components and the Java 1.1

Events They Generate

Show diagram on transparency

9/21/99 www.cs.vt.edu/wwtut/ 31

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);}

http://www.ora.com/info/java/qref11/java.awt.event.MouseMotionListener.html

9/21/99 www.cs.vt.edu/wwtut/ 32

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);}

http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html

9/21/99 www.cs.vt.edu/wwtut/ 33

Illustration of event handling: Scribble Applet

Run the applet

9/21/99 www.cs.vt.edu/wwtut/ 34

Ex. 7-2: Scribble Appletimport java.applet.*; import java.awt.*; import java.awt.event.*;

public class Scribble2 extends Appletimplements 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);

}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 35

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…

http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html

9/21/99 www.cs.vt.edu/wwtut/ 36

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;

}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 37

Ex. 7-2 cont

// The other, unused methods of the MouseListener interface.

public void mouseReleased(MouseEvent e) {;}

public void mouseClicked(MouseEvent e) {;}

public void mouseEntered(MouseEvent e) {;}

public void mouseExited(MouseEvent e) {;}

// The other method of the MouseMotionListener interface.

public void mouseMoved(MouseEvent e) {;}

}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 38

Next example… 4.8 Animation! Click here to run this applet

9/21/99 www.cs.vt.edu/wwtut/ 39

Example 4-8 (Animator)

Illustrates Animation Reading images via URLs Threads

Class Animator includes array of images thread w/run method that displays images

9/21/99 www.cs.vt.edu/wwtut/ 40

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/ 41

CLASS: java.lang.ThreadJava in a Nutshell Online Quick Reference for Java 1.1

Availability: JDK 1.0

public class Thread extends Object implements Runnable {

// Public Constructorspublic Thread();public Thread(Runnable target);

public void destroy();public final native boolean isAlive();public void run();public synchronized native void start();public final void stop();

}

9/21/99 www.cs.vt.edu/wwtut/ 42

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/ 43

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. These problems will be addressed later.

**/

import java.applet.*;

import java.awt.*;

import java.net.*;

import java.util.*;

public class Animator extends Applet implements Runnable {

protected Image[] images;protected int current_image;

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 44

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++) {

images[i] = this.getImage(

this.getDocumentBase(), basename + i);

}}

// 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.

Read Image

9/21/99 www.cs.vt.edu/wwtut/ 45

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;

}

// 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.

9/21/99 www.cs.vt.edu/wwtut/ 46

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) {;}

}

}

}

// 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.

Question: Why not simply extend class Thread?

top related