(Vending Machine GUI) In this exercise, you will customize the properties of several JLabels that display images (as shown in Fig. 2.43).




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


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:SimplyJavaVendingMachine, then pressing Enter.


c) Compiling the template application. Compile your application by typing the com- mand javac VendingMachine.java, then pressing Enter.


d) Running the template application. Run the application by typing java Vending- Machine. The GUI of the VendingMachine template application should appear as shown in Fig. 2.44.





e) Closing the application. Close the running application by clicking its close button.


This returns

```
1 // VendingMachine.java
2 // Creates vending machine GUI.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class VendingMachine extends JFrame
9 {
10 private JButton oneJButton, twoJButton, threeJButton, fourJButton,
11 aJButton, bJButton, pushJButton;
12 private JLabel a1JLabel, a2JLabel, a3JLabel, b1JLabel, b2JLabel,
13 b3JLabel, a1IconJLabel, a2IconJLabel, a3IconJLabel,
14 b1IconJLabel, b2IconJLabel, b3IconJLabel;
15 private JPanel windowJPanel, selectionJPanel;
16 private JTextField displayJTextField;
17
18 // no-argument constructor
19 public VendingMachine()
20 {
21 createUserInterface();
22 }
23
24 // create and position GUI components
25 private void createUserInterface()
26 {
27 // get content pane and set layout to null
28 Container contentPane = getContentPane();
29 contentPane.setLayout( null );
30
31 // set up windowJPanel
32 windowJPanel = new JPanel();
33 windowJPanel.setBounds( 10, 10, 190, 170 );
34 windowJPanel.setBorder( new LineBorder( Color.BLACK ) );
35 windowJPanel.setLayout( null );
36 contentPane.add( windowJPanel );
37
38 // set up a1IconJLabel
39 a1IconJLabel = new JLabel();
40 a1IconJLabel.setIcon( new ImageIcon( "images/cookie.png" ) );
41 a1IconJLabel.setBounds( 10, 10, 50, 50 );
42 windowJPanel.add( a1IconJLabel );
43
44 // set up a1JLabel
45 a1JLabel = new JLabel();
46 a1JLabel.setText( "A1" );
47 a1JLabel.setBounds( 10, 60, 50, 20 );
48 a1JLabel.setHorizontalAlignment( JLabel.CENTER );
49 windowJPanel.add( a1JLabel );
50
51 // set up a2IconJLabel
52 a2IconJLabel = new JLabel();
53 a2IconJLabel.setIcon( new ImageIcon( "images/gum.png" ) );
54 a2IconJLabel.setBounds( 70, 10, 50, 50 );
55 windowJPanel.add( a2IconJLabel );
56
57 // set up a2JLabel
58 a2JLabel = new JLabel();
59 a2JLabel.setText( "A2" );
60 a2JLabel.setBounds( 70, 60, 50, 20 );
61 a2JLabel.setHorizontalAlignment( JLabel.CENTER );
62 windowJPanel.add( a2JLabel );
63
64 // set up a3IconJLabel
65 a3IconJLabel = new JLabel();
66 a3IconJLabel.setIcon( new ImageIcon( "images/pretzel.png" ) );
67 a3IconJLabel.setBounds( 130, 10, 50, 50 );
68 windowJPanel.add( a3IconJLabel );
69
70 // set up a3JLabel
71 a3JLabel = new JLabel();
72 a3JLabel.setText( "A3" );
73 a3JLabel.setBounds( 130, 60, 50, 20 );
74 a3JLabel.setHorizontalAlignment( JLabel.CENTER );
75 windowJPanel.add( a3JLabel );
76
77 // set up b1IconJLabel
78 b1IconJLabel = new JLabel();
79 b1IconJLabel.setIcon( new ImageIcon( "images/pretzel.png" ) );
80 b1IconJLabel.setBounds( 10, 90, 50, 50 );
81 windowJPanel.add( b1IconJLabel );
82
83 // set up b1JLabel
84 b1JLabel = new JLabel();
85 b1JLabel.setText( "B1" );
86 b1JLabel.setBounds( 10, 140, 50, 20 );
87 b1JLabel.setHorizontalAlignment( JLabel.CENTER );
88 windowJPanel.add( b1JLabel );
89
90 // set up b2IconJLabel
91 b2IconJLabel = new JLabel();
92 b2IconJLabel.setIcon( new ImageIcon( "images/cookie.png" ) );
93 b2IconJLabel.setBounds( 70, 90, 50, 50 );
94 windowJPanel.add( b2IconJLabel );
95
96 // set up b2JLabel
97 b2JLabel = new JLabel();
98 b2JLabel.setText( "B2" );
99 b2JLabel.setBounds( 70, 140, 50, 20 );
100 b2JLabel.setHorizontalAlignment( JLabel.CENTER );
101 windowJPanel.add( b2JLabel );
102
103 // set up b3IconJLabel
104 b3IconJLabel = new JLabel();
105 b3IconJLabel.setIcon( new ImageIcon( "images/soda.png" ) );
106 b3IconJLabel.setBounds( 130, 90, 50, 50 );
107 windowJPanel.add( b3IconJLabel );
108
109 // set up b3JLabel
110 b3JLabel = new JLabel();
111 b3JLabel.setText( "B3" );
112 b3JLabel.setBounds( 130, 140, 50, 20 );
113 b3JLabel.setHorizontalAlignment( JLabel.CENTER );
114 windowJPanel.add( b3JLabel );
115
116 // set up pushJButton
117 pushJButton = new JButton();
118 pushJButton.setText( "PUSH" );
119 pushJButton.setBounds( 10, 190, 190, 30 );
120 contentPane.add( pushJButton );
121
122 // set up displayJTextField
123 displayJTextField = new JTextField();
124 displayJTextField.setText( "B2" );
125 displayJTextField.setBounds( 210, 10, 170, 30 );
126 displayJTextField.setEditable( false );
127 displayJTextField.setHorizontalAlignment( JLabel.CENTER );
128 contentPane.add( displayJTextField );
129
130 // set up selectionJPanel
131 selectionJPanel = new JPanel();
132 selectionJPanel.setBounds( 210, 50, 170, 130 );
133 selectionJPanel.setBorder( new TitledBorder( new EtchedBorder(
134 EtchedBorder.LOWERED ), "Please make selection" ) );
135 selectionJPanel.setLayout( null );
136 contentPane.add( selectionJPanel );
137
138 // set up aJButton
139 aJButton = new JButton();
140 aJButton.setText( "A" );
141 aJButton.setBounds( 40, 20, 42, 42 );
142 selectionJPanel.add( aJButton );
143
144 // set up bJButton
145 bJButton = new JButton();
146 bJButton.setText( "B" );
147 bJButton.setBounds( 90, 20, 42, 42 );
148 selectionJPanel.add( bJButton );
149
150 // set up oneJButton
151 oneJButton = new JButton();
152 oneJButton.setText( "1" );
153 oneJButton.setBounds( 12, 72, 42, 42 );
154 selectionJPanel.add( oneJButton );
155
156 // set up twoJButton
157 twoJButton = new JButton();
158 twoJButton.setText( "2" );
159 twoJButton.setBounds( 64, 72, 42, 42 );
160 selectionJPanel.add( twoJButton );
161
162 // set up threeJButton
163 threeJButton = new JButton();
164 threeJButton.setText( "3" );
165 threeJButton.setBounds( 116, 72, 42, 42 );
166 selectionJPanel.add( threeJButton );
167
168 // set properties of application’s window
169 setTitle( "VendingMachine" ); // set title bar text
170 setSize( 395, 255 ); // set window size
171 setVisible( true ); // display window
172
173 } // end method createUserInterface
174
175 // main method
176 public static void main( String args[] )
177 {
178 VendingMachine application = new VendingMachine();
179 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
180
181 } // end method main
182
183 } // end class VendingMachine
```

Computer Science & Information Technology

You might also like to view...

The item shown in the accompanying figure is the Library panel.

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

Computer Science & Information Technology

DML triggers are used to ________.

maliciously attack databases produce automatic responses if the data of the database has been altered Both maliciously attack databases and produce automatic responses if the data of the database has been altered Neither maliciously attack databases nor produce automatic responses if the data of the database has been altered

Computer Science & Information Technology