/*<applet code='ButtonView' width='150' height='100'> </applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class ButtonView extends Applet implements ActionListener{ Button button; public void init() { button = new Button("Button"); button.addActionListener(this); add(button); } public void actionPerformed(ActionEvent event) { button.setLabel("Pressed"); } }ボタンを押すとラベル表示を行う例を示す.
/*<applet code='LabelView' width='150' height='100'> </applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class LabelView extends Applet implements ActionListener{ Button button; Label label; public void init() { button = new Button("変更"); button.addActionListener(this); add(button); label = new Label("変更前です"); add(label); } public void actionPerformed(ActionEvent event) { label.setText("変更しました"); } }一行の文字列を入力するテキストフィールド(TextField)と, 複数行のテキストを入力できるテキストエリア(TextArea)の例を示す.
/*<applet code='TextAreaView' width='300' height='200'> </applet>*/ import java.applet.*; import java.awt.*; import java.awt.event.*; public class TextAreaView extends Applet implements ActionListener{ TextField field; TextArea area; public void init() { field = new TextField(30); area = new TextArea(6, 30); field.addActionListener(this); add(field); add(area); } public void actionPerformed(ActionEvent event) { area.append(field.getText() + "\n"); field.setText(""); } }