Tom Kelliher, CS 116
Sept. 22, 2000
Lab 2 write-ups due now. Postlab due Monday in hardcopy form. Follow our style guidelines.
Read 3.3.
Lab 2 wrap-up, method design exercise.
Component
abstract class.
Applet
.
Active widgets.
Abstract class:
A repository of methods and data (instance variables) that cannot be instantiated. It is a ``factoring out'' of methods and data from classes that have those methods and data in common.Example: a class
Car
.
A portion of the Component
hierarchy:
Some things you can do to a Component object:
void setBackground(Color c)
Wait we've seen this before. Where? What does this imply?
Example:
Label myLabel = new Label("Hi, I'm a label."); myLabel.setBackground(new Color(255, 0, 0));When we used
setBackground()
before, we didn't specify an object.
The method was applied to what object, then?
void paint(Graphics g)
Empty. Each Component
has one. We override the applet's
paint()
method. Widgets know how to ``paint'' themselves.
void repaint()
Request that a Component
repaint itself. Will get done
``eventually.''
Dimension getSize()
Get the size, in pixels, of a Component
.
Examples:
Dimension appletSize; appletSize = getsize(); // We can now query the applet's size by accessing appletSize.width and // appletSize.height. Label myLabel = new Label("Hi, I'm a label."); Dimension labelSize; labelSize = myLabel.getSize();Again, not when you do and do not specify the object to which the method is applied.
Label
constructors. Each has a unique signature.
void setText(String label)
String getText()
A factorization of TextField
and TextArea
, but not an
abstract class.
int getCaretPosition()
Is this something Bugs Bunny would be interested in? What's a caret?
0-based counting.
String getSelectedText()
What's a selection?
A one line text input object.
A multi-line text input object.
Note: There are typos on pg. 82. The method declarations are correct; some of the example code uses the wrong method names. Check the Class References section of the chapter to double-check.
Demonstrates the use and actions of TextField
s.
Run
the applet.
//====================================================================== // Project: CS 116, TextField example // Author: Tom Kelliher // File: TFExample.java // Purpose: The Applet demonstrates the use of TextFields and // Labels. A simple, unchanging Label is created. // Two Textfields are created. The first displays the // size of the Applet. The second displays the // previously selected text from the first. //====================================================================== import java.applet.*; import java.awt.*; //====================================================================== // Class: TFExample // Purpose: See the project purpose. //====================================================================== public class TFExample extends Applet { // Instance variables. Dimension appletSize; Label label = new Label("Size of the applet:"); TextField display = new TextField(12); // Display the size // of the applet. TextField selectedText = new TextField(12); // Display selected // text from display // object. //=================================================================== // Method: init // Purpose: Set Applet's background color. Add and initialize // widgets. //=================================================================== public void init() { setBackground(Color.red); add(label); add(display); add(selectedText); label.setBackground(Color.green); label.setForeground(Color.red); display.setBackground(Color.blue); display.setForeground(Color.yellow); // Get Applet size and set text for display object. appletSize = getSize(); display.setText(appletSize.width + " by " + appletSize.height); selectedText.setBackground(Color.yellow); selectedText.setForeground(Color.blue); } //=================================================================== // Method: paint // Purpose: Update text in display and selectedText objects. // Inputs: g: A graphics object. //=================================================================== public void paint(Graphics g) { String selection; // Temporarily hold the selected text from // the display object. The selected text is // copied to the selectedText object. selection = display.getSelectedText(); // Update Applet size within display object. appletSize = getSize(); display.setText(appletSize.width + " by " + appletSize.height); selectedText.setText(selection); } }