取得時間用
java.util.Calendar或者java.util.Date
在控制元件上控制時間用
javax.swing.Timer
下面給個例子:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class ClockTest extends JFrame {
public ClockTest() {
super("Timer Demo");
setSize(300, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ClockLabel clock = new ClockLabel();
getContentPane().add(clock, BorderLayout.NORTH);
}
public static void main(String args[]) {
ClockTest ct = new ClockTest();
ct.setVisible(true);
class ClockLabel extends JLabel implements ActionListener {
public ClockLabel() {
super((Calendar.getInstance().getTime()).toString());
Timer t = new Timer(1000, this);
t.start();
public void actionPerformed(ActionEvent ae) {
setText(Calendar.getInstance().getTime().toString());
取得時間用
java.util.Calendar或者java.util.Date
在控制元件上控制時間用
javax.swing.Timer
下面給個例子:
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Calendar;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;
public class ClockTest extends JFrame {
public ClockTest() {
super("Timer Demo");
setSize(300, 100);
setDefaultCloseOperation(EXIT_ON_CLOSE);
ClockLabel clock = new ClockLabel();
getContentPane().add(clock, BorderLayout.NORTH);
}
public static void main(String args[]) {
ClockTest ct = new ClockTest();
ct.setVisible(true);
}
}
class ClockLabel extends JLabel implements ActionListener {
public ClockLabel() {
super((Calendar.getInstance().getTime()).toString());
Timer t = new Timer(1000, this);
t.start();
}
public void actionPerformed(ActionEvent ae) {
setText(Calendar.getInstance().getTime().toString());
}
}