Password GUI) In this exercise, you apply the GUI design guidelines you have learned to a graphical user interface for a password-protected message application (Fig. 3.22). You will set the bounds of the Enter your secret message: JTextArea such that it is left aligned with the Enter your secret message: JLabel and right aligned with the Log In JButton.
a) Copying the template to your working directory. Copy the C:ExamplesTutorial03ExercisesPassword directory to your C:SimplyJava directory.
b) Opening the Command Prompt window and changing directories. Open the Command Prompt by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaPassword, then pressing Enter.
c) Compiling the template application. Compile your application by typing javac Password.java, then pressing Enter.
d) Running the template application. Run the application by typing java Password.
The GUI of the Password template application should appear as shown in Fig. 3.23. Notice the differences from Fig. 3.22.
e) Closing the application. Close your running application by clicking its close button. f) Opening the template file. Open the Password.java file in your text editor.
g) Customizing the Enter your secret message: JTextArea. Using Fig. 3.22 and the template code as a guide, you wi
```
1 // Password.java
2 // GUI for a password-protected message application.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class Password extends JFrame
8 {
9 // JLabel and JTextField for user name
10 private JLabel userNameJLabel;
11 private JTextField userNameJTextField;
12
13 // JLabel and JTextField for password
14 private JLabel passwordJLabel;
15 private JPasswordField passwordJPasswordField;
16
17 // JLabel and JTextField for reentering password
18 private JLabel reenterPasswordJLabel;
19 private JPasswordField reenterPasswordJPasswordField;
20
21 // JButton for logging in to the Message Application
22 private JButton logInJButton;
23
24 // JLabel and JTextArea for messages
25 private JLabel messageJLabel;
26 private JTextArea messageJTextArea;
27
28 // JButtons to save a message, log out and clear all input
29 private JButton saveJButton;
30 private JButton logOutJButton;
31 private JButton clearJButton;
32
33 // no-argument constructor
34 public Password()
35 {
36 createUserInterface();
37 }
38
39 // create and position GUI components
40 public void createUserInterface()
41 {
42 // get content pane and set its layout
43 Container container = getContentPane();
44 container.setLayout( null );
45
46 // set up userNameJLabel
47 userNameJLabel = new JLabel();
48 userNameJLabel.setBounds( 16, 16, 96, 21 );
49 userNameJLabel.setText( "Enter your name:" );
50 container.add( userNameJLabel );
51
52 // set up userNameJTextField
53 userNameJTextField = new JTextField();
54 userNameJTextField.setBounds( 172, 16, 140, 21 );
55 userNameJTextField.setText( "Joe Purple" );
56 container.add( userNameJTextField );
57
58 // set up passwordJLabel
59 passwordJLabel = new JLabel();
60 passwordJLabel.setBounds( 16, 48, 122, 21 );
61 passwordJLabel.setText( "Enter your password:" );
62 container.add( passwordJLabel );
63
64 // set up passwordJPasswordField
65 passwordJPasswordField = new JPasswordField();
66 passwordJPasswordField.setBounds( 172, 48, 140, 21 );
67 passwordJPasswordField.setText( "Hello2world" );
68 container.add( passwordJPasswordField );
69
70 // set up reenterPasswordJLabel
71 reenterPasswordJLabel = new JLabel();
72 reenterPasswordJLabel.setBounds( 16, 80, 148, 21 );
73 reenterPasswordJLabel.setText( "Re-enter your password:" );
74 container.add( reenterPasswordJLabel );
75
76 // set up reenterPasswordJPasswordField
77 reenterPasswordJPasswordField = new JPasswordField();
78 reenterPasswordJPasswordField.setBounds( 172, 80, 140, 21 );
79 reenterPasswordJPasswordField.setText( "Hello2world" );
80 container.add( reenterPasswordJPasswordField );
81
82 // set up logInJButton
83 logInJButton = new JButton();
84 logInJButton.setBounds( 240, 120, 72, 24 );
85 logInJButton.setText( "Log In" );
86 container.add( logInJButton );
87
88 // set up messageJLabel
89 messageJLabel = new JLabel();
90 messageJLabel.setBounds( 16, 136, 164, 21 );
91 messageJLabel.setText( "Enter your secret message:" );
92 container.add( messageJLabel );
93
94 // set up messageJTextArea
95 messageJTextArea = new JTextArea();
96 messageJTextArea.setBounds( 16, 160, 296, 72 );
97 messageJTextArea.setText(
98 "Welcome to Simply Java Programming" );
99 container.add( messageJTextArea );
100
101 // set up saveJButton
102 saveJButton = new JButton();
103 saveJButton.setBounds( 58, 248, 72, 24 );
104 saveJButton.setText( "Save" );
105 container.add( saveJButton );
106
107 // set up logOutJButton
108 logOutJButton = new JButton();
109 logOutJButton.setBounds( 146, 248, 78, 24 );
110 logOutJButton.setText( "Log Out" );
111 container.add( logOutJButton );
112
113 // set up clearJButton
114 clearJButton = new JButton();
115 clearJButton.setBounds( 240, 248, 72, 24 );
116 clearJButton.setText( "Clear" );
117 container.add( clearJButton );
118
119 // set properties of application’s window
120 setTitle( "Message Application" ); // set title bar text
121 setSize( 336, 312 ); // set window size
122 setVisible( true ); // display window
123
124 } // end method createUserInterface
125
126 // main method
127 public static void main( String[] args )
128 {
129 Password application = new Password();
130 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
131
132 } // end method main
133
134 } // end class Password
```
You might also like to view...
A _____________ is an object that is a simple container class that groups other objects.
(a) JPanel (b) JMenu (c) JButton (d) JTextArea
Each SharePoint Online list app is arranged in rows
Indicate whether the statement is true or false
The comparison operator, < >, is used to insert a condition that means ____.
A. greater than B. And C. less than D. not equal to
In the accompanying figure, an example of an element from the students vocabulary is _____.
A. firstName B. student C. instructor D. name