(Table of Powers Application) Write an application that displays a table of numbers from 1 to an upper limit, along with ethe square and cube each number. The user should specify the upper limit, and the results should be displayed in a JTextArea as in Fig. 8.23.
```
a) Copying the template to your working directory. Copy the directory C:Examples Tutorial08ExercisesTableOfPowers to your C:SimplyJava directory.
b) Opening the template file. Open the TableOfPowers.java file in your text editor.
c) Handling the keyPressed event for the Upper limit: JTextField. Add code to the limitJTextFieldKeyPressed event handler (which begins in line 97) to clear outputJTextArea.
d) Adding code to the Calculate JButton event handler. Add the code specified in steps e through j to the calculateJButtonActionPerformed event handler, which immediately follows the limitJTextFieldKeyPressed event handler.
e) Declaring a variable to store the loop counter. In the calculateJButtonAction- Performed event handler, declare int variable counter and initialize its value to 1.
f) Clearing outputJTextArea. Add a statement to calculateJButtonActionPer- formed that uses JTextArea method setText to clear any previous output in outputJTextAr
```
1 // Exercise 8.11: TableOfPowers.java
2 // Displays a table of powers up to a given limit.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6
7 public class TableOfPowers extends JFrame
8 {
9 // JLabel and JTextField for limit of loop counter
10 private JLabel limitJLabel;
11 private JTextField limitJTextField;
12
13 // JTextArea for displaying results
14 private JTextArea outputJTextArea;
15
16 // JButton to initiate calculations
17 private JButton calculateJButton;
18
19 // no-argument constructor
20 public TableOfPowers()
21 {
22 createUserInterface();
23 }
24
25 // create and position GUI components; register event handlers
26 private void createUserInterface()
27 {
28 // get content pane for attaching GUI components
29 Container contentPane = getContentPane();
30
31 // enable explicit positioning of GUI components
32 contentPane.setLayout( null );
33
34 // set up limitJLabel
35 limitJLabel = new JLabel();
36 limitJLabel.setBounds( 16, 16, 64, 26 );
37 limitJLabel.setText( "Upper limit:" );
38 contentPane.add( limitJLabel );
39
40 // set up limitJTextField
41 limitJTextField = new JTextField();
42 limitJTextField.setBounds( 85, 16, 59, 26 );
43 limitJTextField.setHorizontalAlignment( JTextField.RIGHT );
44 contentPane.add( limitJTextField );
45 limitJTextField.addKeyListener(
46
47 new KeyAdapter() // anonymous inner class
48 {
49 // event handler called if key pressed in limitJTextField
50 public void keyPressed( KeyEvent event )
51 {
52 limitJTextFieldKeyPressed( event );
53 }
54
55 } // end anonymous inner class
56
57 ); // end call to addKeyListener
58
59 // set up calculateJButton
60 calculateJButton = new JButton();
61 calculateJButton.setBounds( 166, 16, 130, 26 );
62 calculateJButton.setText( "Calculate" );
63 contentPane.add( calculateJButton );
64 calculateJButton.addActionListener(
65
66 new ActionListener() // anonymous inner class
67
68
69
70
71
72
73
74
75
76 );
77
78 //
79 outputJTextArea = new JTextArea();
80 outputJTextArea.setEditable( false );
81
82 // create JScrollPane that provides scrolling
83 // for outputJTextArea
84 JScrollPane scrollJScrollPane =
85 new JScrollPane( outputJTextArea );
86 scrollJScrollPane.setBounds( 16, 56, 280, 95 );
87 contentPane.add( scrollJScrollPane );
88
89 // set properties of application’s window
90 setTitle( "Table Of Powers" ); // set title bar text
91 setSize( 320, 195 ); // set window's size
92 setVisible( true ); // display window
93
94 } // end method createUserInterface
95
96 // method called when user presses key in limitJTextField
97 private void limitJTextFieldKeyPressed( KeyEvent event )
98 {
99 outputJTextArea.setText( "" ); // clear outputJTextArea
100 }
101
102 // method called when user clicks calculateJButton
103 private void calculateJButtonActionPerformed( ActionEvent event
104 {
105 int counter = 1;
106
107 // clear JTextArea outputJTextArea
108 outputJTextArea.setText ( "" );
109
110 // add header
111 outputJTextArea.append( "n\tn-squared\tn-cubed" );
112
113 // obtain upper limit
114 int limit = Integer.parseInt( limitJTextField.getText() );
115
116 // calculate and display square and cube of 1 to limit
117 while ( counter <= limit )
118 {
119 // display result
120 outputJTextArea.append ( "\n" + counter + "\t" +
121 counter * counter + "\t" + counter * counter * counter );
122
123 counter++; // increment counter
124 }
125
126 } // end method calculateJButtonActionPerformed
127
128 // main method
129 public static void main( String args[] )
130 {
131 TableOfPowers application = new TableOfPowers();
132 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
133
134 } // end method main
135
136 } // end class TableOfPowers
```
You might also like to view...
What is an action listener? What is an action event?
What will be an ideal response?
In Google Spreadsheets, Web functions, such as GoogleLookup, access live data on the Web by requiring the user to specify the information for which to search and to then bring the search results into the ____________________.
Fill in the blank(s) with the appropriate word(s).
double[] studentScores; double studentScores[]; ? Are both of the above statements valid for declaring an array variable? Why or why not?
What will be an ideal response?
The virus signature file is maintained by what type of software?
A. antivirus B. keylogger C. remote control D. firewall