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
the subclassesimport 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); } }
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(); } }