55 lines
1.4 KiB
Java
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();
|
|
}
|
|
}
|
|
|
|
|