// InputPanel.java -- The bottom of the GUI shows a place where the user can // type in the desired wakeup time. import javax.swing.*; // for JPanel import java.awt.event.*; // for action listener public class InputPanel extends JPanel { // I'm passing the Clock panel as a parameter so that we will have a // way to communicate the alarm time to it. public InputPanel(final ClockPanel clockPanel) { JLabel label = new JLabel ("Set the alarm clock here: "); add(label); // Compiler wants these text fields declared 'final' because they are // accessed in inner class. We saw this in earlier examples too. final JTextField hourField = new JTextField(2); final JTextField minuteField = new JTextField(2); final JTextField secondField = new JTextField(2); JLabel hourLabel = new JLabel("Hour"); JLabel minuteLabel = new JLabel("Minute"); JLabel secondLabel = new JLabel("Second"); JButton setButton = new JButton ("Set"); // With the button, we should have a listener. ActionListener setButtonListener = new ActionListener() { public void actionPerformed(ActionEvent e) { int alarmHour = Integer.parseInt(hourField.getText()); int alarmMinute = Integer.parseInt(minuteField.getText()); int alarmSecond = Integer.parseInt(secondField.getText()); clockPanel.setAlarmTime(alarmHour, alarmMinute, alarmSecond); } }; setButton.addActionListener(setButtonListener); add(hourLabel); add(hourField); add(minuteLabel); add(minuteField); add(secondLabel); add(secondField); add(setButton); } }