timerdemo1.java

来自「java 完全探索的随书源码」· Java 代码 · 共 54 行

JAVA
54
字号
// TimerDemo1.java

import javax.swing.*;

import java.awt.*;
import java.awt.event.*;

class TimerDemo1 extends JFrame
{
   Timer timer;
   int counter;

   TimerDemo1 (String title)
   {
      super (title);

      addWindowListener (new WindowAdapter ()
                         {
                             public void windowClosing (WindowEvent e)
                             {
                                System.exit (0);
                             }
                         });

      ActionListener a = new ActionListener ()
                         {
                             public void actionPerformed (ActionEvent e)
                             {
                                System.out.println ("Counter = " +
                                                    counter);

                                if (++counter > 10)
                                {
                                    timer.stop ();
                                    System.exit (0);
                                }
                             }
                         };

      timer = new Timer (300, a);
      timer.start ();

      pack ();
      setVisible (true);
   }

   public static void main (String [] args)
   {
      new TimerDemo1 ("Timer Demo1");
   }
}


⌨️ 快捷键说明

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