(Calculator GUI) Many components have the same kinds of properties as JLabels. For instance, JButtons also have size, location and text properties. In this exercise, you will cus- tomize the plus (+) JButton in the Calculator GUI shown in Fig. 2.35.



a) Copying the template to your working directory. Copy the C:Examples Tutorial02ExercisesCalculator 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:SimplyJavaCalculator, then pressing Enter.

c) Compiling the template application. Compile your application by typing the command javac Calculator.java, then pressing Enter.

d) Running the template application. Run the application by typing java Calculator.

The GUI of the Calculator template application should appear as shown in Fig. 2.36. Notice that the plus (+) JButton is missing. The template application creates the plus (+) JButton, but the JButton’s default width and height are both zero, so it does not appear on the screen. You will customize the plus (+) JButton’s text and bounds properties to make it appear as in Fig. 2.35.

![13891|436x242](uplo

```
1
2 // Create GUI calculator
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.border.*;
7
8 public class Calculator extends JFrame
9 {
10 private JButton oneJButton, twoJButton, threeJButton,
11 fourJButton, fiveJButton, sixJButton, sevenJButton,
12 eightJButton, nineJButton, zeroJButton, doubleZeroJButton,
13 plusJButton, minusJButton, timesJButton, divideJButton,
14 equalsJButton, decimalJButton, clearJButton, clearAllJButton,
15 offJButton;
16 private JTextField displayJTextField;
17 private JPanel numberJPanel, operationJPanel, clearJPanel;
18
19 // no-argument constructor
20 public Calculator()
21 {
22 createUserInterface();
23 }
24
25 // create and position GUI components; register event handlers
26 private void createUserInterface()
27 {
28 // get content pane and set layout to null
29 Container contentPane = getContentPane();
30 contentPane.setLayout( null );
31
32 // set up displayJTextField
33 displayJTextField = new JTextField();
34 displayJTextField.setText( "0" );
35 displayJTextField.setBounds( 16, 16, 480, 24 );
36 displayJTextField.setHorizontalAlignment( JTextField.RIGHT );
37 contentPane.add( displayJTextField );
38
39 // set up numberJPanel
40 numberJPanel = new JPanel();
41 numberJPanel.setLayout( null );
42 numberJPanel.setBounds( 16, 62, 176, 224 );
43 numberJPanel.setBorder(
44 new BevelBorder( BevelBorder.LOWERED ) );
45 contentPane.add( numberJPanel );
46
47 // set up oneJButton
48 oneJButton = new JButton();
49 oneJButton.setText( "1" );
50 oneJButton.setBounds( 16, 16, 48, 48 );
51 numberJPanel.add( oneJButton );
52
53 // set up two JButton
54 twoJButton = new JButton();
55 twoJButton.setText( "2" );
56 twoJButton.setBounds( 64, 16, 48, 48 );
57 numberJPanel.add( twoJButton );
58
59 // set up threeJButton
60 threeJButton = new JButton();
61 threeJButton.setText( "3" );
62 threeJButton.setBounds( 112, 16, 48, 48 );
63 numberJPanel.add( threeJButton );
64
65 // set up fourJButton
66 fourJButton = new JButton();
67 fourJButton.setText( "4" );
68 fourJButton.setBounds( 16, 64, 48, 48 );
69 numberJPanel.add( fourJButton );
70
71 // set up fiveJButton
72 fiveJButton = new JButton();
73 fiveJButton.setText( "5" );
74 fiveJButton.setBounds( 64, 64, 48, 48 );
75 numberJPanel.add( fiveJButton );
76
77 // set up sixJButton
78 sixJButton = new JButton();
79 sixJButton.setText( "6" );
80 sixJButton.setBounds( 112, 64, 48, 48 );
81 numberJPanel.add( sixJButton );
82
83 // set up sevenJButton
84 sevenJButton = new JButton();
85 sevenJButton.setText( "7" );
86 sevenJButton.setBounds( 16, 112, 48, 48 );
87 numberJPanel.add( sevenJButton );
88
89 // set up eightJButton
90 eightJButton = new JButton();
91 eightJButton.setText( "8" );
92 eightJButton.setBounds( 64, 112, 48, 48 );
93 numberJPanel.add( eightJButton );
94
95 // set up nineJButton
96 nineJButton = new JButton();
97 nineJButton.setText( "9" );
98 nineJButton.setBounds( 112, 112, 48, 48 );
99 numberJPanel.add( nineJButton );
100
101 // set up zeroJButton
102 zeroJButton = new JButton();
103 zeroJButton.setText( "0" );
104 zeroJButton.setBounds( 16, 160, 48, 48 );
105 numberJPanel.add( zeroJButton );
106
107 // set up doubleZeroJButton
108 doubleZeroJButton = new JButton();
109 doubleZeroJButton.setText( "00" );
110 doubleZeroJButton.setBounds( 64, 160, 96, 48 );
111 numberJPanel.add( doubleZeroJButton );
112
113 // set up operationJPanel
114 operationJPanel = new JPanel();
115 operationJPanel.setLayout( null );
116 operationJPanel.setBounds( 224, 62, 144, 224 );
117 operationJPanel.setBorder(
118 new BevelBorder( BevelBorder.LOWERED ) );
119 contentPane.add( operationJPanel );
120
121 // set up plusJButton
122 plusJButton = new JButton();
123
124

plusJButton.setText( "+" );
plusJButton.setBounds( 16, 16, 48, 128 );
125 plusJButton.setFont(
126 new Font( "SansSerif", Font.PLAIN, 18 ) );
127 operationJPanel.add( plusJButton );
128
129 // set up minusJButton
130 minusJButton = new JButton();
131 minusJButton.setText( "-" );
132 minusJButton.setBounds( 80, 16, 48, 48 );
133 minusJButton.setFont(
134 new Font( "SansSerif", Font.PLAIN, 18 ) );
135 operationJPanel.add( minusJButton );
136
137 // set up timesJButton
138 timesJButton = new JButton();
139 timesJButton.setText( "*" );
140 timesJButton.setBounds( 80, 64, 48, 48 );
141 timesJButton.setFont(
142 new Font( "SansSerif", Font.PLAIN, 18 ) );
143 operationJPanel.add( timesJButton );
144
145 // set up divideJButton
146 divideJButton = new JButton();
147 divideJButton.setText( "/" );
148 divideJButton.setBounds( 80, 112, 48, 48 );
149 divideJButton.setFont(
150 new Font( "SansSerif", Font.PLAIN, 18 ) );
151 operationJPanel.add( divideJButton );
152
153 // set up equalsJButton
154 equalsJButton = new JButton();
155 equalsJButton.setText( "=" );
156 equalsJButton.setBounds( 80, 160, 48, 48 );
157 equalsJButton.setFont(
158 new Font( "SansSerif", Font.PLAIN, 18 ) );
159 operationJPanel.add( equalsJButton );
160
161 // set up decimalJButton
162 decimalJButton = new JButton();
163 decimalJButton.setText( "." );
164 decimalJButton.setBounds( 16, 160, 48, 48 );
165 decimalJButton.setFont(
166 new Font( "SansSerif", Font.PLAIN, 18 ) );
167 operationJPanel.add( decimalJButton );
168
169 // set up clearJPanel
170 clearJPanel = new JPanel();
171 clearJPanel.setLayout( null );
172 clearJPanel.setBounds( 400, 62, 96, 144 );
173 clearJPanel.setBorder(
174 new BevelBorder( BevelBorder.LOWERED ) );
175 contentPane.add( clearJPanel );
176
177 // set up clearJButton
178 clearJButton = new JButton();
179 clearJButton.setText( "C" );
180 clearJButton.setBounds( 16, 16, 64, 48 );
181 clearJPanel.add( clearJButton );
182
183 // set up clearAllJButton
184 clearAllJButton = new JButton();
185 clearAllJButton.setText( "C/A" );
186 clearAllJButton.setBounds( 16, 80, 64, 48 );
187 clearJPanel.add( clearAllJButton );
188
189 // set up offJButton
190 offJButton = new JButton();
191 offJButton.setText( "OFF" );
192 offJButton.setBounds( 400, 238, 96, 48 );
193 contentPane.add( offJButton );
194
195 // set properties of application’s window
196 setTitle( "Calculator" ); // set title bar text
197 setSize( 520, 330 ); // set widow size
198 setVisible( true ); // display window
199
200 } // end method createUserInterface
201
202 // main method
203 public static void main( String args[] )
204 {
205 Calculator application = new Calculator();
206 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
207
208 } // end method main
209
210 } // end class Calculator
```

Computer Science & Information Technology

You might also like to view...

A ________ provides a visual summary of the layouts available in a Navigation form

A) wizard B) menu C) module D) gallery

Computer Science & Information Technology

Which of the following is NOT an item generally included in a forensic kit?

A) Latex gloves B) General case intake form C) Flashlight D) USB external drive

Computer Science & Information Technology