JAVA怎么在JAVA Swing 界面上显示动态的当前时间~~~~

只要动态的显示当前时间 如12:56:50

第1个回答  2013-09-28
package com.exam.student.reserve;

import java.awt.*;
import javax.swing.*;
import java.util.*;
public class Time extends JFrame
{
JPanel pnlmain;
static JLabel lblmove;
JButton bntcontrol;
currenttime ct;
public Time()
{

pnlmain=new JPanel();
this.setContentPane(pnlmain);
lblmove=new JLabel("你好");
lblmove.setFont(new Font("宋体",Font.BOLD,22));
lblmove.setForeground(Color.RED);
pnlmain.add(lblmove);
ct=new currenttime();
ct.start();
setTitle("线程");
setSize(250,150);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setResizable(false);
}

public static void main(String args[])
{
new Time();
}

}
class currenttime extends Thread
{
Date datedisplay;
GregorianCalendar gccalendar;
String strtime;
public currenttime(){}
public void run()
{
while(true)
{
displaytime();
try
{
this.sleep(1000);
}
catch(InterruptedException e)
{
JOptionPane.showMessageDialog(null,"线程中断");
}
}
}
public void displaytime()
{
datedisplay=new Date();
gccalendar=new GregorianCalendar();
gccalendar.setTime(datedisplay);
strtime="当前时间:"+gccalendar.get(Calendar.DATE)+":"+gccalendar.get(Calendar.HOUR)+":"+gccalendar.get(Calendar.MINUTE)+":"+gccalendar.get(Calendar.SECOND);
Time.lblmove.setText(strtime);
}
}
相似回答