Java模拟时钟

来源:互联网 发布:each遍历json对象二重 编辑:程序博客网 时间:2024/05/22 02:16


view plain
  1. import java.awt.BorderLayout;  
  2. import java.awt.Color;  
  3. import java.awt.Dimension;  
  4. import java.awt.Panel;  
  5. import java.text.SimpleDateFormat;  
  6. import java.util.Calendar;  
  7. import java.util.Date;  
  8.   
  9. import javax.swing.JLabel;  
  10.   
  11. public class clockPanel extends Panel  
  12. {  
  13.     Panel p1, p2, p3;  
  14.     JLabel label1;  
  15.     int year, month, day, week;  
  16.   
  17.     public clockPanel()  
  18.     {  
  19.         setBackground(Color.yellow);  
  20.         setPreferredSize(new Dimension(250320));  
  21.         setLayout(new BorderLayout(1010));  
  22.         /**************************************************************************/  
  23.         p1 = new Panel();  
  24.         label1 = new JLabel();  
  25.   
  26.         //Calendar c = Calendar.getInstance();    
  27.         SimpleDateFormat df = new SimpleDateFormat("yyyy年MM月dd日(EE)");//设置日期格式  
  28.         //System.out.println(df.format(new Date()));// new Date()为获取当前系统时间  
  29.         //System.out.println(new Date());  
  30.         //week = c.getTime().getDay();  
  31.         label1.setText(df.format(new Date()));  
  32.         p1.add(label1);  
  33.         /**************************************************************************/  
  34.         p2 = new panel2();  
  35.         /**************************************************************************/  
  36.         p3 = new panel3();  
  37.   
  38.         /**************************************************************************/  
  39.         this.add(p1, BorderLayout.NORTH);  
  40.         this.add(p2, BorderLayout.CENTER);  
  41.         this.add(p3, BorderLayout.SOUTH);  
  42.     }  
  43.   
  44. }  
  45. import java.awt.Container;  
  46. import javax.swing.JFrame;  
  47.   
  48. public class myClock  
  49. {  
  50.   
  51.     /** 
  52.      * @param args 
  53.      */  
  54.     public static void main(String[] args)  
  55.     {  
  56.         JFrame frame = new JFrame("时钟");  
  57.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序  
  58.         Container contentPane = frame.getContentPane();  
  59.         // 得到窗口内容面板  
  60.         contentPane.add(new clockPanel());  
  61.         frame.pack();  
  62.         frame.setVisible(true); // 设置窗口可见  
  63.   
  64.     }  
  65.   
  66. }  
  67.   
  68. import java.awt.Font;  
  69. import java.awt.Graphics;  
  70. import java.awt.Panel;  
  71. import java.awt.event.ActionEvent;  
  72. import java.awt.event.ActionListener;  
  73. import java.util.GregorianCalendar;  
  74.   
  75. import javax.swing.JLabel;  
  76. import javax.swing.Timer;  
  77.   
  78. public class panel3 extends Panel  
  79. {  
  80.     Timer timer;  
  81.     private int hour, min, sec;  
  82.   
  83.     JLabel label2;  
  84.   
  85.     public panel3()  
  86.     {  
  87.         label2 = new JLabel();  
  88.         label2.setFont(new Font("SAN_SERIF", Font.BOLD, 20));  
  89.         add(label2);  
  90.   
  91.         // setPreferredSize(new Dimension(150,200));  
  92.         timer = new Timer(1000new myActionListener());  
  93.         timer.start();  
  94.   
  95.         GregorianCalendar date = new GregorianCalendar();  
  96.         hour = date.getTime().getHours();  
  97.         min = date.getTime().getMinutes();  
  98.         sec = date.getTime().getSeconds();  
  99.     }  
  100.   
  101.     public void paint(Graphics g)  
  102.     {  
  103.         super.paint(g);  
  104.         /****************************************************************************/  
  105.         // 画数字钟  
  106.         label2.setText(String.format("%1$,02d", hour) + ":"  
  107.                 + String.format("%1$,02d", min) + ":"  
  108.                 + String.format("%1$,02d", sec));  
  109.     }  
  110.   
  111.     public class myActionListener implements ActionListener  
  112.     {  
  113.         @SuppressWarnings("deprecation")  
  114.         public void actionPerformed(ActionEvent e)  
  115.         {  
  116.             if (e.getSource() == timer)  
  117.             {  
  118.                 GregorianCalendar date = new GregorianCalendar();  
  119.                 hour = date.getTime().getHours();  
  120.                 min = date.getTime().getMinutes();  
  121.                 sec = date.getTime().getSeconds();  
  122.                 // System.out.println(sec);  
  123.                 // degree+=6;  
  124.                 // System.out.println(degree);  
  125.                 repaint();  
  126.             }  
  127.         }  
  128.     }  
  129. }  
  130.   
  131. public class ClockCenter  
  132. {  
  133.     public static int centerX = 120;  
  134.     public static int centerY = 120;  
  135. }  
  136.   
  137. import java.awt.Container;  
  138. import javax.swing.JFrame;  
  139.   
  140. public class myClock  
  141. {  
  142.   
  143.     /** 
  144.      * @param args 
  145.      */  
  146.     public static void main(String[] args)  
  147.     {  
  148.         JFrame frame = new JFrame("时钟");  
  149.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // 点击x结束程序  
  150.         Container contentPane = frame.getContentPane();  
  151.         // 得到窗口内容面板  
  152.         contentPane.add(new clockPanel());  
  153.         frame.pack();  
  154.         frame.setVisible(true); // 设置窗口可见  
  155.   
  156.     }  
  157.   
  158. }  

原创粉丝点击