- 论坛徽章:
- 0
|
继续介绍Applet控件,包括TextField,TextArea
Text fields [color="#000000"]TextField[color="#000000"]是一个line area,供用户单行输入并编写文本. [color="#000000"]TextField是[color="#000000"]TextComponent的一个子类[color="#000000"],[color="#000000"] [color="#000000"]允许用户选中一些文本,选中的文本被称为[color="#000000"]String[color="#000000"], [color="#000000"]TextField[color="#000000"] 的一些方法参看Java API. 下面的例子演示了[color="#000000"]TextField一些方法的使用[color="#000000"]; :
[color="#990000"]//: TextField1.java
[color="#009900"]// Using the text field control
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class TextField1 [color="#0000ff"]extends Applet {
Button
b1 = [color="#0000ff"]new Button("Get Text"),
b2 = [color="#0000ff"]new Button("Set Text");
TextField
t = [color="#0000ff"]new TextField("Starting text", 30);
String s = [color="#0000ff"]new String();
[color="#0000ff"]public [color="#0000ff"]void init() {
add(b1);
add(b2);
add(t);
}
[color="#0000ff"]public [color="#0000ff"]boolean action (Event evt, Object arg) {
[color="#0000ff"]if(evt.target.equals(b1)) {
getAppletContext().showStatus(t.getText());
s = t.getSelectedText();
[color="#0000ff"]if(s.length() == 0) s = t.getText();
t.setEditable([color="#0000ff"]true);
}
[color="#0000ff"]else [color="#0000ff"]if(evt.target.equals(b2)) {
t.setText("Inserted by Button 2: " + s);
t.setEditable([color="#0000ff"]false);
}
[color="#009900"]// Let the base class handle it:
[color="#0000ff"]else
[color="#0000ff"]return [color="#0000ff"]super.action(evt, arg);
[color="#0000ff"]return [color="#0000ff"]true; [color="#009900"]// We've handled it here
}
} [color="#009900"]///:~
[color="#990000"]有很多方法来创建TextField[color="#000000"];这里我们提供了一个初始字符串,设定了初始长度.
[color="#000000"]Pressing button 1 either gets the text you’ve selected with the mouse or it gets all the text in the field and places the result in [color="#000000"]String s [color="#000000"]. It also allows the field to be edited. Pressing button 2 puts a message and [color="#000000"]s[color="#000000"] into the text field and prevents the field from being edited (although you can still select the text). The editability of the text is controlled by passing [color="#000000"]setEditable( )[color="#000000"] a [color="#000000"]true[color="#000000"] or [color="#000000"]false[color="#000000"].
[color="#000000"]Text areas TextArea[color="#000000"]与[color="#000000"]TextField[color="#000000"]非常相近,不过它支持多行输入,编辑,并且有更多功能.在[color="#000000"]TextField中[color="#000000"],我们可以在一个给定的位置附件,插入,替换文本. It seems like this functionality could be useful for [color="#000000"]TextField[color="#000000"] as well, so it’s a little confusing to try to detect how the distinction is made. You might think that if you want [color="#000000"]TextArea[color="#000000"] functionality everywhere you can simply use a one line [color="#000000"]TextArea[color="#000000"] in places where you would otherwise use a [color="#000000"]TextField[color="#000000"]. In Java 1.0, you also got scroll bars with a [color="#000000"]TextArea [color="#000000"]even when they weren’t appropriate; that is, you got both vertical and horizontal scroll bars for a one line [color="#000000"]TextArea[color="#000000"]. In Java 1.1 this was remedied with an extra constructor that allows you to select which scroll bars (if any) are present. The following example shows only the Java 1.0 behavior, in which the 滚动条 are always on. Later in the chapter you’ll see an example that demonstrates Java 1.1 [color="#000000"]TextArea[color="#000000"]s.
[color="#990000"]//: TextArea1.java
[color="#009900"]// Using the text area control
[color="#0000ff"]import java.awt.*;
[color="#0000ff"]import java.applet.*;
[color="#0000ff"]public [color="#0000ff"]class TextArea1 [color="#0000ff"]extends Applet {
Button b1 = [color="#0000ff"]new Button("Text Area 1");
Button b2 = [color="#0000ff"]new Button("Text Area 2");
Button b3 = [color="#0000ff"]new Button("Replace Text");
Button b4 = [color="#0000ff"]new Button("Insert Text");
TextArea t1 = [color="#0000ff"]new TextArea("t1", 1, 30);
TextArea t2 = [color="#0000ff"]new TextArea("t2", 4, 30);
[color="#0000ff"]public [color="#0000ff"]void init() {
add(b1);
add(t1);
add(b2);
add(t2);
add(b3);
add(b4);
}
[color="#0000ff"]public [color="#0000ff"]boolean action (Event evt, Object arg) {
[color="#0000ff"]if(evt.target.equals(b1))
getAppletContext().showStatus(t1.getText());
[color="#0000ff"]else [color="#0000ff"]if(evt.target.equals(b2)) {
t2.setText("Inserted by Button 2");
t2.appendText(": " + t1.getText());
getAppletContext().showStatus(t2.getText());
}
[color="#0000ff"]else [color="#0000ff"]if(evt.target.equals(b3)) {
String s = " Replacement ";
t2.replaceText(s, 3, 3 + s.length());
}
[color="#0000ff"]else [color="#0000ff"]if(evt.target.equals(b4))
t2.insertText(" Inserted ", 10);
[color="#009900"]// Let the base class handle it:
[color="#0000ff"]else
[color="#0000ff"]return [color="#0000ff"]super.action(evt, arg);
[color="#0000ff"]return [color="#0000ff"]true; [color="#009900"]// We've handled it here
}
} [color="#009900"]///:~ [color="#000000"]TextArea[color="#000000"] 同样有不止一种构造方法,这里我们设置了初始字符串,以及其大小.这里岩石的方法有:getting, appending, replacing, and inserting text.
[color="#000000"]
本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8682/showart_39200.html |
|