<Return>
or
<Enter>
key, the echoes are updated.
Play a bit with the above applet: change the text in the input field, scroll the text, iconify and de-iconify the Web page, reload the Web page, ..., and see what happens;
.java
file with the java code;.java
file until a correct .class
file has been created;.html
file that includes the applet;.html
file with a Java-enabled browser or
the appletviewer of the development environment.
EchoAppletWithGUI.java
with the Java code of the
above applet is as follows:
import java.applet.*; import java.awt.*; public class EchoAppletWithGUI extends Applet { String s; TextField fillin; public void init () { s = "enter some text"; fillin = new TextField(s); add(fillin); } public void paint(Graphics g) { g.drawString(s, 40, 50); g.drawString(s, 40, 70); } public boolean action(Event e, Object arg) { s = fillin.getText(); repaint(); return true; } }
java.applet
package and the java.awt
package.
The advantage of loading a complete package such as the AWT
(Abstract Window Toolkit) package instead of an individual class is that
in this way you have at once access to all classes related to building up and
working with a graphical user interface.
TextField
classTextField
.
A text field is a component in which you can enter a single editable
line of text. One way to create a text field is via
TextField(String text)
.
In this way you get a new text field with the specified text and
wide enough to hold the text.
init
methods
that holds the value of the text to be echoed;fillin
;add
the text field component of the user interface to
the applet.
action
method<Return>
or
<Enter>
,
the string s
gets its new value, the screen is repainted, and the
Java runtime system is told that event handling has been taken care of.