Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: alarm clock gui

  1. #1
    Junior Member
    Join Date
    May 2014
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default alarm clock gui

    hi guys
    i need some help ):
    the analog clock dese not move put it work when i run the program
    and the the buttoms dose not work i do not why
    how i can use the menu alarm to change the alarm time
    this my code run it and tell me if you could help me
    the main class
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import javax.swing.Timer;
     
     
     
    public class s201068380 extends JFrame implements ActionListener  {
     
     
     
       JFrame frame = new JFrame("Java Alarm Clock");
            JPanel left;
            JPanel right;
    		 JButton b1,b2;
    		 int s = 0;
    		 int os = 0;
     
            final Dimension frameDim = new Dimension(700,400);
     
     
    JMenu Clock = new JMenu("Clock");
    	JMenuItem Chdate = new JMenuItem("Change date");
    	JMenuItem Chtime= new JMenuItem("Change time ");
     
     
    JMenu Alarm = new JMenu("Alarm");
     
    	JMenuItem ChAlarmt = new JMenuItem("Change Alarm Time");
    	JMenuItem ChAlarms = new JMenuItem("Change Alarm Sound ");
     
     
    JMenu Help = new JMenu("Help");
    	JMenuItem Manual = new JMenuItem("Manual");
     
    JMenuBar bar = new JMenuBar();
     
     
     
     
    	public s201068380()
    	{
    		super("Java Alarm Clock");
    		    setSize(frameDim);
                setLocationRelativeTo(null);
                setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
     
                this.left = createLeftComponent();
                this.right = createRightComponent();
     
                addComponents();
     
    		setJMenuBar(bar);
     
    		Clock.add(Chdate);
    		Clock.add(Chtime);
    		bar.add(Clock);
     
     
    		Alarm.add(ChAlarmt);
    		Alarm.add(ChAlarms);
    		bar.add(Alarm);
     
     
     
    		Help.add(Manual);
    		bar.add(Help);
     
     
     
    		Chdate.addActionListener(this);
    		Chtime.addActionListener(this);
    		ChAlarmt.addActionListener(this);
    		ChAlarms.addActionListener(this);
    		Manual.addActionListener(this);
    		b1.addActionListener(this);
    		b2.addActionListener(this);
     
     
                setVisible(true);
    	}
    	 private JPanel createLeftComponent(){
                JPanel panel = new JPanel();
                panel.setLayout(new BorderLayout());
                panel.setPreferredSize(new Dimension(300, 0));
                panel.setBorder(BorderFactory.createMatteBorder(0, 0, 0, 1, Color.LIGHT_GRAY));
     
                JPanel upper = new JPanel(new BorderLayout());
                upper.setPreferredSize(new Dimension(0,250));
                upper.setBorder(BorderFactory.createMatteBorder(0, 0, 1, 0, Color.LIGHT_GRAY));
                Analogclock acl = new Analogclock();
               JPanel p1 = new JPanel();
    			upper.add(acl, BorderLayout.CENTER);
    	            upper.setVisible(true);
     
     
                JPanel lower = new JPanel(new BorderLayout());
                lower.setPreferredSize(new Dimension(0,100));
     
                 		Digitalclock dcl = new Digitalclock();
                 		DigitalclockAM dcla = new DigitalclockAM();
     
                if(s == 0){
     
    				lower.add(dcl, BorderLayout.SOUTH);
     
                }
    	     	 else{
     
    	       		lower.add(dcla, BorderLayout.SOUTH);
     
    	     	 }
    	  lower.setVisible(true);
     
                 panel.add(BorderLayout.PAGE_START, upper);
                 panel.add(BorderLayout.PAGE_END, lower);
     
                return panel;
            }
     
     
             private JPanel createRightComponent(){
                JPanel panel = new JPanel(new GridBagLayout());
     
                GridBagConstraints g = new GridBagConstraints();
     
                JLabel l1 = new JLabel("Date");
                JLabel l2 = new JLabel("Day");
                 JLabel l3 = new JLabel("Alarm");
     
     
     
                JTextField f1 = new JTextField();
                JTextField f2 = new JTextField();
                JTextField f3 = new JTextField();
     
                SimpleDateFormat date = new SimpleDateFormat("MMMMMMMMMMMM:dd:yyyy");
    			f1.setText(String.valueOf(date.format(new Date())));
     
    			 SimpleDateFormat day = new SimpleDateFormat("EEEEEEEEEEEE");
    			 f2.setText(String.valueOf(day.format(new Date())));
     
    			if (os == 0){
     
    				f3.setText("                              " + " ON");
    				  f3.setVisible(true);
    			}
    			else {
     
    				f3.setText("                              " + " OFF");
    				  f3.setVisible(true);
    			}
     
                f1.setPreferredSize(new Dimension(120,25));
                f2.setPreferredSize(new Dimension(120,25));
                f3.setPreferredSize(new Dimension(120,25));
     
     
                 b1 = new JButton("Turn Alarm Off");
                 b2 = new JButton("Change to AM/PM");
     
     
     
     
                g.insets = new Insets(5, 5, 5, 5);
     
                g.gridx = 0;
                g.gridy = 0;
                panel.add(l1, g);
     
                g.gridx = 1;
                g.gridy = 0;
                panel.add(f1, g);
     
                g.gridx = 0;
                g.gridy = 1;
                panel.add(l2, g);
     
                g.gridx = 1;
                g.gridy = 1;
                panel.add(f2, g);
     
                 g.gridx = 0;
                g.gridy = 2;
                panel.add(l3, g);
     
                g.gridx = 1;
                g.gridy = 2;
                panel.add(f3, g);
     
       			g.gridx = 0;
                g.gridy = 3;
                panel.add(b1, g);
     
              	g.gridx = 1;
                g.gridy = 3;
                panel.add(b2, g);
     
     
                return panel;
            }
             private void addComponents(){
                this.add(BorderLayout.LINE_START, left);
                this.add(BorderLayout.CENTER, right);
            }
     
    	  public void actionPerformed(ActionEvent ae){
    	  	if(ae.getSource() == b1){
    				os = (os+1)%2;
    	  		}
     
    	  		 if(ae.getSource() == b2){
    				s = (s+1)%2;
    	  }
     
    	 if(ae.getSource() == Manual){
     
    	 	}
    	  }
     
    	        public static void main(String[] args) {
            new s201068380().setVisible(true);
     
        }
    }
    the subclasses
     
     
    import java.awt.Graphics;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.Locale;
    import java.awt.event.*;
    import java.awt.*;
    import javax.swing.*;
    public class Analogclock extends JPanel implements Runnable
    {
    Thread thread = null;
    SimpleDateFormat date = new SimpleDateFormat("s", Locale.getDefault());
    Date currentDate;
    int xcenter = 140, ycenter = 140, lastxm = 0, lastym = 0, lastxh = 0,lastyh = 0;
    private void drawCircle(Graphics g){
     
    g.setColor(Color.white);
    g.fillOval(60, 55, 170, 170);
    g.setColor(Color.BLACK);
    g.drawOval(60, 55, 170, 170);
     
     
    }
    public void paint(Graphics g) {
    int xhour, yhour, xminute, yminute, minute, hour;
    drawCircle(g);
    currentDate = new Date();
    date.applyPattern("m");
    minute = Integer.parseInt(date.format(currentDate));
    date.applyPattern("h");
    hour = Integer.parseInt(date.format(currentDate));
     
     xminute =  (int)(Math.cos(minute * 3.14f / 30 - 3.14f / 2) * 80 + xcenter);
     yminute =  (int)(Math.sin(minute * 3.14f / 30 - 3.14f / 2) * 80 + ycenter);
     xhour =  (int)(Math.cos((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 60 + xcenter );
     yhour =  (int)(Math.sin((hour * 30 + minute / 2) * 3.14f / 180 - 3.14f / 2) * 60 + ycenter);
     
     
     
    if (xminute != lastxm || yminute != lastym)
    {
    g.drawLine(xcenter, ycenter - 1, lastxm , lastym);
    g.drawLine(xcenter - 1, ycenter, lastxm, lastym);
    }
    if (xhour != lastxh || yhour != lastyh)
    {
    g.drawLine(xcenter, ycenter - 1, lastxh, lastyh);
    g.drawLine(xcenter - 1, ycenter, lastxh, lastyh);
    }
     
    g.setColor(Color.black);
    g.drawLine(xcenter, ycenter - 1, xminute  , yminute  );
    g.drawLine(xcenter - 1, ycenter, xminute   , yminute  );
    g.setColor(Color.black);
    g.drawLine(xcenter, ycenter - 1, xhour , yhour  );
    g.drawLine(xcenter - 1, ycenter, xhour , yhour);
     
    lastxm = xminute;
    lastym = yminute;
    lastxh = xhour;
    lastyh = yhour;
    }
     
     
      public void start() {
        if (thread == null) {
          thread = new Thread(this);
          thread.start();
        }
      }
      public void stop()
    {
     
     
    thread = null;
    }
     
      public void run() {
         while (thread != null) {
          try {
            Thread.sleep(100);
          } catch (InterruptedException e) {
          }
          repaint();
        }
        thread = null;
      }
     
      public void update(Graphics g) {
        paint(g);
      }
    }

    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class Digitalclock extends JPanel{
     
     
     JLabel label;
     SimpleDateFormat f = new SimpleDateFormat("HH:mm:ss");
     SimpleDateFormat fam = new SimpleDateFormat("hh:mm:ss a");
    public int s = 0 ;
     
    public Digitalclock() {
    setSize(75,150);
    label = new JLabel();
     
    this.add(label);
     
    new Thread(){
     
     
    public void run(){
    while (true) {
    	if (s == 0)
    label.setText(String.valueOf(f.format(new Date())));
    else
    label.setText(String.valueOf(fam.format(new Date())));
    try {
    Thread.sleep(1000);
    } catch (Exception e) {
     
    }
    }
    }
    }.start();
        }
      }

    import java.text.SimpleDateFormat;
    import java.util.Date;
     
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
     
    public class DigitalclockAM extends JPanel{
     
     
     JLabel label;
     SimpleDateFormat fam = new SimpleDateFormat("hh:mm:ss a");
     
     
    public DigitalclockAM() {
    setSize(75,150);
    label = new JLabel();
     
    this.add(label);
     
    new Thread(){
     
     
    public void run(){
    while (true) {
     
    label.setText(String.valueOf(fam.format(new Date())));
     
    try {
    Thread.sleep(1000);
    } catch (Exception e) {
     
    }
    }
    }
    }.start();
        }
      }


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: alarm clock gui

    Welcome to the forum! Thanks for taking the time to learn how to post code correctly. If you haven't already, please read this topic to learn other useful info for new members.

    How can someone write so much code without a single comment? Then when that person has a mass of uncommented code, how can that person possibly expect anyone else to understand it and help them fix it?

    Comment your code. Describe how the parts work by themselves and work together. Just doing that should help you understand your code better and may help you find errors in the design.

  3. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: alarm clock gui

    Another problem is that the posted code has lost most of the indentations of nested statements.
    That also makes the code hard to read and understand.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Alarm Clock (How to figure out how much time left till alarm will sound)
    By MrMario in forum What's Wrong With My Code?
    Replies: 3
    Last Post: September 8th, 2013, 12:58 AM
  2. Analogue Clock Problem
    By gargamel7 in forum What's Wrong With My Code?
    Replies: 48
    Last Post: September 2nd, 2011, 01:21 PM
  3. Replies: 6
    Last Post: January 28th, 2011, 01:13 AM
  4. Making a clock
    By javapenguin in forum What's Wrong With My Code?
    Replies: 5
    Last Post: January 7th, 2011, 04:36 PM
  5. help with clock class
    By collinsislee in forum Object Oriented Programming
    Replies: 1
    Last Post: February 24th, 2010, 02:53 PM