Presentation is loading. Please wait.

Presentation is loading. Please wait.

9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/

Similar presentations


Presentation on theme: "9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/"— Presentation transcript:

1 9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/

2 9/21/99www.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

3 9/21/99www.cs.vt.edu/wwtut/3 Graphics classes of the java.awt package java.awt Show Fig. 19-1 on transparency

4 9/21/99www.cs.vt.edu/wwtut/4 Component and layout classes of the java.awt packagejava.awt Show Fig. 19-2 on transparency

5 9/21/99www.cs.vt.edu/wwtut/5 GUI Components Button Canvas Roll your own GUI components Checkbox Maintains boolean state Checkboxgroup Combines checkboxes into radio buttons

6 9/21/99www.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) 

7 9/21/99www.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

8 9/21/99www.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 9/21/99www.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

10 9/21/99www.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

11 9/21/99www.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

12 9/21/99www.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

13 9/21/99www.cs.vt.edu/wwtut/13 Java GUI components in Motif Show Fig. 5-3 on transparency

14 9/21/99www.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

15 9/21/99www.cs.vt.edu/wwtut/15 Example 5-1: Information Dialog Code: Class InfoDialogInfoDialog Class MultiLineLabel (used in InfoDialog)MultiLineLabel 

16 9/21/99www.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

17 9/21/99www.cs.vt.edu/wwtut/17 Classes used by Example 5-1 Window pack() dispose() Component resize() show() hide() Button (String) PanelObject Container add(String, Component) setLayout(LayoutManager)  resize Dialog(Frame parent, String title, boolean modal) Frame(String title)

18 9/21/99www.cs.vt.edu/wwtut/18 Ex. 5-1 import java.awt.*; public class InfoDialog extends Dialog {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.

19 9/21/99www.cs.vt.edu/wwtut/19 Ex. 5-1 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.

20 9/21/99www.cs.vt.edu/wwtut/20 Ex. 5-1 import java.awt.*; public class InfoDialog extends Dialog {Dialog protected Button button;Button protected MultiLineLabel label; public InfoDialog(Frame parent, String title, String message)Frame { // 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));BorderLayout // 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.

21 9/21/99www.cs.vt.edu/wwtut/21 BorderLayout 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!

22 9/21/99www.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, …

23 9/21/99www.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 title super(parent, title, false); // Create and use a BorderLayout manager // with specified margins this.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.

24 9/21/99www.cs.vt.edu/wwtut/24 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 } // 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.

25 9/21/99www.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");

26 9/21/99www.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

27 9/21/99www.cs.vt.edu/wwtut/27 Java 1.1 Event Handling java.awt.event

28 9/21/99www.cs.vt.edu/wwtut/28 Java 1.1 Event Handling Java Object addListener() Listener Object (WindowListener) windowClosing() Event windowClosing Java Object implements ?Listener windowClosing() OR Event

29 9/21/99www.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

30 9/21/99www.cs.vt.edu/wwtut/30 Table 7-7 AWT Components and the Java 1.1 Events They Generate Show diagram on transparency

31 9/21/99www.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 { EventListener // Public Instance Methods public abstract void mouseDragged(MouseEvent e);MouseEvent public abstract void mouseMoved(MouseEvent e);MouseEvent } http://www.ora.com/info/java/qref11/java.awt.event.MouseMotionListener.html

32 9/21/99www.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 { EventListener // Public Instance Methods public abstract void mouseClicked(MouseEvent e);MouseEvent public abstract void mouseEntered(MouseEvent e);MouseEvent public abstract void mouseExited(MouseEvent e);MouseEvent public abstract void mousePressed(MouseEvent e);MouseEvent public abstract void mouseReleased(MouseEvent e);MouseEvent } http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html

33 9/21/99www.cs.vt.edu/wwtut/33 Illustration of event handling: Scribble Applet Run the applet

34 9/21/99www.cs.vt.edu/wwtut/34 Ex. 7-2Ex. 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); } // 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.

35 9/21/99www.cs.vt.edu/wwtut/35 Recall… public abstract interface MouseListener extends EventListener { EventListener // Public Instance Methods public abstract void mouseClicked(MouseEvent e);MouseEvent public abstract void mouseEntered(MouseEvent e);MouseEvent public abstract void mouseExited(MouseEvent e);MouseEvent public abstract void mousePressed (MouseEvent e);MouseEvent public abstract void mouseReleased(MouseEvent e);MouseEvent } So our code must implement *all* these methods… http://www.ora.com/info/java/qref11/java.awt.event.MouseListener.html

36 9/21/99www.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.

37 9/21/99www.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.

38 9/21/99www.cs.vt.edu/wwtut/38 Next example… 4.8 Animation! Click here to run this applethere

39 9/21/99www.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

40 9/21/99www.cs.vt.edu/wwtut/40 Animator Applet (HTML file) The Animator Applet Here's a simple animation.

41 9/21/99www.cs.vt.edu/wwtut/41 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 {ObjectRunnable // Public Constructors public Thread(); public Thread(Runnable target);Runnable public void destroy(); public final native boolean isAlive(); public void run(); public synchronized native void start(); public final void stop(); }

42 9/21/99www.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(); }

43 9/21/99www.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.

44 9/21/99www.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

45 9/21/99www.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.

46 9/21/99www.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?


Download ppt "9/21/99www.cs.vt.edu/wwtut/1 Introduction To Abstract Windowing Toolkit (AWT) Part 3 – AWT Marc Abrams Virginia Tech CS Dept courses.cs.vt.edu/wwwtut/"

Similar presentations


Ads by Google