Create an application that allows the user to inventory DVDs. Users input the title of the DVD and bonus materials and that information is stored in an object. The GUI is provided for you (Fig. 18.43). You will create a class (DVDInfo) to represent the DVD object and another class (BonusInfo) to represent bonus materials for a DVD object such as the movie’s trailer.
a) Copying the template to your working directory. Copy the C:Examples Tutorial18ExercisesDVDInventory 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:SimplyJavaDVD- Inventory.
c) Opening the template files. Open the DVDInventory.java, DVDInfo.java and
BonusInfo.java files in your text editor.
d) Creating the BonusInfo class. View the BonusInfo class. Add code to this class so that its objects will each represent one bonus material item on the DVD. Each BonusInfo object should have a name (name) and a length (itemLength). Use this tutorial’s CookingTime class as your guide in creating the get and set methods for the name and length of each bonus material. Have your set methods truncate a name
longer than twenty characters and set the minutes of a
```
1 // DVDInventory.java
2 // An application that inventories DVDs.
3 import java.awt.*;
4 import java.awt.event.*;
5 import javax.swing.*;
6 import javax.swing.border.*;
7
8 public class DVDInventory extends JFrame
9 {
10 // JPanel for containing movie information
11 private JPanel informationJPanel;
12
13 // JLabel and JTextField for movie title
14 private JLabel movieJLabel;
15 private JTextField movieJTextField;
16
17 // JLabel and JTextField for minutes in movie
18 private JLabel minutes1JLabel;
19 private JTextField minutes1JTextField;
20
21 // JButton to create and display a DVDObject
22 private JButton createJButton;
23 private JButton informationJButton;
24
25 // JTextField for DVD creation message
26 private JTextField bottomJTextField;
27
28 // JPanel for containing bonus material information
29 private JPanel bonusJPanel;
30
31 // JLabels and JTextFields for bonus material
32 // descriptions and minutes in bonus material
33 private JLabel description1JLabel;
34 private JTextField description1JTextField;
35 private JLabel minutes2JLabel;
36 private JTextField minutes2JTextField;
37 private JLabel description2JLabel;
38 private JTextField description2JTextField;
39 private JLabel minutes3JLabel;
40 private JTextField minutes3JTextField;
41 private JLabel description3JLabel;
42 private JTextField description3JTextField;
43 private JLabel minutes4JLabel;
44 private JTextField minutes4JTextField;
45
46 // DVDInfo for storing DVD information
47 private DVDInfo dvd;
48
49 // no-argument constructor
50 public DVDInventory()
51 {
52 createUserInterface();
53 }
54
55 // create and position GUI components; register event handlers
56 private void createUserInterface()
57 {
58 // get content pane for attaching GUI components
59 Container contentPane = getContentPane();
60
61 // enable explicit positioning of GUI components
62 contentPane.setLayout( null );
63
64 // set up informationJPanel
65 informationJPanel = new JPanel();
66 informationJPanel.setBounds( 8, 8, 240, 112 );
67 informationJPanel.setBorder( new TitledBorder(
68 "DVD Information" ) );
69 informationJPanel.setLayout( null );
70 contentPane.add( informationJPanel );
71
72 // set up movieJLabel
73 movieJLabel = new JLabel();
74 movieJLabel.setBounds( 8, 32, 72, 24 );
75 movieJLabel.setText( "Movie title:" );
76 informationJPanel.add( movieJLabel );
77
78 // set up movieJTextField
79 movieJTextField = new JTextField();
80 movieJTextField.setBounds( 80, 32, 140, 24 );
81 informationJPanel.add( movieJTextField );
82
83 // set up minutes1JLabel
84 minutes1JLabel = new JLabel();
85 minutes1JLabel.setBounds( 8, 66, 56, 24 );
86 minutes1JLabel.setText( "Minutes:" );
87 informationJPanel.add( minutes1JLabel );
88
89 // set up minutes1JTextField
90 minutes1JTextField = new JTextField();
91 minutes1JTextField.setBounds( 168, 64, 50, 24 );
92 informationJPanel.add( minutes1JTextField );
93
94 // set up createJButton
95 createJButton = new JButton();
96 createJButton.setBounds( 78, 136, 100, 24 );
97 createJButton.setText( "Create" );
98 contentPane.add( createJButton );
99 createJButton.addActionListener(
100
101 new ActionListener() // anonymous inner class
102 {
103 // event handler called when createJButton is pressed
104 public void actionPerformed ( ActionEvent event )
105 {
106 createJButtonActionPerformed( event );
107 }
108
109 } // end anonymous inner class
110
111 ); // end call to addActionListener
112
113 // set up informationJButton
114 informationJButton = new JButton();
115 informationJButton.setBounds( 78, 168, 100, 24 );
116 informationJButton.setText( "Information" );
117 informationJButton.setEnabled( false );
118 contentPane.add( informationJButton );
119 informationJButton.addActionListener(
120
121 new ActionListener() // anonymous inner class
122 {
123 // event handler called when
124 // informationJButton is pressed
125 public void actionPerformed ( ActionEvent event )
126 {
127 informationJButtonActionPerformed( event );
128 }
129
130 } // end anonymous inner class
131
132 ); // end call to addActionListener
133
134 // set up bottomJTextField
135 bottomJTextField = new JTextField();
136 bottomJTextField.setBounds( 8, 206, 240, 24 );
137 bottomJTextField.setHorizontalAlignment( JTextField.CENTER );
138 bottomJTextField.setEditable( false );
139 contentPane.add( bottomJTextField );
140
141 // set up bonusJPanel
142 bonusJPanel = new JPanel();
143 bonusJPanel.setBounds( 264, 8, 248, 224 );
144 bonusJPanel.setBorder( new TitledBorder( "Bonus Materials" ) );
145 bonusJPanel.setLayout( null );
146 contentPane.add( bonusJPanel );
147
148 // set up description1JLabel
149 description1JLabel = new JLabel();
150 description1JLabel.setBounds( 8, 24, 74, 24 );
151 description1JLabel.setText( "Description:" );
152 bonusJPanel.add( description1JLabel );
153
154 // set up description1JTextField
155 description1JTextField = new JTextField();
156 description1JTextField.setBounds( 88, 24, 140, 24 );
157 bonusJPanel.add( description1JTextField );
158
159 // set up minutes2JLabel
160 minutes2JLabel = new JLabel();
161 minutes2JLabel.setBounds( 8, 56, 56, 24 );
162 minutes2JLabel.setText( "Minutes:" );
163 bonusJPanel.add( minutes2JLabel );
164
165 // set up minutes2JTextField
166 minutes2JTextField = new JTextField();
167 minutes2JTextField.setBounds( 178, 56, 50, 24 );
168 bonusJPanel.add( minutes2JTextField );
169
170 // set up description2JLabel
171 description2JLabel = new JLabel();
172 description2JLabel.setBounds( 8, 88, 74, 24 );
173 description2JLabel.setText( "Description:" );
174 bonusJPanel.add( description2JLabel );
175
176 // set up description2JTextField
177 description2JTextField = new JTextField();
178 description2JTextField.setBounds( 88, 88, 140, 24 );
179 bonusJPanel.add( description2JTextField );
180
181 // set up minutes3JLabel
182 minutes3JLabel = new JLabel();
183 minutes3JLabel.setBounds( 8, 120, 56, 24 );
184 minutes3JLabel.setText( "Minutes:" );
185 bonusJPanel.add( minutes3JLabel );
186
187 // set up minutes3JTextField
188 minutes3JTextField = new JTextField();
189 minutes3JTextField.setBounds( 178, 120, 50, 24 );
190 bonusJPanel.add( minutes3JTextField );
191
192 // set up description3JLabel
193 description3JLabel = new JLabel();
194 description3JLabel.setBounds( 8, 152, 74, 24 );
195 description3JLabel.setText( "Description:" );
196 bonusJPanel.add( description3JLabel );
197
198 // set up description3JTextField
199 description3JTextField = new JTextField();
200 description3JTextField.setBounds( 88, 152, 140, 24 );
201 bonusJPanel.add( description3JTextField );
202
203 // set up minutes4JLabel
204 minutes4JLabel = new JLabel();
205 minutes4JLabel.setBounds( 8, 184, 56, 24 );
206 minutes4JLabel.setText( "Minutes:" );
207 bonusJPanel.add( minutes4JLabel );
208
209 // set up minutes4JTextField
210 minutes4JTextField = new JTextField();
211 minutes4JTextField.setBounds( 178, 184, 50, 24 );
212 bonusJPanel.add( minutes4JTextField );
213
214 // set properties of application’s window
215 setTitle( "DVD Inventory" ); // set title bar string
216 setSize( 550, 265 ); // set window size
217 setVisible( true ); // display window
218
219 } // end method createUserInterface
220
221 // get user input and store in a new DVDObject
222 private void createJButtonActionPerformed( ActionEvent event )
223 {
224 BonusInfo bonus[] = new BonusInfo[ 3 ];
225
226 // store movie name
227 String movieTitle = movieJTextField.getText();
228
229 // store movie length
230 int movieMinutes = Integer.parseInt(
231 minutes1JTextField.getText() );
232
233 // bonus material description
234 String bonus1 = description1JTextField.getText();
235 String bonus2 = description2JTextField.getText();
236 String bonus3 = description3JTextField.getText();
237
238 // store bonus minutes from JTextFields
239 int bonusLength1 = Integer.parseInt(
240 minutes2JTextField.getText() );
241 int bonusLength2 = Integer.parseInt(
242 minutes3JTextField.getText() );
243 int bonusLength3 = Integer.parseInt(
244 minutes4JTextField.getText() );
245
246 // add bonus material name and time to the array
247 bonus[ 0 ] = new BonusInfo( bonus1, bonusLength1 );
248 bonus[ 1 ] = new BonusInfo( bonus2, bonusLength2 );
249 bonus[ 2 ] = new BonusInfo( bonus3, bonusLength3 );
250
251 // call constructor for new DVDInfo
252 dvd = new DVDInfo( movieTitle, bonus, movieMinutes );
253
254 // let the user know about the progress
255 bottomJTextField.setText(
256 "Your DVD was created successfully!" );
257
258 // enable informationJButton
259 informationJButton.setEnabled( true );
260
261 } // end method
You might also like to view...
Describe two ways to handle the IPv4-to-IPv6 migration process.
What will be an ideal response?
____ are very powerful, as they allow administrators complete control over path selection.
A. RIP routes B. Dynamic routes C. Static routes D. Stub routes