Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.

Slides:



Advertisements
Similar presentations
1 Graphical User Interface (GUI) Applications Abstract Windowing Toolkit (AWT) Events Handling Applets.
Advertisements

Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
GUI. Swing Programs Four basic types of Top Level Window – JFrame, a top level window decorated like a native window – JWindow, an undecorated stand-alone.
Graphic User Interfaces Layout Managers Event Handling.
F27SB2 Programming Languages
June 1, 2000 Object Oriented Programming in Java (95-707) Advanced Topics 1 Lecture 9 Object Oriented Programming in Java Advanced Topics Abstract Windowing.
Gui Interfaces a la Swing Up to speed with Swing by Steven Gutz is a good source It don’t mean a thing if it ain’t got that swing Duke Ellington.
1 lecture 12Lecture 13 Event Handling (cont.) Overview  Handling Window Events.  Event Adapters Revisited.  Introduction to Components and Containers.
Chapter 13: Advanced GUIs and Graphics J ava P rogramming: From Problem Analysis to Program Design, From Problem Analysis to Program Design, Second Edition.
Java GUI building with the AWT. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many.
Scott Grissom, copyright 2006Ch 11: GUI Slide 1 Graphical User Interfaces (Ch 11) Careful design of a graphical user interface is key to a viable software.
10.1 AWT The AWT classes Users today expect a Graphical User Interface (GUI) Improves application usability Difficult to implement cross-platform.
PROGRAMMING REVIEW Lab 2 EECS 448 Dr Fengjun Li and Meenakshi Mishra.
CHAPTER-1 SWING JAVA-GUI. HERE ARE SOME TERMS THAT YOU’LL ENCOUNTER IN YOUR LESSON ON GRAPHICS: AWT Swing Applet/JApplet Graphics object init() GUI.
Java Programming Chapter 10 Graphical User Interfaces.
Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12.
Java Software Solutions Lewis and Loftus Chapter 10 1 Copyright 1997 by John Lewis and William Loftus. All rights reserved. Graphical User Interfaces --
Java Programming: From Problem Analysis to Program Design, 4e Chapter 12 Advanced GUIs and Graphics.
Chapter 13 Java AWT – Part II (Optional) Lecture Slides to Accompany An Introduction to Computer Science Using Java (2nd Edition) by S.N. Kamin, D. Mickunas,
1 CSC111H Graphical User Interfaces (GUIs) Introduction GUIs in Java Understanding Events A Simple Application The Containment Hierarchy Layout Managers.
Applets and Frames CS 21a: Introduction to Computing I First Semester,
Java GUI building with the AWT. AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Graphical User Interface CSI 1101 N. El Kadri. Plan - agenda Graphical components Model-View-Controller Observer/Observable.
Java GUI building with Swing. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in (almost) every Java textbook Adequate for.
Java Programming 1 Java Programming II Events, AWT, and Swing.
C13a, AWT Create, display, facilitate user interaction with window objects software framework: a way of structuring generic solutions to common problems.
1 Outline 1 Introduction 2 Overview of Swing Components 3 JLabel 4 Event Handling 5 TextFields 6 How Event Handling Works 7 JButton 8 JCheckBox and JRadioButton.
Graphic User Interface. Graphic User Interface (GUI) Most of us interact with computers using GUIs. GUIs are visual representations of the actions you.
1 Unit 5 GUI Aum Amriteshwaryai Namah. 2 Overview Shall learn how to reuse the graphics classes provided by Java for constructing Graphical User Interface.
Copyright © 2002, Systems and Computer Engineering, Carleton University c-Gui3.ppt * Object-Oriented Software Development Part 18-c Building.
Layout Managers Arranges and lays out the GUI components on a container.
Applets and Frames. Copyright 2005, by the authors of these slides, and Ateneo de Manila University. All rights reserved L14: GUI Slide 2 Applets Usually.
1 / 67 COP 3503 FALL 2012 SHAYAN JAVED LECTURE 14 Programming Fundamentals using Java 1.
Computer Programming with JAVA Chapter 7. Event-Driven Programming Using the AWT Event-Driven Programming GUIs and the AWT Simple Window Interfaces Components,
Chapter 7Java: an Introduction to Computer Science & Programming - Walter Savitch 1 Chapter 7 l Event-Driven Programming »GUIs and the AWT l Simple Window.
Object Oriented Programming.  Interface  Event Handling.
Ch13 Creating windows and applets. Short overview AWT (Abstract Windowing Toolkit) Early Java development used graphic classesEarly Java development used.
Kim B. Bruce, Andrea Danyluk & Tom Murtagh Williams College † Partially supported by NSF CCLI grant DUE Java: An Eventful Approach An innovative.
GUI DYNAMICS Lecture 11 CS2110 – Fall GUI Statics and GUI Dynamics  Statics: what’s drawn on the screen  Components buttons, labels, lists, sliders,
Csc Basic Graphical User Interface (GUI) Components.
CSI 3125, Preliminaries, page 1 AWT Control. CSI 3125, Preliminaries, page 2 AWT Control The AWT supports the following types of controls: ■ Labels ■
Graphical User Interfaces (GUI). PART ONE About GUI’s.
1 Event Driven Programs Rick Mercer. 2 So what happens next?  You can layout a real pretty GUI  You can click on buttons, enter text into a text field,
What Is an Event? Events – Objects that describe what happened Event sources – The generator of an event Event handlers – A method that receives an event.
Chapter 10 - Writing Graphical User Interfaces1 Chapter 10 Writing Graphical User Interfaces.
CSC 205 Programming II Lecture 7 AWT – Event Handling & Layout.
5-1 GUIs and Events Rick Mercer. 5-2 Event-Driven Programming with Graphical user Interfaces  Most applications have graphical user interfaces to respond.
AWT Vs SWING. 2 AWT (Abstract Window Toolkit) Present in all Java implementations Described in most Java textbooks Adequate for many applications Uses.
CSC 205 Programming II Lecture 5 AWT - I.
GUI building with the AWT
Welcome To java
The Abstract Window Toolkit
Abstract Window ToolKit (AWT)
Ellen Walker Hiram College
The Abstract Window Toolkit
Chap 7. Building Java Graphical User Interfaces
Graphical User Interfaces -- Introduction
JAVA AWT.
The Abstract Window Toolkit
The Abstract Window Toolkit
Introduction to Computing Using Java
The Abstract Window Toolkit
AWT Components and Containers
G. Pullaiah College of Engineering and Technology
The Abstract Window Toolkit
GUI building with the AWT
The Abstract Window Toolkit
GUI building with the AWT
Graphical User Interface
Presentation transcript:

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 1 Chapter 12 The Abstract Window Toolkit

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Overview AWT (Abstract Window Toolkit) –Classes and tools for building GUI Swing (Not using here) –Newer library for GUI –More powerful and sophisticated than the AWT Creating a Graphical User Interface –Components : an object that the user can see on the screen and interact with (Ex. button, scrollbar) –Container : a component that’s capable of holding other components (Ex. Windows, frame) –Events : an action triggered by the user (Ex. a key press, mouse click)

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Frames Frame is a window with a title and a border or menu. (DrawableFrame is a subclass of Frame) Frame Methods –Point getLocation() –Demension getSize() –void pack() –void setLocation(int x, int y) –void setSize(int width, int height) –void setVisible(boolean b)

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 4 Creating a Frame A simple program that creates a Frame object and displays it on the screen import java.awt.*; public class FrameTest { public static void main(String[] args) { Frame f = new Frame(“Frame Test”); f.setSize(150, 100); f.setVisible(true); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 5 Frame methods // Check the methods of frame in FrameTest2.javaFrameTest2.java

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 6 Adding Components to a sub class of a Frame ButtonTestFrame : A subclass of Frame import java.awt.*; import java.awt.event.*; public class ButtonTest { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button(“Testing”); add(b); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 7 Adding Components to a Frame ButtonTestAgain : Not use a subclass import java.awt.*; public class ButtonTestAgain { public static void main(String[] args) { Frame f = new Frame("Button Test"); f.setLayout(new FlowLayout()); Button b = new Button("Testing"); f.add(b); f.setSize(150, 100); f.setVisible(true); f.pack(); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Event Listeners Event –ActionEvent A Sigificant action has been performed on a component E.x Pressing button, pressing enter key in text field.. –AdjustmentEvent The state of an adjustable component(such as a scrollbar) has changed –ItemEvent An item has been selected (or deselected) within a checkbox, choice menu, or list –TextEvent The contents of a text area or text field have changed –WindowEvent: low-level event. It happens when the user attempts to close a window

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 9 Interface Interface looks like a class, but its methods are not fully defined. –Name, Parameter list,Result type –NO body, though Example : ActionListener public interface ActionListener extends EventListener{ public void actionPerformed(ActionEvent evt); } Interface is a pattern. A class implements an interface, that is, write the body of the method of interface.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 10 Event Listeners … Listener Interfaces (ButtonTest2.java)) –ActionListener : actionPerformed(ActionEvent evt) –AdjustmentListener :AdjestmentValueChanged(AdjustmentEvent evt) –ItemListent : itemStatChanged(ItemEvent evt) –TexListener : TextValueChanged(TextEvent evt) public interface ActionListener extends EventListener { public void actionPerformed(ActionEvent ext); } class class-name implements ActionListener { public void actionPerformed(ActionEvent ext) { …. // body } … //Variables, constructors, and methods, if desired }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 11 Event Listeners … Creating Event Listeners class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent ext) { …. } Botton objectBottonListener object * Registering single listener object to a single components Button b = new Button(“Change Color”); ButtonListener listener = new ButtonListener(); b.addActionListener(listener);

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 12 Event Listeners … ButtonTest2.java // Displays a frame containing a single "Close window" // button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest2 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 13 Continued ButtonTest2.java …. // Frame class class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window"); add(b); b.addActionListener(new ButtonListener()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 14 Event Listeners … Work with the. Implements WindowListener interface is a bad idea, as we do not need all of the seven methods. WindowAdapter (Adapter) Class –Implement WindowListener interface –Include empty methods so we can extend those methods * Installing WindowListener to frame … addWIndowListenr ( new WindowCloser () );.. // Listener for window class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 15 Add Adapter class ButtonTest3.java // Displays a frame containing a single "Close window" // button. The frame can be closed by pressing the button. import java.awt.*; import java.awt.event.*; // Driver class public class ButtonTest3 { public static void main(String[] args) { Frame f = new ButtonTestFrame("Button Test"); f.setSize(150, 100); f.setVisible(true); } class ButtonTestFrame extends Frame { public ButtonTestFrame(String title) { super(title); setLayout(new FlowLayout()); Button b = new Button("Close window");

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 16 Continued ButtonTest3.java add(b); b.addActionListener(new ButtonListener()); // Attach window listener addWindowListener(new WindowCloser()); } // Listener for button class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { System.exit(0); } // Listener for window class WindowCloser extends WindowAdapter { public void windowClosing(WindowEvent evt) { System.exit(0); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Inner Classes Inner Classes ( ChangeColor.java ) –A class that’s nested inside another class –The methods in an inner class have access to the variables and methods of the enclosing class, allowing the inner class to serve as a “helper” for the enclosing class –(Move the ButtonListener class inside of a frame class.) class ButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { if (getBackground() == Color.white) setBackground(Color.black); else setBackground(Color.white); } * Code to change background color of a frame.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Attaching Listeners to Multiple Components Single listener to multiple components The methods to determine which component is involved –getSource() : return an object reference (ChangeColor4.java) Ex. Object source = evt.getSource(); If (source == testButton) … –getActionCommand() : return the name (label) associated with an action event Ex. String label = evt.getActionCommand(); If (label.equals(“Testing”)) … (ChangeColor2.java) Botton object BottonListener object Botton object

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 19 Separate Listeners Multiple listeners to multiple components (ChangeColor3.java) –Implement separate listeners for each componenets Botton objectBottonListener2 object Botton objectBottonListener1 object // Listener for "Lighter" button class LighterButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { setBackground(getBackground().brighter()); } // Listener for "Darker" button class DarkerButtonListener implements ActionListener { public void actionPerformed(ActionEvent evt) { setBackground(getBackground().darker()); }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Layout Layout manager –determine the sizes and positions of components within the container Class NameBehavior BorderLayoutArranges components along the sides of the container and in the middle CardLayoutArrange components in “cards.” Only one card is visible at a time FlowLayoutArranges components in variable-length rows GridBagLayoutAligns components horizontally and vertically components can be of different sizes GridLayoutArranges components in fixed-length rows and columns

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 21 Layout The FlowLayout Class Ex1. setLayout(new FlowLayout() ); Ex2. setLayout(new FlowLayout(FlowLayout.LEFT)); Ex3. setLayout(new FlowLayout(FlowLayout.LEFT, 20, 10) );//horizontal and vertical gap The GridLayout Class Ex1. setLayout(new GridLayout(4, 5) ); Ex2. setLayout(new GridLayout(4, 5, 20, 10) );

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 22 Layout The BorderLayout Class Ex1. setLayout(new BorderLayout() ); Ex2. setLayout(new BorderLayout(20, 10) ); cf) add(“Center”, new Button(“Test”));

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 23 Preferred Sizes How layout managers work? According to “preferred size” of each component. –FlowLayout : Honors the preferred sizes of all components. Buttons size is not changing. –GridLayout : Ignores the preferred sizes of all components. –BorderLayout : Honors the preferred widths of the East and West components. Honors the preferred heights of the North and South components. Ignores the preferred size of the Center component.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 24 Panels A panel—an instance of the Panel class—is another kind of container. A panel is rectangular but has no border. When a panel is placed inside another container, it blends in seamlessly. Each panel has its own layout manager. A panel can be used to create a group of components that is treated as a single component.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 25 Panels A figure showing the panels as dashed rectangles:

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 26 Panels Statements to create the phone layout: (SimPhone.java) Panel buttonPanel = new Panel(); buttonPanel.setLayout(new GridLayout(4, 3, 10, 10)); for (int i = 1; i <= 9; i++) buttonPanel.add(new Button(i + "")); buttonPanel.add(new Button("*")); buttonPanel.add(new Button("0")); buttonPanel.add(new Button("#")); Panel centerPanel = new Panel(); centerPanel.add(buttonPanel); add("Center", centerPanel); Panel bottomPanel = new Panel(); bottomPanel.add(new Button("Dial")); add("South", bottomPanel);

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Creating and Using Components How to create the components? What kind of event(s) it fires? How to determine the current state of the component?

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 28 Checkboxes (CheckBoxDemo.java) Ex1. Checkbox cb = new Checkbox(“Enable sounds”); // No label Ex2. Checkbox cb = new Checkbox( ); // “on” Ex3. Checkbox cb = new Checkbox(“Enable sounds”, true); To detect this event, we need to implements the ItemListener interface, which requires “itemStateChanged()” method class CheckboxListener implements ItemListener { public void itemStateChanged(ItemEvent evt) { … } boolean getState(), void setState(boolean st)

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 29 Checkbox Groups (CheckBoxGroupDemo.java) Ex1. CheckboxGroup musicGroup = new CheckboxGroup(); Checkbox rockBox = new Checkbox(“Rock”, musicGroup, true); Checkbox jazzBox = new Checkbox(“Jazz”, musicGroup, false); Checkbox classicalBox = new Checkbox(“Classical”, musicGroup, false);

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 30 Choice Menus Ex1. Choice countryChoice = new Choice(); countryChoice.add(“U.S.A”); countryChoice.add(“Canada”); countryChoice.add(“Mexico”); To detect this event, we need to implements the ItemListener interface, which requires “itemStateChanged()” method class ChoiceMenuListener implements ItemListener { public void itemStateChanged(ItemEvent evt) { … } String getSelectedItem(), int getSelectedIndex()

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 31 Labels Ex1. Label lastName = new Label(“Enter last name : ”); String getText(), void setText() No border. Often placed next to other components to indicate their meaning or function. The user can’t change a label’s text; there are no events defined for labels. Ex) PickColor PickColor

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 32 Lists A list is a rectangle containing a series of items: If not all list items are visible, a scrollbar appears to the right of the list:

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 33 Lists Ex1. List countryList = new List(); Ex2. List countryList = new List(5); countryList.add(“U.S.A”); countryList.add(“Canada”); countryList.add(“Mexico”); String getSelectedItem(), int getSelectedIndex() Two events: –Single-clicking on a list item causes an item event. –Double-clicking causes an action event. Ex: ShowDefinitionShowDefinition

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 34 Scrollbars A scrollbar is a sliding bar. Scrollbars can be either horizontal or vertical: Create a scrollbar. (type, initial-value, width, min, max) Scrollbar sb = new Scrollbar(Scrollbar.HORIZONTAL,50, 1, 0, 100);

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 35 Scrollbars To detect this event: class ScrollbarListener implements AdjustmentListener { public void adjustmentValueChanged(AdjustmentEvent evt) { … } sb.addAdjustmentListener(new ScrollbarListener()); Get: int value = sb.getValue(); Set: sb.setValue(newValue); Ex:PickColorPickColor

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 36 Text Areas A text area is capable of displaying multiple lines of text: Scrollbars at the bottom and right side make it possible for the user to view text that’s not otherwise visible.

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 37 Text Areas How to create a TextArea object: –TextArea ta = new TextArea(); –String quote = “ ….”; TextArea ta = new TextArea(quote); –TextArea ta = new TextArea(10, 20); // rows, columns –TextArea ta = new TextArea(quote, 10, 20); Set editable or not editable, default is true: –ta.setEditable(false);//not editable Detect an event class TextAreaListener implements TextListener { public void textValueChanged(TextEvent evt) { … } ta.addTextListener(new TextAreaListener());

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 38 Text Areas Methods: String text = ta.getText(); ta.setText("Line 1\nLine 2\nLine 3"); ta.append("\nLine 4"); Ex) TextAreaDemo ShowDefinition

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved. 39 Text Fields A text field contains a single line of text: (ex: ConvertTemp) ConvertTemp How to create: TextField tf = new TextField(); TextField tf = new TextField("Your name here"); TextField tf = new TextField(40); TextField tf = new TextField("Your name here", 40); To detect this event, we need to implements the ActionListener. class textFieldListner implements ActionListener { public void actionPerformed(ActionEvent evt) { … }

Chapter 12: The Abstract Window Toolkit Java Programming FROM THE BEGINNING Copyright © 2000 W. W. Norton & Company. All rights reserved Examples The ConvertTemp, ShowDefinition, and PickColor programs illustrate the use of various GUI components. ConvertTemp ShowDefinition PickColor