1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
| import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.text.*;
import javax.swing.*;
public class Test {
/** Self-test main.
* @param args Arguments from the command-line.
*/
public static void main(String... args) {
// Start at EDT.
SwingUtilities.invokeLater(new Runnable() {
/* {@inheritDoc}
*/
public void run() {
//final Calendar calendar = Calendar.getInstance();
final DateFormat format = DateFormat.getTimeInstance(DateFormat.LONG);
// Initialize and configure timer.
final javax.swing.Timer timer = new javax.swing.Timer(1000, new ActionListener() {
/* {@inheritDoc}
*/
public void actionPerformed(ActionEvent event) {
long time = System.currentTimeMillis();
//calendar.setTimeInMillis(time);
//Date date = calendar.getTime();
// In case of error, use commented lines instead.
Date date = new Date(time);
String out = format.format(date);
System.out.println(out);
}
});
// Initialize and configure button.
final JToggleButton button = new JToggleButton("Start");
button.addActionListener(new ActionListener() {
/* {@inheritDoc}
*/
public void actionPerformed(ActionEvent event) {
if (button.isSelected()) {
button.setText("Stop");
timer.restart();
}
else {
button.setText("Start");
timer.stop();
}
}
});
// Initialize and show frame.
JFrame frame = new JFrame("Test");
frame.add(button);
frame.setSize(500, 400);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setVisible(true);
}
});
}
} |