watch.java

来自「一个显示时间的时钟」· Java 代码 · 共 95 行

JAVA
95
字号
/*                             X11Like.java   Copyright F. P. Marin ( April 07 13:12:53 1997 )   E-mail: felix@bloch.ciens.ucv.ve      Permission to use, copy, modify and distribute this software and its   documentation for NON-COMERCIAL purposes and without fee is hereby   granted provided that this copyright notice appears in all copies.   F. P. Marin makes no representations or warranties about the suitability   of the software, either express or implied, including but no limited to   the implied warranties of merchantability, fitness for a particular   purpose, or non-infringement. F. P. Marin shall not be liable for any   damage suffered by license as a result of using, modifying or   distributing this software or its derivatives.   Html code is   <applet archive="X11Like.jar"                                          code="X11Like.class"                                      height=85                                                    width=325>   </applet>*/import java.awt.*;import java.util.Date;public class X11Like extends java.applet.Applet implements Runnable{               Font f=new Font("TimesRoman",Font.BOLD,14);       FontMetrics fm=getFontMetrics(f);       int x,y;       String s;       Thread watch=null;       public void init()       {        setLayout(new BorderLayout());        add("North",new Button(""));        add("South",new Button(""));        add("East",new Button(""));        add("West",new Button(""));        y=(int)(0.5*(size().height + fm.getAscent() + 1));       }              public void start()       {        if ( watch==null ) {           watch=new Thread(this);           watch.start();        }       }       public void run()       {        do {            s=new Date().toLocaleString();            x=(size().width - fm.stringWidth(s))/2;            repaint();            try {                  Thread.sleep(1000);            } catch ( InterruptedException ie ) {}        } while ( true );        }       public void update(Graphics g)       {        paint(g);       }       public void paint(Graphics g)       {        g.setColor(Color.black);        g.fillRect(0,0,size().width,size().height);        g.setColor(Color.green.brighter());        g.setFont(f);        g.drawString(s,x,y);       }              public void stop()       {        if ( watch!=null ) {           watch.stop();           watch=null;        }       }}

⌨️ 快捷键说明

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