FormDemo , , , .
(. 3) , . , , .
. 3. FormDemo
First Second Enter your first name Enter your second name. Ready, , Mode 1, Mode 2 Mode 3 . .
, Mode 3, . . , . Layout Manager, .
. FormDemo.java
import java.applet.Applet;
import java.awt.*;
import java.util.*;
public class FormDemo extends Applet
{
Button btReady; // btReady Ready
// chbox1 chbox2
// ,
//
Checkbox chbox1;
Checkbox chbox2;
// grRadio ,
// Mode 1, Mode 2 Mode 3:
CheckboxGroup grRadio;
//
Checkbox rd1;
Checkbox rd2;
Checkbox rd3;
// ch1 ,
Choice ch1;
// ,
// Label.
// lbFirstName lbSecondName
Label lbFirstName;
Label lbSecondName;
TextField txtFirstName; //
TextField txtSecondName; // txtFirstName txtSecondName
TextArea txta;
// init .
// ,
// .
// init ,
// Label
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. ,
// ,
// .
add(txtSecondName);
//
// CheckboxGroup
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
add(rd2);
add(rd3);
ch1 = new Choice(); // Choice
ch1.addItem("White");
ch1.addItem("Green");
ch1.addItem("Yellow");
add(ch1); //
setBackground(Color.yellow);
// ,
// . ,
// setBackground
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);
// TextArea.
// 6 45
txta = new TextArea("", 6, 45);
add(txta);
txta.setBackground(Color.white);
// init ,
// - Ready
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);
}
// action ,
// . evt.target:
public boolean action(Event evt, Object obj)
{
Button btn;
String str1, str2;
if (evt.target instanceof Button)
{
// action ,
// Button Choice. ,
// - , false.
// , .
// action true.
// , action , .
// , evt.target
// btReady:
if (evt.target.equals(btReady))
{
// action false,
// . , ?
// ,
// ( , ):
btn = (Button)evt.target;
|
|
//
// , getText.
// str1 str2
str1 = txtFirstName.getText();
str2 = txtSecondName.getText();
// action
// chbox1 chbox2. ,
// txta:
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)
{
// ch1, action ,
//
// .
// getSelectedIndex
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;
}