欢迎访问悦橙教程(wld5.com),关注java教程。悦橙教程  java问答|  每日更新
页面导航 : > > 文章正文

标签动态显示时间,标签动态显示,标签动态显示时间JTim

来源: javaer 分享于  点击 6143 次 点评:131

标签动态显示时间,标签动态显示,标签动态显示时间JTim


标签动态显示时间

JTimerLabel.java

import java.util.*;import javax.swing.*;class JTimerLabel extends JLabel implements Runnable {    private static final long serialVersionUID = 1L;    private Date d = null;    private String caption = "";    private Thread th = new Thread(this);    public JTimerLabel(String caption) {        this.caption = caption;    }    public void start() {        th.start();    }    public void run() {        while (true) {            d = new Date();            this.setText(caption + d.getHours() + ":" + d.getMinutes() + ":"                    + d.getSeconds());            try {                Thread.sleep(1000);            } catch (InterruptedException e) {            }        }    }}

[Java]代码

public class Test extends JFrame {    private static final long serialVersionUID = 1L;    private static JTimerLabel jtl;    public Test() {        super("动态时间");        this.setLayout(new BorderLayout());        jtl = new JTimerLabel("现在时间:");        this.add(jtl);        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);        this.setSize(160, 80);        this.setLocation(100, 200);        this.setVisible(true);        jtl.start();    }    public static void main(String[] args) {        new Test();    }}
相关栏目:

用户点评