JAVA窗口程序实例一

 1 package 甲;
 2 
 3 import java.awt.Dimension;
 4 import java.text.SimpleDateFormat;
 5 import java.util.Calendar;
 6 import java.util.Date;
 7 import java.util.Timer;
 8 import java.util.TimerTask;
 9 import javax.swing.JFrame;
10 import javax.swing.JLabel;
11 import javax.swing.JPanel;
12 /**
13  * This class is a simple JFrame implementation(实现) to explain how to display time
14  * dynamically(动态的) on the JSwing-based interface.
15  */
16 @SuppressWarnings("serial")
17 public classextends JFrame {
18     /*
19      * Variables
20      */
21     private JPanel timePanel;
22     private JLabel timeLabel;
23     private JLabel displayArea;
24     private String DEFAULT_TIME_FORMAT = "GYYYY年MM月dd日 EEE HH时mm分ss秒SSS";
25     //G-公元
26     //YYYY-年份
27     //MM-月份
28     //dd-日期
29     //EEE-星期
30     //HH-小时
31     //mm-分钟
32     //ss-秒钟
33     //SSS-毫秒
34     private String time;
35     private int ONE_SECOND = 9;//9毫秒刷新一次
36 
37     public 一() {
38         super("东八区");//标题栏文字
39         timePanel = new JPanel();
40         timeLabel = new JLabel("北京时间");
41         displayArea = new JLabel();
42 
43         configTimeArea();
44         timePanel.add(timeLabel);
45         timePanel.add(displayArea);
46         this.add(timePanel);
47         this.setAlwaysOnTop(true); //窗口置顶
48         this.setResizable(false);//窗口大小固定
49         this.setDefaultCloseOperation(EXIT_ON_CLOSE);//可以单击X退出
50         this.setSize(new Dimension(300, 90));//窗口尺寸大小
51         this.setLocationRelativeTo(null);//窗口出现位置
52         this.setType(java.awt.Window.Type.UTILITY); //窗口在任务栏不显示
53         this.setUndecorated(false); //不显示标题栏
54     }
55 
56     /**
57      * 这个方法创建 a timer task 每秒更新一次 the time
58      */
59     private void configTimeArea() {
60         Timer tmr = new Timer();
61         tmr.scheduleAtFixedRate(new JLabelTimerTask(), new Date(), ONE_SECOND);
62     }
63 
64     /**
65      * Timer task 更新时间显示区
66      * 
67      */
68     protected class JLabelTimerTask extends TimerTask {
69         SimpleDateFormat dateFormatter = new SimpleDateFormat(
70                 DEFAULT_TIME_FORMAT);
71 
72         @Override
73         public void run() {
74             time = dateFormatter.format(Calendar.getInstance().getTime());
75             displayArea.setText(time);
76         }
77     }
78 
79     public static void main(String arg[]) {
80         一 timeFrame = new 一();
81         timeFrame.setVisible(true);
82     }
83 }
View Code
原文地址:https://www.cnblogs.com/TENOKAWA/p/5799844.html