gvsu/cs621/proj1/ClockView.java
josh 2d610b1c83 cs621: proj1: Clock modified to 12-hour format, show AM/PM
git-svn-id: svn://anubis/gvsu@9 45c1a28c-8058-47b2-ae61-ca45b979098e
2008-02-03 04:41:25 +00:00

55 lines
1.4 KiB
Java

import javax.swing.*;
/**
* This class provides a graphical view of the current time
*/
public class ClockView extends JFrame
{
private JLabel tLabel = new JLabel();
/**
* Constructor method to make a ClockView object.
*/
public ClockView()
{
super("Clock");
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
setSize(100, 70);
tLabel.setHorizontalAlignment(SwingConstants.CENTER);
getContentPane().add(tLabel);
refreshTimeDisplay();
}
/**
* Helper method to convert an integer to a 0-padded string.
* @param i The integer to convert
* @return A string representing the integer, padded on the
* left by a '0' character if the integer was less than 10.
*/
protected String getDigitsAsString(int i)
{
String str = Integer.toString(i);
if (i < 10)
str = "0" + str;
return str;
}
/**
* A method to update the time displayed in the ClockView window.
*/
public void refreshTimeDisplay()
{
Timestamp t = new Timestamp();
t.fillTimes();
String display = getDigitsAsString(t.hrs) + ":" +
getDigitsAsString(t.mins) + ":" +
getDigitsAsString(t.secs) + " " +
t.am_pm;
tLabel.setText(" " + display);
tLabel.repaint();
}
}