(Microwave Oven GUI) JPanels, like other GUI components, have bounds and back- ground properties. In this exercise, you will customize the bounds and background properties of the JPanel that represents a microwave oven’s door. The completed GUI is shown in Fig. 2.39.




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


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


d) Running the template application. Run the application by typing java Microwave- Oven. The GUI of the Microwave template application should appear as shown in Fig. 2.40.





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


This returns you to th


```
1
2 // Create GUI Microwave
3 import javax.swing.*;
4 import javax.swing.border.*;
5 import java.awt.*;
6 import java.awt.event.*;
7
8 public class Microwave extends JFrame
9 {
10 private JLabel displayJLabel;
11 private JButton oneJButton, twoJButton, threeJButton,
12 fourJButton, fiveJButton, sixJButton, sevenJButton,
13 eightJButton, nineJButton, zeroJButton, startJButton,
14 clearJButton;
15 private JPanel doorJPanel, controlJPanel;
16
17 // no-argument constructor
18 public Microwave()
19 {
20 createUserInterface();
21 }
22
23 // create and position GUI components; register event handlers
24 private void createUserInterface()
25 {
26 // get content pane and set layout to null
27 Container contentPane = getContentPane();
28 contentPane.setLayout( null );
29
30 // set up doorJPanel
31 doorJPanel = new JPanel();
32 doorJPanel.setBounds( 16, 16, 328, 284 );
33 doorJPanel.setBackground( Color.GRAY );
34 contentPane.add( doorJPanel );
35
36 // set up controlJPanel
37 controlJPanel = new JPanel();
38 controlJPanel.setLayout( null );
39 controlJPanel.setBounds( 368, 16, 182, 284 );
40 controlJPanel.setBorder( new LineBorder( Color.BLACK ) );
41 contentPane.add( controlJPanel );
42
43 // set up displayJLabel
44 displayJLabel = new JLabel();
45 displayJLabel.setBounds( 31, 13, 120, 30 );
46 displayJLabel.setText( "12:00" );
47 displayJLabel.setBorder( new LineBorder( Color.BLACK ) );
48 displayJLabel.setHorizontalAlignment( JLabel.CENTER );
49 controlJPanel.add( displayJLabel );
50
51 // set up oneJButton
52 oneJButton = new JButton();
53 oneJButton.setBounds( 23, 56, 45, 45 );
54 oneJButton.setText( "1" );
55 controlJPanel.add( oneJButton );
56
57 // set up twoJButton
58 twoJButton = new JButton();
59 twoJButton.setBounds( 68, 56, 45, 45 );
60 twoJButton.setText( "2" );
61 controlJPanel.add( twoJButton );
62
63 // set up threeJButton
64 threeJButton = new JButton();
65 threeJButton.setBounds( 113, 56, 45, 45 );
66 threeJButton.setText( "3" );
67 controlJPanel.add( threeJButton );
68
69 // set up fourJButton
70 fourJButton = new JButton();
71 fourJButton.setBounds( 23, 101, 45, 45 );
72 fourJButton.setText( "4" );
73 controlJPanel.add( fourJButton );
74
75 // set up fiveJButton
76 fiveJButton = new JButton();
77 fiveJButton.setBounds( 68, 101, 45, 45 );
78 fiveJButton.setText( "5" );
79 controlJPanel.add( fiveJButton );
80
81 // set up sixJButton
82 sixJButton = new JButton();
83 sixJButton.setBounds( 113, 101, 45, 45 );
84 sixJButton.setText( "6" );
85 controlJPanel.add( sixJButton );
86
87 // set up sevenJButton
88 sevenJButton = new JButton();
89 sevenJButton.setBounds( 23, 146, 45, 45 );
90 sevenJButton.setText( "7" );
91 controlJPanel.add( sevenJButton );
92
93 // set up eightJButton
94 eightJButton = new JButton();
95 eightJButton.setBounds( 68, 146, 45, 45 );
96 eightJButton.setText( "8" );
97 controlJPanel.add( eightJButton );
98
99 // set up nineJButton
100 nineJButton = new JButton();
101 nineJButton.setBounds(113, 146, 45, 45 );
102 nineJButton.setText( "9" );
103 controlJPanel.add( nineJButton );
104
105 // set up zeroJButton
106 zeroJButton = new JButton();
107 zeroJButton.setBounds( 68, 191, 45, 45 );
108 zeroJButton.setText( "0" );
109 controlJPanel.add( zeroJButton );
110
111 // set up startJButton
112 startJButton = new JButton();
113 startJButton.setBounds( 13, 252, 74, 24 );
114 startJButton.setText( "Start" );
115 controlJPanel.add( startJButton );
116
117 // set up clearJButton
118 clearJButton = new JButton();
119 clearJButton.setBounds( 100, 252, 74, 24 );
120 clearJButton.setText( "Clear" );
121 controlJPanel.add( clearJButton );
122
123 // set properties of application’s window
124 setTitle( "Microwave" ); // set title bar text
125 setSize( 574, 346 ); // set window size
126 setVisible( true ); // display window
127
128 } // end method createUserInterface
129
130 // main method
131 public static void main( String args[] )
132 {
133 Microwave application = new Microwave();
134 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
135
136 } // end method main
137
138 } // end class Microwave
```

Computer Science & Information Technology

You might also like to view...

How can you change a spot color to a process color?

What will be an ideal response?

Computer Science & Information Technology

What are the three general concepts that are important as a starting place for understanding Active Directory?

What will be an ideal response?

Computer Science & Information Technology

This chapter refers to an article on magnetic storage in the December 2000 edition of IEEE Spectrum with the title “Magnetic Storage: The medium that wouldn’t die”. The title implies that magnetic storage should have become obsolete. Explain why many may have believed this, and why magnetic storage has continued to thrive.

What will be an ideal response?

Computer Science & Information Technology

Adding a(n) ____ to a description list element enables you to format a list without specifying the formatting for every item-description pair.

A. property B. style C. element D. class

Computer Science & Information Technology