Historia wymaga pasterzy, nie rzeźników.

appendText(": " + t1.getText());
getAppletContext().showStatus(t2.getText());
}
else if(evt.target.equals(b3)) {
String s = " Replacement ";
t2.replaceText(s, 3, 3 + s.length());
}
else if(evt.target.equals(b4))
t2.insertText(" Inserted ", 10);
// Let the base class handle it:
else
return super.action(evt, arg);
return true; // We've handled it here
}
} ///:~
There are several different TextArea constructors, but the one shown here gives a starting string and the number of rows and columns. The different buttons show getting, appending, replacing, and inserting text.
Labels
A Label does exactly what it sounds like it should: places a label on the form. This is particularly important for text fields and text areas that don’t have labels of their own, and can also be useful if you simply want to place textual information on a form. You can, as shown in the first example in this chapter, use drawString( ) inside paint( ) to place text in an exact location. When you use a Label it allows you to (approximately) associate the text 482
Thinking in Java
www.BruceEckel.com
with some other component via the layout manager (which will be discussed later in this chapter).
With the constructor you can create a blank label, a label with initial text in it (which is what you’ll typically do), and a label with an alignment of CENTER, LEFT, or RIGHT (static final ints defined in class Label). You can also change the label and its alignment with setText( ) and setAlignment( ), and if you’ve forgotten what you’ve set these to you can read the values with getText( ) and getAlignment( ). This example shows what you can do with labels:
//: Label1.java
// Using labels
import java.awt.*;
import java.applet.*;
public class Label1 extends Applet {
TextField t1 = new TextField("t1", 10);
Label labl1 = new Label("TextField t1");
Label labl2 = new Label(" ");
Label labl3 = new Label(" ",
Label.RIGHT);
Button b1 = new Button("Test 1");
Button b2 = new Button("Test 2");
public void init() {
add(labl1); add(t1);
add(b1); add(labl2);
add(b2); add(labl3);
}
public boolean action (Event evt, Object arg) {
if(evt.target.equals(b1))
labl2.setText("Text set into Label");
else if(evt.target.equals(b2)) {
if(labl3.getText().trim().length() == 0)
labl3.setText("labl3");
if(labl3.getAlignment() == Label.LEFT)
labl3.setAlignment(Label.CENTER);
else if(labl3.getAlignment()==Label.CENTER)
labl3.setAlignment(Label.RIGHT);
else if(labl3.getAlignment() == Label.RIGHT)
labl3.setAlignment(Label.LEFT);
}
else
return super.action(evt, arg);
return true;
}
} ///:~
The first use of the label is the most typical: labeling a TextField or TextArea. In the second part of the example, a bunch of empty spaces are reserved and when you press the “Test 1”
button setText( ) is used to insert text into the field. Because a number of blank spaces do not equal the same number of characters (in a proportionally-spaced font) you’ll see that the text gets truncated when inserted into the label.
Chapter 13: Creating Windows & Applets
483
The third part of the example reserves empty space, then the first time you press the “Test 2”
button it sees that there are no characters in the label (since trim( ) removes all of the blank spaces at each end of a String) and inserts a short label, which is initially left-aligned. The rest of the times you press the button it changes the alignment so you can see the effect.
You might think that you could create an empty label and then later put text in it with setText( ). However, you cannot put text into an empty label – presumably because it has zero width – so creating a label with no text seems to be a useless thing to do. In the example above, the “blank” label is filled with empty spaces so it has enough width to hold text that’s placed inside later.
Similarly, setAlignment( ) has no effect on a label that you’d typically create with text in the constructor. The label width is the width of the text, so changing the alignment doesn’t do anything. However, if you start with a long label and then change it to a shorter one you can see the effect of the alignment.
These behaviors occur because of the default layout manager that’s used for applets, which causes things to be squished together to their smallest size. Layout managers will be covered later in this chapter, when you’ll see that other layouts don’t have the same effect.
Check boxes
A check box provides a way to make a single on-off choice; it consists of a tiny box and a label. The box typically holds a little ‘x’ (or some other indication that it is set) or is empty depending on whether that item was selected.