Java The Complete Reference, Seventh Edition

(Greg DeLong) #1
add(name);
add(passp);
add(pass);

// register to receive action events
name.addActionListener(this);
pass.addActionListener(this);
}

// User pressed Enter.
public void actionPerformed(ActionEvent ae) {
repaint();
}

public void paint(Graphics g) {
g.drawString("Name: " + name.getText(), 6, 60);
g.drawString("Selected text in name: "
+ name.getSelectedText(), 6, 80);
g.drawString("Password: " + pass.getText(), 6, 100);
}
}

Sample output from theTextFieldDemoapplet is shown in Figure 24-7.

Using a TextArea


Sometimes a single line of text input is not enough for a given task. To handle these situations,
the AWT includes a simple multiline editor calledTextArea. Following are the constructors
forTextArea:

TextArea( ) throws HeadlessException
TextArea(intnumLines, intnumChars) throws HeadlessException
TextArea(Stringstr) throws HeadlessException
TextArea(Stringstr, intnumLines, intnumChars) throws HeadlessException
TextArea(Stringstr, intnumLines, intnumChars, intsBars) throws HeadlessException

Chapter 24: Using AWT Controls, Layout Managers, and Menus 721


FIGURE 24-7 Sample output from theTextFieldDemoapplet
Free download pdf