标签动态显示时间,标签动态显示,标签动态显示时间JTim
标签动态显示时间
JTimerLabel.javaimport 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(); }}