```
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
```