(Alarm Clock GUI), JButtons have some properties in common with JLabels. In this exercise, you will customize the properties of the AM and PM JRadioButton components. In addition, you will customize the JPanel that displays the time by setting its background property to black and setting its foreground property to white (by using setForeground). When you complete your modifications, the Alarm Clock GUI will appear as in Fig. 2.37.
a) Copying the template to your working directory. Copy the C:Examples Tutorial02ExercisesAlarmClock 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:SimplyJavaAlarmClock, then pressing Enter.
c) Compiling the template application. Compile your application by typing the com- mand javac AlarmClock.java, then pressing Enter.
d) Running the template application. Run the application by typing java AlarmClock.
The GUI of the Alarm Clock template application should appear as shown in Fig. 2.38.
e) Closing the application. Close the running application by clicking its close button.
This returns you to the Co
```
1
2 // Create GUI for an alarm clock
3 import javax.swing.*;
4 import java.awt.*;
5 import java.awt.event.*;
6 import javax.swing.border.*;
7
8 public class AlarmClock extends JFrame
9 {
10 private JButton hourJButton, minuteJButton, secondJButton,
11 onJButton, offJButton, timerJButton;
12 private JPanel ampmJPanel;
13 private JRadioButton amJRadioButton, pmJRadioButton;
14 private JLabel timeJLabel, snoozeJLabel;
15
16 // no-argument constructor
17 public AlarmClock()
18 {
19 createUserInterface();
20 }
21
22 // create and position GUI components; register event handlers
23 private void createUserInterface()
24 {
25 // get content pane and set layout to null
26 Container contentPane = getContentPane();
27 contentPane.setLayout( null );
28
29 // set up hourJButton
30 hourJButton = new JButton();
31 hourJButton.setText( "HOUR" );
32 hourJButton.setBounds( 0, 0, 84, 46 );
33 contentPane.add( hourJButton );
34
35 // set up minuteJButton
36 minuteJButton = new JButton();
37 minuteJButton.setBounds( 84, 0, 84, 46 );
38 minuteJButton.setText( "MINUTE" );
39 contentPane.add( minuteJButton );
40
41 // set up secondJButton
42 secondJButton = new JButton();
43 secondJButton.setBounds( 168, 0, 84, 46 );
44 secondJButton.setText( "SECOND" );
45 contentPane.add( secondJButton );
46
47 //set up onJButton
48 onJButton = new JButton();
49 onJButton.setBounds( 252, 0, 56, 46 );
50 onJButton.setText( "ON" );
51 contentPane.add( onJButton );
52
53 // set up offJButton
54 offJButton = new JButton();
55 offJButton.setBounds( 308, 0, 56, 46 );
56 offJButton.setText( "OFF" );
57 contentPane.add( offJButton );
58
59 // set up timeJLabel
60 timeJLabel = new JLabel();
61
62
63
64
65 timeJLabel.setOpaque( true );
66 timeJLabel.setHorizontalAlignment( JLabel.CENTER );
67 contentPane.add( timeJLabel );
68
69 // set up the ampm JLabel
70 ampmJPanel = new JPanel();
71 ampmJPanel.setLayout( null );
72 ampmJPanel.setBorder( new TitledBorder(
73 new EtchedBorder( EtchedBorder.LOWERED ), "AM/PM" ) );
74 ampmJPanel.setBounds( 132, 56, 90, 80 );
75 contentPane.add( ampmJPanel );
76
77 // set up amJRadioButton
78 amJRadioButton = new JRadioButton();
79 amJRadioButton.setText( "AM" );
80 amJRadioButton.setBounds( 20, 18, 50, 30 );
81 ampmJPanel.add( amJRadioButton );
82
83 // set up pmJRadioButton
84 pmJRadioButton = new JRadioButton();
85 pmJRadioButton.setText( "PM" );
86 pmJRadioButton.setBounds( 20, 40, 50, 30 );
87 ampmJPanel.add( pmJRadioButton );
88
89 // set up timerJButton
90 timerJButton = new JButton();
91 timerJButton.setBounds( 256, 76, 80, 50 );
92 timerJButton.setText( "Timer" );
93 contentPane.add( timerJButton );
94
95 // set up snoozeJLabel
96 snoozeJLabel = new JLabel( "SNOOZE" );
97 snoozeJLabel.setBounds( 0, 150, 364, 46 );
98 snoozeJLabel.setText( "SNOOZE" );
99 snoozeJLabel.setBorder( new LineBorder( Color.BLACK ) );
100 snoozeJLabel.setHorizontalAlignment( JLabel.CENTER );
101 contentPane.add( snoozeJLabel );
102
103 // set properties of application’s window
104 setTitle( "Alarm Clock" ); // set title bar text
105 setSize( 374, 226 ); // set window size
106 setVisible( true ); // display window
107
108 } // end method createIserInterface
109
110 // main method
111 public static void main( String args[] )
112 {
113 AlarmClock application = new AlarmClock();
114 application.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
115
116 } // end method main
117
118 } // end class AlarmClock
```
You might also like to view...
The ________ is a temporary holding area in the computer's memory; it is only temporary storage
Fill in the blank(s) with correct word
Unlike ______, which is a social network open to business professionals, _____ is a social networking site restricted to credentialed physicians.
A. ?Facebook/Yammer B. ?LinkedIn/Sermo C. ?Classmates/GoogleAdvantage D. ?Safari/Titter