example9_2.java

来自「《Java程序设计与应用》-张仕斌-源程序 《Java程序设计与应用》-张仕斌」· Java 代码 · 共 73 行

JAVA
73
字号
/*
 * Example9_2.java
 *
 * Created on 2006年10月19日, 上午11:24
 */

package example9_2;
import java.awt.Graphics;
import java.awt.Color;
import java.util.Date; 
import java.applet.Applet;

/**
 *
 * @author  Administrator
 */
public class Example9_2 extends Applet implements Runnable{
    
    /** Initializes the applet Example9_2 */
  /*  public void init() {
        try {
            java.awt.EventQueue.invokeAndWait(new Runnable() {
                public void run() {
                    initComponents();
                }
            });
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }*/
   private Thread clockThread = null;
	public void init() { setBackground(Color.white);}
  	public void start()
        {
    	if (clockThread == null) 
        {
      		clockThread = new Thread(this, "Clock");
      		clockThread.start();
    	}
  	}
  	public void run(){
    	Thread myThread = Thread.currentThread();
    	while(clockThread == myThread) {
      		repaint();
      		try {Thread.sleep(1000);}
     		catch (InterruptedException e) { }
   		}
  	}
  	public void paint(Graphics g) {
    	Date now = new Date();
    	g.drawString(now.getHours() + ":" +now.getMinutes() + ":" +now.getSeconds(), 5, 10);
      	}
      	public void stop() {clockThread = null;}
 
    /** This method is called from within the init() method to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc=" Generated Code ">//GEN-BEGIN:initComponents
    private void initComponents() {
        
        setLayout(new java.awt.BorderLayout());
        
    }
    // </editor-fold>//GEN-END:initComponents
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
    
}

⌨️ 快捷键说明

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