时钟类---swing画图、Timer、JFrame用法示例

来源:互联网 发布:不是方阵有逆矩阵吗 编辑:程序博客网 时间:2024/05/22 11:54

1、运行结果

2、源代码如下:

[java] view plaincopy
  1. package demo.others;  
  2.   
  3. import java.awt.BorderLayout;  
  4. import java.awt.Color;  
  5. import java.awt.Font;  
  6. import java.awt.Graphics;  
  7. import java.util.Calendar;  
  8. import java.util.GregorianCalendar;  
  9. import java.util.TimerTask;  
  10.   
  11. import javax.swing.*;  
  12.   
  13. /** 
  14.  * 时钟类 
  15.  */  
  16. public class Clock extends JFrame {  
  17.     // 画时钟的面板  
  18.     private paintPanel clock = new paintPanel();  
  19.     // 定时器  
  20.     private java.util.Timer timer = new java.util.Timer();  
  21.     // 显示时间的label  
  22.     JLabel messageLabel = new JLabel("", SwingConstants.CENTER);  
  23.   
  24.     public Clock() {  
  25.         setTitle("时钟");  
  26.         this.setVisible(true);  
  27.         this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  28.         // this.setLocationRelativeTo(null);  
  29.         this.setSize(400400);  
  30.   
  31.         add(clock);  
  32.         messageLabel.setForeground(Color.RED);  
  33.         messageLabel.setFont(new Font("Courier", Font.BOLD, 18));  
  34.         add(messageLabel, BorderLayout.SOUTH);  
  35.   
  36.         // 定时器执行任务  
  37.         timer.schedule(new TimerTask() {  
  38.             @Override  
  39.             public void run() {  
  40.                 clock.setCurrentTime();// 设置为当前时间  
  41.                 messageLabel.setText(clock.getHour() + ":" + clock.getMinute() + ":" + clock.getSecond() + '\n');  
  42.                 repaint();  
  43.             }  
  44.         }, 01000);  
  45.   
  46.     }  
  47.   
  48.     public static void main(String[] args) {  
  49.         new Clock();  
  50.     }  
  51.   
  52.     private class paintPanel extends JPanel {  
  53.         private int hour, minute, second;  
  54.   
  55.         public paintPanel() {  
  56.             setCurrentTime();  
  57.         }  
  58.   
  59.         // 设置时钟为当前时间  
  60.         private void setCurrentTime() {  
  61.             Calendar calendar = new GregorianCalendar();  
  62.             hour = calendar.get(Calendar.HOUR_OF_DAY);  
  63.             minute = calendar.get(Calendar.MINUTE);  
  64.             second = calendar.get(Calendar.SECOND);  
  65.         }  
  66.   
  67.         @Override  
  68.         protected void paintComponent(Graphics g) {  
  69.             super.paintComponent(g);  
  70.             int xCenter = getWidth() / 2;  
  71.             int yCenter = getHeight() / 2;  
  72.   
  73.             // 计算半径  
  74.             int radius = (int) (Math.min(this.getWidth(), this.getHeight()) * 0.8 * 0.5);  
  75.             // 画圆  
  76.             g.drawOval(xCenter - radius, yCenter - radius, radius * 2, radius * 2);  
  77.   
  78.             // 画钟面上显示的数字  
  79.             g.drawString("12", xCenter - 6, yCenter - radius + 12);  
  80.             g.drawString("3", xCenter + radius - 12, yCenter + 4);  
  81.             g.drawString("6", xCenter - 4, yCenter + radius - 8);  
  82.             g.drawString("9", xCenter - radius + 4, yCenter + 6);  
  83.   
  84.             // 画时针、分针、秒针  
  85.             g.drawLine(xCenter, yCenter, (int) (xCenter + radius * 0.8 * Math.sin(second * 2 * Math.PI / 60)), (int) (yCenter - radius * 0.8 * Math.cos(second * 2 * Math.PI / 60)));  
  86.             g.drawLine(xCenter, yCenter, (int) (xCenter + radius * 0.6 * Math.sin(minute * 2 * Math.PI / 60)), (int) (yCenter - radius * 0.6 * Math.cos(minute * 2 * Math.PI / 60)));  
  87.             g.drawLine(xCenter, yCenter, (int) (xCenter + radius * 0.4 * Math.sin((hour + minute / 60.0) * 2 * Math.PI / 12)), (int) (yCenter - radius * 0.4  
  88.                     * Math.cos((hour + minute / 60.0) * 2 * Math.PI / 12)));  
  89.         }  
  90.   
  91.         public int getHour() {  
  92.             return hour;  
  93.         }  
  94.   
  95.         public int getMinute() {  
  96.             return minute;  
  97.         }  
  98.   
  99.         public int getSecond() {  
  100.             return second;  
  101.         }  
  102.     }  

原创粉丝点击