troubleshooting Question

Alarm Clock

Avatar of Mike Eghtebas
Mike EghtebasFlag for United States of America asked on
Java
7 Comments1 Solution562 ViewsLast Modified:
To build an alarm clock, the attached code shows current hour/minute/second. Need help to:

1. Set alarm h/m/s and store it in an array and close set alarm screen.
    (display current time was on the alarm clock)

2. If alarm check box is true, then beep 10 times.  

Thank you.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import javax.swing.*;
import javax.swing.Timer;
import java.util.*;
import javax.swing.BorderFactory;
import javax.swing.border.Border;

public class AlarmClock extends JApplet implements ActionListener{

    private JCheckBox chkAlarm = new JCheckBox("Alarm");
    private JButton btnSetAlarm = new JButton("Set Alarm");
    private JLabel lblHour = new JLabel("Hour");
    private JLabel lblMinute = new JLabel("Minute");
    private JLabel lblSecond = new JLabel("Second");
    private JLabel lblHourDisplay = new JLabel("9");
    private JLabel lblMinuteDisplay = new JLabel("24");
    private JLabel lblSecondDisplay = new JLabel("49");
    private AudioClip audioClip;
    Font f1 = new Font("Arial", 1, 36);
    Font f2 = new Font("Arial", 1, 12);
    private Timer timer = new Timer(0, new TimerListener());
    Border border = BorderFactory.createLineBorder(Color.GRAY);
    
    public void init() {

        setCurrentTime();
        chkAlarm.addActionListener(this);
        btnSetAlarm.addActionListener(this);

        JPanel pane1 = new JPanel(new GridLayout(0, 3, 0, 0));
        lblHour.setFont(f2); lblMinute.setFont(f2); lblSecond.setFont(f2);
        lblHour.setHorizontalAlignment(0); lblMinute.setHorizontalAlignment(0); lblSecond.setHorizontalAlignment(0);
        pane1.add(lblHour);
        pane1.add(lblMinute);
        pane1.add(lblSecond);
        add(pane1, BorderLayout.NORTH);
        
        JPanel pane2 = new JPanel(new GridLayout(0, 3, 5, 0));
        lblHourDisplay.setFont(f1); lblMinuteDisplay.setFont(f1); lblSecondDisplay.setFont(f1);
        lblHourDisplay.setHorizontalAlignment(0); lblMinuteDisplay.setHorizontalAlignment(0); lblSecondDisplay.setHorizontalAlignment(0);
        lblHourDisplay.setBackground(Color.white); lblMinuteDisplay.setBackground(Color.white); lblSecondDisplay.setBackground(Color.white);
        //lblHourDisplay.setBorder(border); lblMinuteDisplay.setBorder(border); lblSecondDisplay.setBorder(border);
        lblHourDisplay.setOpaque(true); lblMinuteDisplay.setOpaque(true); lblSecondDisplay.setOpaque(true);
        
        pane2.add(lblHourDisplay); pane2.add(lblMinuteDisplay); pane2.add(lblSecondDisplay);
        add(pane2, BorderLayout.CENTER);
        
        JPanel pane3 = new JPanel();
        pane3.add(chkAlarm);
        pane3.add(btnSetAlarm);
        add(pane3, BorderLayout.SOUTH);
        timer.start();
    }

    public void setCurrentTime() {
        
          Calendar calendar = new GregorianCalendar();
          String am_pm;
          int hour = calendar.get(Calendar.HOUR);
          int minute = calendar.get(Calendar.MINUTE);
          int second = calendar.get(Calendar.SECOND);
          if(calendar.get(Calendar.AM_PM) != 0)
              hour +=12;
          
          lblHourDisplay.setText("" + hour);
          lblMinuteDisplay.setText("" + minute);
          lblSecondDisplay.setText("" + second);
    }
    
    private class TimerListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
//          System.out.print("llkk");
          setCurrentTime();
//          repaint();
        }
    } 
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        
        if (source == btnSetAlarm) {
            System.out.println("test ok btnSetAlarm");
            AlarmClockReset restAlarm = new AlarmClockReset();
            restAlarm.setTitle("Set Alarm");
            restAlarm.setSize(200, 160);
            restAlarm.setLocationRelativeTo(null);
//            restAlarm.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            restAlarm.setVisible(true);
        
        }else if (source == chkAlarm) {
            System.out.println("test ok chkAlarm");
        }
    }
    
    public static void main(String[] args) {
        
        JFrame frame = new JFrame();
        frame.setTitle("Exercise 18.22: Alarm Clock");
        frame.setSize(300, 160);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	AlarmClock tp = new AlarmClock();
	tp.init();
	frame.getContentPane().add(tp);
        frame.setVisible(true);
        
       
    }
}

class AlarmClockReset extends JFrame implements ActionListener{
   
    private JButton btnOK;
    private JButton btnCancel;
    private JLabel lblInstruction;
    private JLabel lblHour;
    private JLabel lblMinute;
    private JLabel lblSecond;
    private JTextField txtHour;
    private JTextField txtMinute;
    private JTextField txtSecond;

    public AlarmClockReset() {
        
        btnOK = new JButton("OK");
        btnCancel = new JButton("Cancel");
        lblInstruction = new JLabel("Enter Hour, Minute, and Second");
        lblHour = new JLabel("Hour");
        lblMinute = new JLabel("Minute");
        lblSecond = new JLabel("Second");
        txtHour = new JTextField(2);
        txtMinute = new JTextField(2);
        txtSecond = new JTextField(2);

        btnOK.addActionListener(this);
        btnCancel.addActionListener(this);

        JPanel p1 = new JPanel(new BorderLayout());

        p1.add(lblInstruction, BorderLayout.NORTH);
        JPanel p2 = new JPanel(new GridLayout(4, 0));
        p2.add(lblHour); p2.add(txtHour);
        p2.add(lblMinute); p2.add(txtMinute);
        p2.add(lblSecond); p2.add(txtSecond);
        p2.add(btnOK); p2.add(btnCancel);

        Container c1 =  this.getContentPane();
        c1.setLayout(new BorderLayout());
        c1.add(p1, BorderLayout.NORTH);
        c1.add(p2, BorderLayout.CENTER);
    }    
    
    public void actionPerformed(ActionEvent e) 
    {
        Object source = e.getSource();
        if (source.equals(btnOK)) {
                System.out.println("test ok btnOK");
        }else if (source == btnCancel) {
            System.out.println("test ok btnCancel");
        }
    }
}
ASKER CERTIFIED SOLUTION
Join our community to see this answer!
Unlock 1 Answer and 7 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 7 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros