栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java 核心技术(第八版)卷1:基础知识:例6-3P218TimerTest

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java 核心技术(第八版)卷1:基础知识:例6-3P218TimerTest

6.3这个部分讲的是接口和回调

当到达指定的时间间隔之后,定时器就调用actionPerformed方法

import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
import javax.swing.Timer;//避免和 java.util.Timer 冲突
public class TimerTest {
    public static void main(String[] args)
    {
        ActionListener listener=new TimerPrinter();
        //构建一个 timer 它可以每隔10秒钟调用listener
        Timer t=new Timer(10000,listener);
        t.start();
        JOptionPane.showMessageDialog(null,"Quit program?");
        System.exit(0);

    }

}
class TimerPrinter implements ActionListener
{
    public void actionPerformed(ActionEvent event)
    {
        Date now=new Date();
        System.out.println("At the tone ,the time is:"+now);
        Toolkit.getDefaultToolkit().beep();
    }
}

运行结果:

首先出现一个对话框:

 

不要做任何操作 10秒后,出现:

At the tone ,the time is:Fri May 06 12:36:09 CST 2022
At the tone ,the time is:Fri May 06 12:36:19 CST 2022
At the tone ,the time is:Fri May 06 12:36:29 CST 2022
At the tone ,the time is:Fri May 06 12:36:39 CST 2022
At the tone ,the time is:Fri May 06 12:36:49 CST 2022

计算机还会发出蜂鸣声。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/862867.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号