Исходный текст аплета FormDemo
Исходный текст аплета FormDemo вы найдете в листинге 1.
Листинг 1. Файл FormDemo.java
import java.applet.Applet; import java.awt.*; import java.util.*;
public class FormDemo extends Applet { Button btReady;
Checkbox chbox1; Checkbox chbox2;
CheckboxGroup grRadio; Checkbox rd1; Checkbox rd2; Checkbox rd3;
Choice ch1;
Label lbFirstName; Label lbSecondName;
TextField txtFirstName; TextField txtSecondName; TextArea txta;
public void init() { chbox1 = new Checkbox("First"); add(chbox1);
lbFirstName = new Label("Enter your first name:"); add(lbFirstName);
txtFirstName = new TextField(" ", 30); add(txtFirstName);
chbox2 = new Checkbox("Second"); add(chbox2);
lbSecondName = new Label("Enter your second name:"); add(lbSecondName);
txtSecondName = new TextField(" ", 30); add(txtSecondName);
grRadio = new CheckboxGroup(); rd1 = new Checkbox("Mode 1", grRadio, true); rd2 = new Checkbox("Mode 2", grRadio, false); rd3 = new Checkbox("Mode 3", grRadio, false);
add(rd1); add(rd2); add(rd3);
ch1 = new Choice(); ch1.addItem("White"); ch1.addItem("Green"); ch1.addItem("Yellow");
add(ch1);
setBackground(Color.yellow);
lbFirstName.setBackground(Color.yellow); lbSecondName.setBackground(Color.yellow);
rd1.setBackground(Color.yellow); rd2.setBackground(Color.yellow); rd3.setBackground(Color.yellow);
chbox1.setBackground(Color.yellow); chbox2.setBackground(Color.yellow);
txta = new TextArea("", 6, 45); add(txta); txta.setBackground(Color.white);
btReady = new Button("Ready"); add(btReady); }
public String getAppletInfo() { return "Name: FormDemo"; }
public void paint(Graphics g) { Dimension dimAppWndDimension = getSize();
g.setColor(Color.black); g.drawRect(0, 0, dimAppWndDimension.width - 1, dimAppWndDimension.height - 1); }
public boolean action(Event evt, Object obj) { Button btn; String str1, str2;
if(evt.target instanceof Button) { if(evt.target.equals(btReady)) { btn = (Button)evt.target;
str1 = txtFirstName.getText(); str2 = txtSecondName.getText();
if(chbox1.getState()) txta.append(str1);
if(chbox2.getState()) txta.append(str2);
if(rd1.getState()) txta.append("\nMode 1\n");
if(rd2.getState()) txta.append("\nMode 2\n");
if(rd3.getState()) txta.append("\nMode 3\n"); } else { return false; } return true; } else if(evt.target instanceof Choice) { if(evt.target.equals(ch1)) { if(ch1.getSelectedIndex() == 0) txta.setBackground(Color.white);
if(ch1.getSelectedIndex() == 1) txta.setBackground(Color.green);
if(ch1.getSelectedIndex() == 2) txta.setBackground(Color.yellow); } } return false; } }
В листинге 2 мы привели исходный текст документа HTML, созданный для нашего аплета системой Java WorkShop.
Листинг 2. Файл FormDemo.tmp.html
<applet name="FormDemo" code="FormDemo" codebase= "file:/e:/sun/articles/vol6/src/FormDemo" width="500" height="600" align="Top" alt="If you had a java-enabled browser, you would see an applet here."> <hr>If your browser recognized the applet tag, you would see an applet here.<hr> </applet>