The View Name application allows the user to enter the user’s first and last name. When the user clicks the View Name JButton, a JOptionPane that displays the user’s first and last name appears. Your application creates an instance of Class Name. This class uses set methods to set the first-name and last-name instance variables. While testing your application, you noticed that the JOptionPane did not display the correct out- put. The last name is displayed, but the first name is not. The GUI is shown in Fig. 18.42.



a) Copying the template to your working directory. Copy the C:Examples Tutorial18ExercisesDebuggerViewName directory to your C:SimplyJava directory.

b) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt window by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaViewName.

c) Using the debugger. Use the debugger to find the logic error(s) in your application.

Use the watch command to see all the changes to the instance variables of class Name. When you have found the logic error, change the code appropriately. The application with the correct output is displayed in Fig. 18.42.

d) Saving the application. Save your modified source code files.

e) Compiling the application. Compile the application by typing javac

ViewName.java Name.java.

f) Running the application. When your application compiles correctly, run it by typing java ViewName.


```
1 // ViewName.java
2 // User inputs are displayed in a JOptionPane.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class ViewName extends JFrame
8 {
9 // JLabel and JTextField for first name
10 private JLabel firstJLabel;
11 private JTextField firstJTextField;
12
13 // JLabel and JTextField for last name
14 private JLabel lastJLabel;
15 private JTextField lastJTextField;
16
17 // JButton to view name
18 private JButton viewJButton;
19
20 // no-argument constructor
21 public ViewName()
22 {
23 createUserInterface();
24 }
25
26 // create and position GUI components; register event handler
27 private void createUserInterface()
28 {
29 // get content pane for attaching GUI components
30 Container contentPane = getContentPane();
31
32 // enable explicit positioning of GUI components
33 contentPane.setLayout( null );
34
35 // set up firstJLabel
36 firstJLabel = new JLabel();
37 firstJLabel.setBounds( 16, 16, 80, 21 );
38 firstJLabel.setText( "First name:" );
39 contentPane.add( firstJLabel );
40
41 // set up firstJTextField
42 firstJTextField = new JTextField();
43 firstJTextField.setBounds( 112, 16, 140, 21 );
44 firstJTextField.setHorizontalAlignment( JTextField.LEFT );
45 contentPane.add( firstJTextField );
46
47 // set up lastJLabel
48 lastJLabel = new JLabel();
49 lastJLabel.setBounds( 16, 56, 80, 21 );
50 lastJLabel.setText( "Last name:" );
51 contentPane.add( lastJLabel );
52
53 // set up lastJTextField
54 lastJTextField = new JTextField();
55 lastJTextField.setBounds( 112, 56, 140, 21 );
56 lastJTextField.setHorizontalAlignment( JTextField.LEFT );
57 contentPane.add( lastJTextField );
58
59 // set up viewJButton
60 viewJButton = new JButton();
61 viewJButton.setBounds( 142, 96, 100, 24 );
62 viewJButton.setText( "View Name" );
63 contentPane.add( viewJButton );
64 viewJButton.addActionListener(
65
66 new ActionListener() // anonymous inner class
67 {
68 // event handler called when viewJButton is pressed
69 public void actionPerformed ( ActionEvent event )
70 {
71 viewJButtonActionPerformed( event );
72 }
73
74 } // end anonymous inner class
75
76 ); // end call to addActionListener
77
78 // set properties of application’s window
79 setTitle( "View Name" ); // set title bar string
80 setSize( 280, 160 ); // set window size
81 setVisible( true ); // display window
82
83 } // end method createUserInterface
84
85 // create new Name object and output to a JOptionPane
86 private void viewJButtonActionPerformed( ActionEvent event )
87 {
88 // create new Name
89 Name name = new Name( firstJTextField.getText(),
90 lastJTextField.getText() );
91
92 // assign user's name to output
93 String output = ( "Your name is: " + name.getFirst() + " " +
94 name.getLast() );
95
96 // display user's name
97 JOptionPane.showMessageDialog( this, output, "Name",
98 JOptionPane.INFORMATION_MESSAGE );
99
100 } // end method viewJButtonActionPerformed
101
102 // main method
103 public static void main( String args[] )
104 {
105 ViewName application = new ViewName();
106 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
107
108 } // end method main
109
110 } // end class ViewName
```

```
1 // Name.java
2 // Represents name data and contains get and set methods.
3
4 public class Name
5 {
6 // Strings for storing first and last name
7 private String first = "";
8 private String last = "";
9
10 // Name constructor, first and last supplied
11 public Name( String firstValue, String lastValue )
12 {
13 setFirst( firstValue );
14 setLast( lastValue );
15
16 } // end constructor
17
18 // return first value
19 public String getFirst()
20 {
21 return first;
22
23 } // end method getFirst
24
25 // set first value
26 public void setFirst( String value )
27 {
28 first = value;
29
30 } // end method setFirst
31
32 // return last value
33 public String getLast()
34 {
35 return last;
36
37 } // end method getLast
38
39 // set last value
40 public void setLast( String value )
41 {
42 last = value;
43
44 } // end method setLast
45
46 } // end class Name
```

Computer Science & Information Technology

You might also like to view...

An advantage of inheritance is that:

a) all methods can be inherited b) all instance variables can be uniformly accessed by base classes and derived classes c) Objects of a derived class can be treated like objects of their base class d) None of the above.

Computer Science & Information Technology

A well-formed XML document that satisfies the rules of a DTD or schema is said to be a(n) _____ document.

A. invalid B. valid C. correct D. verified

Computer Science & Information Technology

A(n) ____ layer is a new layer added to the image to affect a large-scale tonal change.

a. mask b. filter c. adjustment d. style

Computer Science & Information Technology

The a+ mode argument opens the specified file for reading only and places the file pointer at the beginning of the file.

Answer the following statement true (T) or false (F)

Computer Science & Information Technology