局部匿名内部类

package anonymousInnerClass;

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;

public class AnonymousInnerClassTest {
    public static void main(String[] args)
    {
        TalkingClock clock = new TalkingClock();
        clock.start(100,true);

        JOptionPane.showMessageDialog(null,"Quit Program?");
        System.exit(0);
    }
}

class TalkingClock{
    public void start(int interval, boolean beep)
    {
        //Anonymous Inner Class that implements the interface:ActionListener
        ActionListener listener = new ActionListener()
        {
            //need implements the actionPerformed function
            public void actionPerformed(ActionEvent event)
            {
                System.out.println("At the tone,the time is: "+new Date());
                if(beep) Toolkit.getDefaultToolkit().beep();
            }
        };
        
        Timer t = new Timer(interval,listener);
        t.start();
    }
}

 

posted @ 2018-08-01 22:43  confusion  阅读(132)  评论(0编辑  收藏  举报