import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
///////////////////////////////////////////////////////////// TextClock2
public class TextClock2 extends JApplet {
//====================================================== constructor
public TextClock2() {
add(new Clock());
}
//============================================================= main
public static void main(String[] args) {
JFrame window = new JFrame("Time of Day");
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setContentPane(new Clock());
window.pack();
window.setVisible(true);
System.out.println(window.getContentPane().size());
}
}
----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.util.Calendar;
///////////////////////////////////////////////////////////// Clock
class Clock extends JTextField {
javax.swing.Timer m_t;
//================================================== constructor
public Clock() {
//... Set some attributes.
setColumns(6);
setFont(new Font("sansserif", Font.PLAIN, 48));
//... Create a 1-second timer.
m_t = new javax.swing.Timer(1000, new ClockTickAction());
m_t.start(); // Start the timer
}
/////////////////////////////////////////// inner class listener
private class ClockTickAction implements ActionListener {
public void actionPerformed(ActionEvent e) {
//... Get the current time.
Calendar now = Calendar.getInstance();
int h = now.get(Calendar.HOUR_OF_DAY);
int m = now.get(Calendar.MINUTE);
int s = now.get(Calendar.SECOND);
setText("" + h + ":" + m + ":" + s);
}
}
}
No comments:
Post a Comment