Java Reference
In-Depth Information
You can use the getText() , getSelectedText() , and setText( String ) methods with
text areas as you would text fields. Also, an append( String ) method adds the specified
text at the end of the current text, and an insert( String , int ) method inserts the spec-
ified text at the indicated position.
The setLineWrap( boolean ) method determines whether text will wrap to the next line
when it reaches the right edge of the component. Call setLineWrap(true) to cause line
wrapping to occur.
The setWrapStyleWord( boolean ) method determines what wraps to the next line—
either the current word ( true ) or the current character ( false ).
9
The next project you will create, the Authenticator application in Listing 9.5, uses sev-
eral Swing components to collect user input: a text field, a password field, and a text
area. Labels also are used to indicate the purpose of each text component.
LISTING 9.5
The Full Text of Authenticator.java
1: import javax.swing.*;
2:
3: public class Authenticator extends javax.swing.JFrame {
4: JTextField username = new JTextField(15);
5: JPasswordField password = new JPasswordField(15);
6: JTextArea comments = new JTextArea(4, 15);
7: JButton ok = new JButton(“OK”);
8: JButton cancel = new JButton(“Cancel”);
9:
10: public Authenticator() {
11: super(“Account Information”);
12: setSize(300, 220);
13: setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
14:
15: JPanel pane = new JPanel();
16: JLabel usernameLabel = new JLabel(“Username: “);
17: JLabel passwordLabel = new JLabel(“Password: “);
18: JLabel commentsLabel = new JLabel(“Comments: “);
19: comments.setLineWrap(true);
20: comments.setWrapStyleWord(true);
21: pane.add(usernameLabel);
22: pane.add(username);
23: pane.add(passwordLabel);
24: pane.add(password);
25: pane.add(commentsLabel);
26: pane.add(comments);
27: pane.add(ok);
28: pane.add(cancel);
29: add(pane);
30: setVisible(true);
Search WWH ::




Custom Search