當前位置:編程學習大全網 - 源碼下載 - java 這是壹個秒表,我想把顯示的時間變成系統時間要怎麽做,具體操作

java 這是壹個秒表,我想把顯示的時間變成系統時間要怎麽做,具體操作

簡單的寫了壹個時間顯示的程序 時間顯示的格式 ?時:分:秒 毫秒

參考代碼如下

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來做界面,.那麽效果會比較漂亮, 我也寫了壹個效果如下圖

  • 上一篇:音樂超人的獎項
  • 下一篇:微信上有哪些貸款
  • copyright 2024編程學習大全網