import javax.swing.*
import java.awt.HeadlessException
import java.awt.BorderLayout
import java.awt.FlowLayout
import java.awt.Font
import java.awt.event.ActionListener
import java.awt.event.ActionEvent
public class Timer extends JFrame {
private static final long serialVersionUID = 1L
private static final String INITIAL_LABEL_TEXT = "00:00:00 000"
// 计数线程
private CountingThread thread = new CountingThread()
private long programStart = System.currentTimeMillis()
// 程序一开始就是暂停的
private long pauseStart = programStart
// 程序暂停的总时间
private long pauseCount = 0
private JLabel label = new JLabel(INITIAL_LABEL_TEXT)
private JButton startPauseButton = new JButton("开始")
private JButton resetButton = new JButton("清零")
private ActionListener startPauseButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
if (thread.stopped) {
pauseCount += (System.currentTimeMillis() - pauseStart)
thread.stopped = false
startPauseButton.setText("暂停")
} else {
pauseStart = System.currentTimeMillis()
thread.stopped = true
startPauseButton.setText("继续")
}
}
}
private ActionListener resetButtonListener = new ActionListener() {
public void actionPerformed(ActionEvent e) {
pauseStart = programStart
pauseCount = 0
thread.stopped = true
label.setText(INITIAL_LABEL_TEXT)
startPauseButton.setText("开始")
}
}
public Timer(String title) throws HeadlessException {
super(title)
setDefaultCloseOperation(EXIT_ON_CLOSE)
setLocation(300, 300)
setResizable(false)
setupBorder()
setupLabel()
setupButtonsPanel()
startPauseButton.addActionListener(startPauseButtonListener)
resetButton.addActionListener(resetButtonListener)
thread.start()// 计数线程一直就运行着
}
// 为窗体面板添加边框
private void setupBorder() {
JPanel contentPane = new JPanel(new BorderLayout())
contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5))
this.setContentPane(contentPane)
}
// 配置按钮
private void setupButtonsPanel() {
JPanel panel = new JPanel(new FlowLayout())
panel.add(startPauseButton)
panel.add(resetButton)
add(panel, BorderLayout.SOUTH)
}
// 配置标签
private void setupLabel() {
label.setHorizontalAlignment(SwingConstants.CENTER)
label.setFont(new Font(label.getFont().getName(), label.getFont().getStyle(), 40))
this.add(label, BorderLayout.CENTER)
}
// 程序入口
public static void main(String[] args) {
try {
UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName())
} catch (Exception e) {
e.printStackTrace()
}
Timer frame = new Timer("计时器")
frame.pack()
frame.setVisible(true)
}
private class CountingThread extends Thread {
public boolean stopped = true
private CountingThread() {
setDaemon(true)
}
@Override
public void run() {
while (true) {
if (!stopped) {
long elapsed = System.currentTimeMillis() - programStart - pauseCount
label.setText(format(elapsed))
}
try {
sleep(1) // 1毫秒更新一次显示
} catch (InterruptedException e) {
e.printStackTrace()
System.exit(1)
}
}
}
// 将毫秒数格式化
private String format(long elapsed) {
int hour, minute, second, milli
milli = (int) (elapsed % 1000)
elapsed = elapsed / 1000
second = (int) (elapsed % 60)
elapsed = elapsed / 60
minute = (int) (elapsed % 60)
elapsed = elapsed / 60
hour = (int) (elapsed % 60)
return String.format("%02d:%02d:%02d %03d", hour, minute, second, milli)
}
}
}
你可以试试,希望能帮到你!
简单的写了一个时间显示的程序 时间显示的格式 时:分:秒 毫秒
参考代码如下
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来做界面,.那么效果会比较漂亮, 我也写了一个效果如下图
import java.awt.*public class Clock {
private int hour
private int minute
private int second
public Clock(){
}
public Clock(int hrs,int min,int sec){
hour =hrs % 12
minute = min
second = sec
}
void show (Graphics g,int cx,int cy,int rad){
int hourLenght = (int)(rad * 0.5)//时针的长度
int minuteLenght = (int)(rad * 0.6)//分针的长度
int secondLenght = (int)(rad * 0.9)//秒针的长度
double angle//角度
//画出钟面
g.drawOval(cx-rad, cy - rad, rad * 2, rad * 2)
//画出时针
angle = (double)(hour*60*60 + minute*60 + second)/43200.0*2.0*Math.PI
drawNiddle(g,Color.blue, cx, cy, hourLenght,angle)
//画分针
angle = (double)(minute*60 + second)/3600 * 2.0 * Math.PI
drawNiddle(g,Color.blue, cx, cy, minuteLenght,angle)
// 画秒针
angle = (double)(second)/60*2.0*Math.PI
drawNiddle(g,Color.blue, cx, cy, secondLenght,angle)
}
private void drawNiddle(Graphics g ,Color c,int x,int y,int len,double angle){
int ex = (int)(x + len * Math.sin(angle))
int ey = (int)(y - len * Math.cos(angle))
g.setColor(c)
g.drawLine(x,y,ex,ey)
}
}
----------------ClockTest1.java---------------------
import javax.swing.*
import java.awt.*
import java.util.*
public class ClockTest1 extends JFrame{
/**
* @param args
*/
private Clock clock
private Date timeNow
public ClockTest1(){
super("时钟")
setSize(400,400)
setVisible(true)
}
public void paint(Graphics g){
super.paint(g)
timeNow = new Date()
clock = new Clock(timeNow.getHours(),timeNow.getMinutes(),timeNow.getSeconds())
clock.show(g,170,150,100)
try{
//for(int i =0 i <10 i += 10)
Thread.sleep(1000)
}catch(InterruptedException e){
}
repaint()
}
public static void main(String[] args) {
// TODO 自动生成方法存根
ClockTest1 appication = new ClockTest1()
appication.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
}
}