import java.awt.BorderLayout;
import java.awt.Container;
import java.util.Date;
import javax.swing.JButton;
import javax.swing.JFrame;
class MyFrame extends JFrame {
JButton timeButton;
public MyFrame() {
setSize(400, 200);
setEnabled(true);
setTitle("时间表");
Container container = getContentPane();
timeButton = new JButton();
container.add(timeButton, BorderLayout.CENTER);
setVisible(true);
String timeStr;
String year;
String month;
String day;
String hour;
String minute;
String second;
while (true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Date date = new Date();
year = String.valueOf(date.getYear() + 1900);
month = String.valueOf(date.getMonth() + 1);
day = String.valueOf(date.getDate());
hour = String.valueOf(date.getHours());
minute = String.valueOf(date.getMinutes());
second = String.valueOf(date.getSeconds());
timeStr = "现在时刻是:" + year + "年" + month + "月" + day + "日" + " "
+ hour + "时" + minute + "分" + second + "秒";
timeButton.setText(timeStr);
}
}
}
public class TimeTest {
public static void main(String[] args) {
MyFrame myFrame = new MyFrame();
myFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}