簡單的寫了壹個時間顯示的程序 時間顯示的格式 ?時:分:秒 毫秒
參考代碼如下
import java.awt.*;
import java.awt.event.*;
import java.text.SimpleDateFormat;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.Timer;//註意導入的是javax.swing.Timer; 有壹些類似的包不要導錯了
public class TimeTest extends JFrame {
private JLabel jlTime;
SimpleDateFormat sd = new SimpleDateFormat("hh:mm:ss SSS");//時間格式化; 樣式為 時:分:秒 毫秒
public TimeTest() {
jlTime = new JLabel("",JLabel.CENTER);
jlTime.setForeground(Color.BLUE);
jlTime.setFont(new Font(Font.MONOSPACED, Font.BOLD, 25));
add(jlTime);
setTitle("雪飛瀟瀟 ?java時間Demo");
setSize(360, 160);
setLocationRelativeTo(null);
setDefaultCloseOperation(EXIT_ON_CLOSE);
//每隔10毫秒, 更新壹次文本標簽上的文字
Timer timer = new Timer(10, new ActionListener() {
public void actionPerformed(ActionEvent e) {
long now = System.currentTimeMillis();//獲取當前系統毫秒數
String textTime=sd.format(now);//轉換成規定的格式
jlTime.setText(textTime);//設置文本
}
});
timer.start();//啟動timer更新時間
}
public static void main(String[] args) {
new TimeTest().setVisible(true);
}
}
----------------------分割線------------------------
當然了swing寫的界面,往往比較簡陋.如果選擇了javaFX來做界面,.那麽效果會比較漂亮, 我也寫了壹個效果如下圖