zoukankan      html  css  js  c++  java
  • java动态显示时间

      

    import java.awt.Frame;
    import java.awt.HeadlessException;
    import java.awt.Label;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    
    public class Timer {
        public static void main(String[] args) {
            Time time = new Time("系统时间");
            Thread t = new Thread(time);
            t.start();
        }
    
    }
    
    class Time extends Frame implements Runnable {
    
        Date nowtime;
        Label text;
        Label timeStr;
    
        Time(String str) {
            super(str);
            // TODO Auto-generated constructor stub
    
            text = new Label("现在的时间是:", text.CENTER);
            nowtime = new Date();
            SimpleDateFormat matter1 = new SimpleDateFormat(
                    "HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E");
            timeStr = new Label(matter1.format(nowtime));
            add(text);
            add(timeStr);
            // setBounds(x,y,width,height); x:组件在容器X轴上的起点 y:组件在容器Y轴上的起点 组件的长度
            // height:组件的高度
            setBounds(100, 100, 500, 150);
    
            addWindowListener(new WindowAdapter() {
                public void windowClosing(WindowEvent e) {
                    System.exit(0);
                }
            });
            setVisible(true);
            validate();
    
        }
    
        @Override
        public void run() {
            while (true) {
                nowtime = new Date();
                SimpleDateFormat matter2 = new SimpleDateFormat(
                        "HH 时 mm 分 ss秒 yyyy年 MM 月DD 日 E");
                timeStr.setText((matter2.format(nowtime)));
                try {
                    Thread.sleep(1000);
                } catch (Exception ex) {
                }
            }
        }
    
    }
  • 相关阅读:
    兼容ie6的ul水平居中对齐
    button小手设置 css的cursor
    virtualbox xp连不上网
    转发与重定向的区别(forward与redirect的区别)
    注册表单输入框检测
    html中块元素的居中。及兼容性
    centos下不重装php——给PHP添加新扩展库
    linux编译安装时常见错误解决办法
    Swift可选链
    jQueryMobile(一)
  • 原文地址:https://www.cnblogs.com/simplty/p/2950844.html
Copyright © 2011-2022 走看看