I'm trying to make a digital clock in Java. The program is at the end going to be an alarmclock. So far I've made 3 classes: Main, Clock and Time. Main only creates an object of Clock. Time handles the javax.swing.Timer function for the clock. And the Clock creates a GUI window that displays the time.
But! I can't figure out how to make it repaint the time for each passing second when I pass the time form the Time class to the Clock class! What am I doing wrong?
Since I'm going to expand the program at some point I don't want the GUI part in the same class as the Time.
Clock class:
import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTabbedPane;
import javax.swing.JPanel;
import javax.swing.JButton;
public class Clock extends JFrame implements ActionListener{
public Time time;
JLabel timeL = new JLabel();
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
public Clock()
{
super();
JTabbedPane tabbedPane = new JTabbedPane();
JPanel front = new JPanel();
front.setLayout(new BorderLayout());
front.add((timeL), BorderLayout.NORTH);
front.add(new JLabel("Something"), BorderLayout.WEST);
front.add(new JLabel("Something else"), BorderLayout.CENTER);
front.add(new JLabel("Yep!"), BorderLayout.EAST);
tabbedPane.addTab("Front", null, front, "First");
JPanel back = new JPanel();
back.add(new JButton("Test 1"), BorderLayout.NORTH);
back.add(new JButton("Test 2"), BorderLayout.SOUTH);
tabbedPane.addTab("Back", null, back, "Second");
add(tabbedPane);
timeL.setText(timeFormat.format(new Date(System.currentTimeMillis())));
timeL.setFont(new Font("Arial", Font.PLAIN,72));
this.pack();
} // end constructor
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(time.getTimeL()))
{
time.getTimeL().setText(timeFormat.format(new Date(System.currentTimeMillis())));
} // end if
} // end method
} // end class
Time class
import java.text.SimpleDateFormat;
import java.util.Date;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.Timer;
import javax.swing.JLabel;
public class Time implements ActionListener {
SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm:ss");
Timer timer;
JLabel timeL = new JLabel();
public Time()
{
timer = new Timer(500, this);
timer.setRepeats(true);
timer.start();
} // end constructor
public void actionPerformed(ActionEvent e)
{
if(e.getSource().equals(timer))
{
timeL.setText(timeFormat.format(new Date(System.currentTimeMillis())));
} // end if
} // end method
public JLabel getTimeL() {
return timeL;
}
} // end class
Oh! And this is NOT a school assignment. I'm just comming up with things to practice on before the exams in May. But I know classes, GUI and javax.swing may be vital on this exam so I'm trying to combine them in one program.
(The program is running without errors so if there are any typos here it's because I've translated stuff from my native toung to english.)