Java-Alarm

来源:互联网 发布:2016淘宝双11知识问答 编辑:程序博客网 时间:2024/05/19 01:29

Java-Alarm

  • 偶然的机会 :
    • 因为市场的需求才会产生我们所写的代码, 所以今天我也帮助同学的项目中的需要产生了一个需求, 就是检测计算机CPU的占用率和内存的占用率过高出现报警的声音。
    • 以下展示的代码是实现报警装置的代码。

  • Code :
package Test;import javax.swing.Timer;import javax.swing.JOptionPane;import java.awt.Toolkit;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import java.util.Date;//import java.util.Timer;/** * Created by JackDan9 on 2017/5/25. */public class TimerTest implements ActionListener{    public void actionPerformed (ActionEvent e) {        Date jkd = new Date();        System.out.println("the time is : " + jkd);        Toolkit.getDefaultToolkit().beep();    }    public static void main(String[] args) {        ActionListener jkal = new TimerTest();//        Timer jkt = new Timer(1000, jkal);// javax.swing.Timer        Timer jkt = new Timer(1000, jkal);        jkt.start();//        jkt.stop();        System.out.println("stop time !!!!");// javax.swing.JOptionPane              JOptionPane.showMessageDialog(null, "exit system");        System.exit(0);    }}
  • 简单实现 :
    • 程序启动, 每隔1s出现一次警报的声音, 点击pane的弹出面板的确定按钮就会停止声响, 就是简单的实现了报警的效果.下面进行代码的解析.
    • Toolkit.getDefaultToolkit().beep();
    • public abstract class Toolkit
      extends Object
    • 此类是所有 Abstract Window Toolkit实际实现的抽象超类. Toolkit 的子类被用于将各种组件绑定到特定本机工具包实现.
    • 许多 GUI 操作可以异步执行. 这意味着如果设置某一组件的状态,随后立刻查询该状态,则返回的值可能并没有反映所请求的更改. 这包括但不局限于以下操作 :
      • 滚动到指定位置.
      • 例如,如果原始请求没有被处理,那么调用 ScrollPane.setScrollPosition 并随后调用getScrollPosition可能返回一个不正确的值.

  • import javax.swing.Timer;
    • 在java里面书写某些程序的时候需要使用定时器的效果, 为什么了? 因为我们需要去定时触发一个事件, 这个事件就是去解决一些你想要的、预计的效果, 所以我们也就需要去import一个这样的Timer类。
    • Timer的API: public class Timer extends Object implements Serialiable
    • Timer的介绍API: Files one or more ActionEvents at specified intervals. An example use is an animation object that uses a Timer as the trigger for drawing its iframes.解释: 用以指定的时间间隔记录一个或多个ActionEvents(动态的事件), 而在一般的样本示例中使用Timer去处理动画效果, Timer被认为是一个触发器的内部框架, 而这个触发器是关于绘画的.
    • Setting up a timer involves creating a Timer object, registering one or more action listeners on it, and starting the timer using the start method. For example, the following code creates and starts a timer that fires an action event once per second (as specified by the first argument to the Timer constructor). The second argument to the Timer constructor specifies a listener to receive the timer’s action events. 解释:设置定时器包括一下步骤:1、设置一个定时器对象;2、在这个定时器对象上注册/定义一个或者多个监听对象;3、使用start方法来启动定时器对象。例如,以下的代码创建并且启动每隔一秒中去触发一次动作事件的定时器事件(这是Timer构造函数的第一个参数指定)。Timer的第二个参数用来指定一个侦听器来接收定时器的动作事件。
int delay = 1000; //millisecondsActionListener taskPerformer = new ActionListener() {    public void actionPerformed (ActionEvent evt) {        // ...Perform a task...    }};new Timer(delay, taskPerformer).start();
  • 在本例中也用到了类似的用法.

  • javax.swing.JOptionPane;
    • Java API:
    • public class JOptionPane extends JComponent implements Accessible
    • Java API: 用法
      • JOptionPane makes it easy to pop up a standard dialog box that prompts users for a value or informs them of something. 解释:JOptionPane可以轻松地弹出一个标准对话框, 提示用户获取值或通知他们某些东西。
      • While the JOptionPane class may appear complex because of the large number of methods, almost all uses of this class are one-line to one of the static showXxxDialog methods shown below:解释:虽然JOptionPane类可能会因为大量方法而复杂化,但几乎所有这些类的使用都是单行调用静态showXxxDialog方法之一,如下所示:
        • showConfirmDialog: Asks a confirming question, like yes/no/cancel.
        • showInputDialog: Prompt for some input.
        • showMessageDialog: Tell the user about something that has happened.
        • showOptionDialog: The Grand Unification of the above three.
        • 为什么在这里使用showMessagePane的方法进行操作,上面的解释已经十分地清楚了.

  • java.awt.event.ActionEvent
    • actionEvent是一个动作事件,该事件为执行动作事件ACTION_PERDORMED.触发这个事件的动作为:
      • 1、点击按钮button;
      • 2、双击列表中的选项;
      • 3、选择菜单项;
      • 4、在文本框中输入回车;
    • 常使用的方法是:
      • public String getActionCommand()
      • 返回引发某个事件的命令按钮的名字,如果名字为空,那么返回标签值。
      • public void setActionCommand(String command)
      • 设置引发事件的按钮的名字,默认设置为按钮的标签。

  • awt的事件处理:
    • 在学习处理事件时,必须很好的掌握事件源、监视器、处理事件的接口:
      • 事件源:能够产生java认可的对象都可称为事件源,也就是说事件源必须是对象。
      • 监视器:监视事件源,以便对发生的事件做出处理。
      • 如:对于文本框,这个方法为:
        • addActionListener(监视器);
      • 处理事件的接口:为了让监视器这个对象能对事件源发生的事件进行处理,创建该监视器对象的类必须声明实现响应的接口,即必须在类体中给出该接口中所有方法的方法体。
      • java.awt.event包中提供了许多事件类和处理事件的接口。
      • 对于文本框,这个接口的名字是ActionListener,这个接口的唯一方法是:public void actionPerformed(ActionEvent e)
      • 为了监视到ActionEvent类型的事件,事件源必须使用addActionListener方法获得监视器,创建监视器的类必须实现接口ActionListener
    • ActionEvent类有如下常用方法:
      • public Object getSource()
      • ActionEvent对象调用该方法可以获取发生ActionEvent事件的事件源对象的引用。
      • public String getActionCommand()
      • ActionEvent对象调用该方法可以获取发生ActionEvent事件时,和该事件相关的一个命令字符串。
    • 注意:创建监视器对象的类必须声明实现相应的接口:
      • class A implements xxxListener

JackDan9 Thinking

原创粉丝点击