zoukankan      html  css  js  c++  java
  • JavaSE 事件监听器接口

    {相关信息}
    Java 除了提供实现监听器接口的方法处理事件,还提供了另外一种简单的实现监听器的手段——事件适配器(EventAdapter)。程序员可以通过继承事件所 对应的适配器类,

    重写感兴趣的方法。通过事件适配类可以缩短程序代码,但是 Java 只能实现单一的继承,当程序需要捕获多种事件时,就无法使用事件适配器的方法了。

    java.awt.event 包中定义的事件适配器类包括以下几种:

    ComponentAdapter (组件适配器)
    ContainerAdapter (容器适配器)
    FocusAdapter (焦点适配器)
    KeyAdapter (键盘适配器)
    MouseAdapter (鼠标适配器)
    MouseMotionAdapter (鼠标运动适配器)
    WindowAdapter (窗口适配器)

    {示例样图}
    功 能描述:面 板 ( panel ) 中 添 加 监 听 器MouseAdapter , 并 重 载 了 mousePressed 和mouseReleased 方法。在 mousePressed 方法中,

    获取鼠标的坐标并保存。在 mouseReleased 中,再次获取鼠标的坐标,然后根据前后获取的坐标画一条线。

    {相关代码}

    package sup.orange.learn;
    
    import java.awt.*;
    import java.awt.event.MouseAdapter;
    import java.awt.event.MouseEvent;
    import java.awt.event.WindowAdapter;
    import java.awt.event.WindowEvent;
    import java.awt.image.ImageObserver;
    import java.text.AttributedCharacterIterator;
    
    /**
     * Created by re-x on 11/2/14.
     */
    public class AdapterDemo {
    
        private Frame f;
        Point start, end;
        Panel panel;
    
        public AdapterDemo () {
            f = new Frame("click or drag mouse");
            panel = new Panel();
            f.add("Center", panel);
    
            panel.addMouseListener(new MouseAdapter() {
                public void mousePressed(MouseEvent e) {
                    start = e.getPoint();
                    System.out.println("start "+start);
                }
    
                public void mouseReleased(MouseEvent e) {
                    end = e.getPoint();
                    System.out.println("end "+end);
                    Graphics g = panel.getGraphics();
                    panel.paint(g);
                    g.drawLine(start.x, start.y, end.x, end.y);
    
                }
            });
    
            f.addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    super.windowClosing(e);
                    System.exit(1);
                }
            });
    
            f.setSize(300, 400);
            f.setVisible(true);
        }
    
        public static void main(String[] args) {
            new AdapterDemo();
        }
    }
  • 相关阅读:
    JavaScript测试工具chai
    gradle初始仓库依赖(含junit)
    汇编和中断
    oracle-12c-ee安装
    pickle
    Python (zip, lambda, map, shallow copy, deepcopy)
    豆瓣源下载tensorflow
    Linux上XRDP可作为WIN的远程连接
    VNC连接远程Linux——废弃电脑作为运算机器
    MACOS 运行Keras报错
  • 原文地址:https://www.cnblogs.com/aqing1987/p/4231510.html
Copyright © 2011-2022 走看看