⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 clock.java

📁 java文本动态显示时钟当前 时间java源代码
💻 JAVA
字号:
import java.awt.*;
import java.util.*;
import java.applet.*;
import java.text.*;
//import java.lang.Math.*;


public class Clock extends java.applet.Applet implements Runnable {
    private volatile Thread clockThread = null;
    DateFormat formatter;        // Formats the date displayed
    String lastdate;             // String to hold date displayed
    Date currentDate;            // Used to get date to display
    Color numberColor;           // Color of numbers
    Font clockFaceFont;
    Locale locale;
    private int x=0;
    private int y=12;
    private boolean MoveRight = true;
    private boolean MoveDown = true;

    public void init() {
        setBackground(Color.white);
        numberColor = Color.red;
        locale = Locale.getDefault();
        formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale);
        currentDate = new Date();
        lastdate = formatter.format(currentDate);
        clockFaceFont = new Font("Sans-Serif", Font.PLAIN, 14);
        resize(320,240); 
   }
    public void start() {
      if (clockThread == null) {
         clockThread = new Thread(this, "Clock");
         clockThread.start();
      }
    }
    public void run() {
      Thread myThread = Thread.currentThread();
        while (clockThread == myThread) {
          if(x == 110){
              MoveRight = false;
          }
          if(MoveRight){
              x++;
          }
          if(MoveRight == false){
              x--;
          }
          if( x == 0){
              MoveRight = true;
          }
          if(y == 240){
              MoveDown = false;
          }
          if(MoveDown){
              y++;
          }
          if(MoveDown == false){
              y--;
          }
          if( y == 12){
              MoveDown = true;
          }
          repaint();
            try {
                Thread.sleep(20);

                
            } catch (InterruptedException e){ }
        }
    }
    public void paint(Graphics g) {
      String today;
      currentDate = new Date();
      formatter = DateFormat.getDateTimeInstance(DateFormat.FULL, DateFormat.MEDIUM, locale);
      today = formatter.format(currentDate);
      g.setFont(clockFaceFont);

      // Erase and redraw  
      g.setColor(getBackground());
      g.drawString(lastdate, x, y);   				

      g.setColor(numberColor);
      g.drawString(today, x, y);    
      
      lastdate = today;
      currentDate=null;

    }
    public void stop() {
        clockThread = null;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -