(Computer Monitor Invoice GUI) In this exercise, you apply the GUI design guide- lines you have learned to a graphical user interface for an invoicing application (Fig. 3.24). You will specify the bounds of the JTextFields in the rows of the GUI that begin with the 15", 17" and 19" JLabels.
a) Copying the template to your working directory. Copy the C:Examples Tutorial03ExercisesMonitorInvoice directory to your C:SimplyJava direc- tory.
b) Opening the Command Prompt window and changing directories. Open the Com- mand Prompt by selecting Start > Programs > Accessories > Command Prompt. Change to your working directory by typing cd C:SimplyJavaMonitorInvoice, then pressing Enter.
c) Compiling the template application. Compile your application by typing javac
MonitorInvoice.java, then pressing Enter.
d) Running the template application. Run the application by typing java MonitorIn- voice. The GUI of the Monitor Invoice template application should appear as shown in Fig. 3.25.
e) Closing the application. Close your running application by clicking its close button. f) Opening the template file. Open the MonitorInvoice.java file in your text editor.
g) Customizing the JTextFields below the Quantity: JLabel. Using Fig. 3.24 and the template code
```
1 // Exercise 3.14: MonitorInvoice.java
2 // GUI for invoice application.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class MonitorInvoice extends JFrame
9 {
10 // JLabel that displays title in window
11 private JLabel titleJLabel;
12
13 // JLabel and JTextField for invoice number
14 private JLabel invoiceNumberJLabel;
15 private JTextField invoiceNumberJTextField;
16
17 // JLabel and JTextField for invoice date
18 private JLabel invoiceDateJLabel;
19 private JTextField invoiceDateJTextField;
20
21 // JLabel and JTextField for company name
22 private JLabel companyNameJLabel;
23 private JTextField companyNameJTextField;
24
25 // JLabel and JTextField for street address
26 private JLabel addressJLabel;
27 private JTextField addressJTextField;
28
29 // JLabel and JTextField for city, state and zip code
30 private JLabel cityStateZipJLabel;
31 private JTextField cityStateZipJTextField;
32
33 // JLabels for column heads
34 private JLabel sizeJLabel;
35 private JLabel quantityJLabel;
36 private JLabel priceJLabel;
37 private JLabel totalsJLabel;
38
39 // JLabel and JTextFields for 15" monitors
40 private JLabel fifteenJLabel;
41 private JTextField quantity15JTextField;
42 private JTextField price15JTextField;
43 private JTextField totals15JTextField;
44
45 // JLabel and JTextFields for 17" monitors
46 private JLabel seventeenJLabel;
47 private JTextField quantity17JTextField;
48 private JTextField price17JTextField;
49 private JTextField totals17JTextField;
50
51 // JLabel and JTextFields for 19" monitors
52 private JLabel nineteenJLabel;
53 private JTextField quantity19JTextField;
54 private JTextField price19JTextField;
55 private JTextField totals19JTextField;
56
57 // JLabel and JTextField subtotal
58 private JLabel subtotalJLabel;
59 private JTextField subtotalJTextField;
60
61 // JLabel and JTextField for taxes
62 private JLabel taxJLabel;
63 private JTextField taxJTextField;
64
65 // JLabel and JTextField for total
66 private JLabel totalJLabel;
67 private JTextField totalJTextField;
68
69 // JButtons to save, calculate results and clear JTextFields
70 private JButton saveJButton;
71 private JButton calculateJButton;
72 private JButton clearJButton;
73
74 // no-argument constructor
75 public MonitorInvoice()
76 {
77 createUserInterface();
78 }
79
80 // create and position GUI components
81 public void createUserInterface()
82 {
83 // get content pane and set its layout
84 Container container = getContentPane();
85 container.setLayout( null );
86
87 // set up titleJLabel
88 titleJLabel = new JLabel();
89 titleJLabel.setBounds( 0, 16, 378, 32 );
90 titleJLabel.setText( "Monitor Invoice Application" );
91 titleJLabel.setHorizontalAlignment( JLabel.CENTER );
92 titleJLabel.setFont( new Font(
93 titleJLabel.getFont().getName(),
94 titleJLabel.getFont().getStyle(), 20 ) );
95 container.add( titleJLabel );
96
97 // set up invoiceNumberJLabel
98 invoiceNumberJLabel = new JLabel();
99 invoiceNumberJLabel.setBounds( 16, 64, 92, 21 );
100 invoiceNumberJLabel.setText( "Invoice number:" );
101 container.add( invoiceNumberJLabel );
102
103 // set up invoiceDateJLabel
104 invoiceDateJLabel = new JLabel();
105 invoiceDateJLabel.setBounds( 176, 64, 72, 21 );
106 invoiceDateJLabel.setText( "Invoice date:" );
107 container.add( invoiceDateJLabel );
108
109 // set up companyNameJLabel
110 companyNameJLabel = new JLabel();
111 companyNameJLabel.setBounds( 16, 104, 92, 21 );
112 companyNameJLabel.setText( "Company name:" );
113 container.add( companyNameJLabel );
114
115 // set up addressJLabel
116 addressJLabel = new JLabel();
117 addressJLabel.setBounds( 16, 144, 92, 21 );
118 addressJLabel.setText( "Address (line 1):" );
119 container.add( addressJLabel );
120
121 // set up cityStateZipJLabel
122 cityStateZipJLabel = new JLabel();
123 cityStateZipJLabel.setBounds( 16, 184, 92, 21 );
124 cityStateZipJLabel.setText( "Address (line 2):" );
125 container.add( cityStateZipJLabel );
126
127 // set up sizeJLabel
128 sizeJLabel = new JLabel();
129 sizeJLabel.setBounds( 20, 224, 80, 21 );
130 sizeJLabel.setText( "Monitor size:" );
131 sizeJLabel.setHorizontalAlignment( JLabel.RIGHT );
132 container.add( sizeJLabel );
133
134 // set up quantityJLabel
135 quantityJLabel = new JLabel();
136 quantityJLabel.setBounds( 112, 224, 56, 21 );
137 quantityJLabel.setText( "Quantity:" );
138 container.add( quantityJLabel );
139
140 // set up priceJLabel
141 priceJLabel = new JLabel();
142 priceJLabel.setBounds( 192, 224, 42, 21 );
143 priceJLabel.setText( "Price:" );
144 container.add( priceJLabel );
145
146 // set up totalsJLabel
147 totalsJLabel = new JLabel();
148 totalsJLabel.setBounds( 288, 224, 40, 21 );
149 totalsJLabel.setText( "Totals:" );
150 container.add( totalsJLabel );
151
152 // set up fifteenJLabel
153 fifteenJLabel = new JLabel();
154 fifteenJLabel.setBounds( 20, 248, 80, 21 );
155 fifteenJLabel.setText( "15\":" );
156 fifteenJLabel.setHorizontalAlignment( JLabel.RIGHT );
157 container.add( fifteenJLabel );
158
159 // set up seventeenJLabel
160 seventeenJLabel = new JLabel();
161 seventeenJLabel.setBounds( 20, 280, 80, 21 );
162 seventeenJLabel.setText( "17\":" );
163 seventeenJLabel.setHorizontalAlignment( JLabel.RIGHT );
164 container.add( seventeenJLabel );
165
166 // set up nineteenJLabel
167 nineteenJLabel = new JLabel();
168 nineteenJLabel.setBounds( 20, 312, 80, 21 );
169 nineteenJLabel.setText( "19\":" );
170 nineteenJLabel.setHorizontalAlignment( JLabel.RIGHT );
171 container.add( nineteenJLabel );
172
173 // set up subtotalJLabel
174 subtotalJLabel = new JLabel();
175 subtotalJLabel.setBounds( 224, 344, 56, 21 );
176 subtotalJLabel.setText( "Subtotal:" );
177 container.add( subtotalJLabel );
178
179 // set up taxJLabel
180 taxJLabel = new JLabel();
181 taxJLabel.setBounds( 224, 376, 32, 21 );
182 taxJLabel.setText( "Tax:" );
183 container.add( taxJLabel );
184
185 // set up totalJLabel
186 totalJLabel = new JLabel();
187 totalJLabel.setBounds( 224, 408, 40, 21 );
188 totalJLabel.setText( "Total:" );
189 container.add( totalJLabel );
190
191 // set up invoiceNumberJTextField
192 invoiceNumberJTextField = new JTextField();
193 invoiceNumberJTextField.setBounds( 122, 64, 38, 21 );
194 invoiceNumberJTextField.setText( "128" );
195 container.add( invoiceNumberJTextField );
196
197 // set up invoiceDateJTextField
198 invoiceDateJTextField = new JTextField();
199 invoiceDateJTextField.setBounds( 256, 64, 104, 21 );
200 invoiceDateJTextField.setText( "04/01/2003" );
201 container.add( invoiceDateJTextField );
202
203 // set up companyNameJTextField
204 companyNameJTextField = new JTextField();
205 companyNameJTextField.setBounds( 122, 104, 238, 21 );
206 companyNameJTextField.setText( "Deitel & Associates" );
207 container.add( companyNameJTextField );
208
209 // set up addressJTextField
210 addressJTextField = new JTextField();
211 addressJTextField.setBounds( 122, 144, 238, 21 );
212 addressJTextField.setText( "100 Main Street" );
213 container.add( addressJTextField );
214
215 // set up cityStateZipJTextField
216 cityStateZipJTextField = new JTextField();
217 cityStateZipJTextField.setBounds( 122, 184, 238, 21 );
218 cityStateZipJTextField.setText( "Some Town, MA 00000" );
219 container.add( cityStateZipJTextField );
220
221 // set up quantity15JTextField
222 quantity15JTextField = new JTextField();
223 quantity15JTextField.setBounds( 112, 248, 64, 21 );
224 quantity15JTextField.setText( "10" );
225 quantity15JTextField.setHorizontalAlignment(
226 JTextField.RIGHT );
227 container.add( quantity15JTextField );
228
229 // set up quantity17JTextField
230 quantity17JTextField = new JTextField();
231 quantity17JTextField.setBounds( 112, 280, 64, 21 );
232 quantity17JTextField.setText( "0" );
233 quantity17JTextField.setHorizontalAlignment(
234 JTextField.RIGHT );
235 container.add( quantity17JTextField );
236
237 // set up quantity19JTextField
238 quantity19JTextField = new JTextField();
239 quantity19JTextField.setBounds( 112, 312, 64, 21 );
240 quantity19JTextField.setText( "0" );
241 quantity19JTextField.setHorizontalAlignment(
242 JTextField.RIGHT );
243 container.add( quantity19JTextField );
244
245 // set up price15JTextField price15JTextField = new
246 JTextField(); price15JTextField.setBounds( 192, 248, 80, 21 );
247 price15JTextField.setText( "150" );
248 price15JTextField.setHorizontalAlignment( JTextField.RIGHT );
249 container.add( price15JTextField );
250
251
252 // set up price17JTextField price17JTextField = new
253
You might also like to view...
A(n) ________ is a database object that is used to add data into a table
Fill in the blank(s) with correct word
The area at the bottom of the desktop which displays the Start button, pinned application buttons, the notification area, and the Show desktop button is called the ________
A) status bar B) taskbar C) actionbar D) activity bar