-
April 20th, 2002, 03:04 AM
#1
Java Clock
Hey everyone,
I'm just posting this clock/timer applet I created. It's nothing special, just a timer - I find it very usefull when I listen to the footy on the radio. If any Aussies here follow the AFL, hopefully you'll know what I mean. Well anyways, if you are learning java, I'm hoping this will be a good example for you. I find that example applets/applications are really good to learn from - you can't just read text books...too boring.
Well, I'll post the source code here and hopefully my poor documentation will explain most of the things.
(Note: It's java, even though I put it in PHP quotes - it just looked the best in them - I tried 'code' quotes, but it was all over the place.)
PHP Code:
//import the packages that will be used
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
//define the class Clock
public class Clock extends JApplet implements ActionListener
{
//define all instance variables
private Container pane;
private JPanel center, south;
private JLabel hours, minutes, seconds;
private JTextField hourValue, minuteValue, secondValue;
private JButton startButton, stopButton;
private String theHour, theMinute, theSecond;
private Timer theTimer;
//define the init method, which basically outlines what the applet looks like
public void init()
{
//define the pane where everything will be added
pane = getContentPane();
//define what layout the pane will use
pane.setLayout(new BorderLayout());
center = new JPanel();
center.setLayout(new FlowLayout());
pane.add(center, "Center");
south = new JPanel();
south.setLayout(new FlowLayout());
pane.add(south, "South");
//define the labels
hours = new JLabel("Hours");
minutes = new JLabel("Minutes");
seconds = new JLabel("Seconds");
//define the text fields - the actual timer
hourValue = new JTextField("0", 3);
minuteValue = new JTextField("0", 3);
secondValue = new JTextField("0", 3);
//define the buttons to start and stop the timer
startButton = new JButton("Start Timer");
stopButton = new JButton("Stop Timer");
//add the buttons to the action listener - so they fire an event when clicked
startButton.addActionListener(this);
stopButton.addActionListener(this);
//add the buttons to the pane
south.add(startButton);
south.add(stopButton);
//add all other objects to the pane
center.add(hours);
center.add(hourValue);
center.add(minutes);
center.add(minuteValue);
center.add(seconds);
center.add(secondValue);
//create a timer that fires an event every second (1000 milliseconds)
theTimer = new Timer(1000, this);
}
//defines the method which is called when an event is fired
public void actionPerformed(ActionEvent e)
{
//capture the source which fired the event
Object event = e.getSource();
//define what to do when the start button is pushed
if(event == startButton)
{
theTimer.start();
}
//define what to do when the stop button is pushed
else if(event == stopButton)
{
theTimer.stop();
}
//define what to do when the timer fires the event (every 1000 milliseconds in this case)
else if(event == theTimer)
{
addSecondValue();
}
}
//define the method that increments the 'hours' section
public void addHourValue()
{
theHour = hourValue.getText();
int theHourValue = (Integer.parseInt(theHour)) + 1;
hourValue.setText(Integer.toString(theHourValue));
}
//define the method that increments the 'minutes' section
public void addMinuteValue()
{
theMinute = minuteValue.getText();
String incrementedValue = increment(theMinute);
minuteValue.setText(incrementedValue);
if(incrementedValue.equals("0"))
addHourValue();
}
//define the method that increments the 'seconds' section
public void addSecondValue()
{
theSecond = secondValue.getText();
String incrementedValue = increment(theSecond);
secondValue.setText(incrementedValue);
if(incrementedValue.equals("0"))
addMinuteValue();
}
//define the method that increments the seconds and minutes, reseting the value to '0' every 60 occurances
public String increment(String value)
{
int theValue = Integer.parseInt(value);
if(theValue <= 58)
theValue++;
else
theValue = 0;
return Integer.toString(theValue);
}
}
And here is the ZIP file that contains the HTML file and the CLASS file so that you can view it (the JAVA file is there aswell - the source code). Just open it in any java-enabled browser and it should work fine. Any questions just reply and I'll be happy to help.
EDIT: OK, so the ZIP file is attached in my post below (2 posts down).
Cheers,
Greg
\"Do you know what people are most afraid of?
What they don\'t understand.
When we don\'t understand, we turn to our assumptions.\"
-- William Forrester
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
|