Подробное руководство по программированию на Java

         

public class FormLayout extends Applet



Листинг 1

. Файл FormLayout.java import java.applet.Applet; import java.awt.*; import java.util.*; public class FormLayout 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() { setLayout(new GridLayout(4, 3));
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; } } Исходный текст документа HTML, созданный для нашего аплета системой Java WorkShop, представлен в листинге 2.



Листинг 2

. Файл FormLayout.tmp.html <applet name="FormLayout" code="FormLayout" codebase= "file:/e:/sun/articles/vol7/src/FormLayout" width="500" height="600" align="Top" alt="If you had a java- enabled browser, you would see an applet here.">
</applet>

Содержание раздела