(Counter Application) Create a Counter application (Fig. 4.13). Your Counter appli- cation’s GUI will consist of a JTextField and a JButton. The JTextField initially displays 0, but each time a user clicks the JButton, the value in the JTextField increases by
) Copying the template to your working directory. Copy the C:Examples Tutorial04ExercisesCounter 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:SimplyJavaCounter.
c) Compiling the template application. Compile your application by typing javac
Counter.java.
d) Running the template application. Run the Counter template application by typing java Counter. The GUI of the Counter template application should appear as shown in Fig. 4.14. Note the differences from Fig. 4.13.
e) Opening the template file. Open the Counter.java file in your text editor.
f) Customizing countJTextField. After
```
1 // Counter.java
2 // This application displays a number which increments each time the
3 // Count JButton is pressed.
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.*;
7
8 public class Counter extends JFrame
9 {
10 // JTextField for displaying total count
11 private JTextField countJTextField;
12
13 // JButton to initiate adding one to numeric value in JTextField
14 private JButton countJButton;
15
16 // no-argument constructor
17 public Counter()
18 {
19 createUserInterface();
20 }
21
22 // create and position GUI components; register event handler
23 private void createUserInterface()
24 {
25 // get content pane and set layout to null
26 Container contentPane = getContentPane();
27 contentPane.setLayout( null );
28
29 // set up countJTextField
30 countJTextField = new JTextField();
31 countJTextField.setText( "0" );
32 countJTextField.setHorizontalAlignment( JTextField.CENTER );
33 countJTextField.setBounds( 24, 24, 88, 21 );
34 countJTextField.setEditable( false );
35 contentPane.add( countJTextField );
36
37 // set up countJButton
38 countJButton = new JButton();
39 countJButton.setText( "Count" );
40 countJButton.setBounds( 24, 64, 88, 24 );
41 contentPane.add( countJButton );
42 countJButton.addActionListener(
43
44 new ActionListener() // anonymous inner class
45 {
46 // event handler called when countJButton is pressed
47 public void actionPerformed( ActionEvent event )
48 {
49 countJButtonActionPerformed( event );
50 }
51
52 } // end anonymous inner class
53
54 ); // end call to addActionListener
55
56 // set properties of application’s window
57 setTitle( "Counter" ); // set title bar text
58 setSize( 144, 136 ); // set window size
59 setVisible( true ); // display window
60
61 } // end method createUserInterface
62
63 // increment the count each time countJButton is pressed
64 private void countJButtonActionPerformed( ActionEvent event )
65 {
66 // increment the count displayed in countJTextField
67 countJTextField.setText( String.valueOf(
68 1 + Integer.parseInt( countJTextField.getText() ) ) );
69
70 } // end method countJButtonActionPerformed
71
72 // main method
73 public static void main( String args[] )
74 {
75 Counter application = new Counter();
76 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
77
78 } // end method main
79
80 } // end class Counter
```
You might also like to view...
In SharePoint, the ________ permission level enables users to view pages and documents, but not historical versions of documents
A) limited access B) manage hierarchy C) contribute D) restricted read
Black hat hackers are also known as script kiddies
Indicate whether the statement is true or false.