With the timer delay to achieve the mouse double click event events, click and double-click events independently of each other!

import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.Date;
import java.util.Timer;

/**
 *  Use the timer delay click event implements mouse double-click event  , Click and double-click event from interfering with each other  !
 */
public class UserMouseAdapter extends MouseAdapter {

        private boolean flag = false;//  Use to determine whether has been executed the double-click event
        private int clickNum = 0;//  Use to determine whether the execution double-click event  

        public void mouseClicked(MouseEvent e) {
                final MouseEvent me = e;//  The event source  

                this.flag = false;//  Each click of the mouse initialization double-click event flag to false  

                if (this.clickNum == 1) {//  When clickNum  ==1 When you perform a double-click event
                        this.mouseDoubleClicked(me);//  Perform a double-click event
                        this.clickNum = 0;//  Initializes a double-click event flag to 0
                        this.flag = true;//  Double-click the event have already been performed  , Event flag to true
                        return;
                }

                //  Define the timer
                Timer timer = new Timer();

                //  Timer start  , Delay-0  .2 Seconds after the click event
                timer.schedule(new java.util.TimerTask() {
                        private int n = 0;//  Record timer executions  

                        public void run() {
                                if (flag) {//  If you double-click event has executed  , Then click the perform directly cancel
                                        n = 0;
                                        clickNum = 0;
                                        this.cancel();
                                        return;
                                }
                                if (n == 1) {//  Timer wait 0  .2 Seconds  , Double-click the event has not yet happened  , Perform the click event
                                        mouseSingleClicked(me);//  Perform the click event
                                        flag = true;
                                        clickNum = 0;
                                        n = 0;
                                        this.cancel();
                                        return;
                                }
                                clickNum++;
                                n++;
                        }
                }, new Date(), 200); //  To set the delay time
        }

        /** */
        /**
         *  The mouse click event
         *
         * @param e
         *             The event source parameters
         */
        public void mouseSingleClicked(MouseEvent e) {
                // System.out.println("Single Clicked!");
        }

        /** */
        /**
         *  Mouse double-click event
         *
         * @param e
         *             The event source parameters
         */
        public void mouseDoubleClicked(MouseEvent e) {
                // System.out.println("Doublc Clicked!");
        }
}
分类:Java 时间:2010-03-29 人气:544
分享到:
blog comments powered by Disqus

相关文章

  • (Original) Java timer: java.util.Timer 2010-11-25

    java.util.Timer time in the program can achieve the task, Timer and implementation classes need to inherit java.util.TimerTask the run () method, in fact, is an abstract class TimerTask, he inherited the java.lang.Runnable interface. package yehaizi.

  • Java language, Calendar and Date classes 2010-03-29

    Java-Calendar (calendar), Date (date), and DateFormat (date format) to form a basic Java standard but very important part. Business logic to calculate the date is a key part. All the developers should be able to calculate a future date, custom date d

  • java to operate the date type 2010-03-05

    In JDK 1.1, after, java.util.Date type of most of the methods have not recommended use. Instead it is the Calendar. In between java.sql.Date and java.util.Date, and some subtle relationship. How to Date type into a database, a lot of people in a no s

  • flex and java how communication with the public session webService 2010-12-26

    flex and java how communication with the public session webService This is very troublesome, because you have to be very understanding of what session which in the end (as to flex there, not so strict over there in java) The correct approach is, java

  • java.sql.SQLException: ORA-01830: date format picture ends before converting ent 2011-08-17

    java.sql.SQLException: ORA-01830: date format picture ends before converting ent ire input string at oracle.jdbc.driver.DatabaseError.throwSqlException (Ljava.lang.String; Ljava.lang.String; I) V (DatabaseError.java: 125) at oracle.jdbc.driver.T4CTTI

  • Some rights java modifiers, private / friendly / protected / public 2010-05-29

    Begin testing the following: A Test class, as was access to the class, private not say, can access only its own, public is able to access, this is mainly to distinguish between friendly and protected New two classes, Test1 and Test2 to visit Test, Te

  • Double click a button to install - Java installation program production 2011-04-22

    Double click a button to install - Java installation program production Title: A double-click the Run button to install - Java installation program made of: Jason Du Time: 2009-3-23 Original: http://www.blogjava.net/javapro/archive/2009/03/24/JavaApp

  • public void testStatementAddBatch () public void testConnCommit () 2011-09-01

    public void testStatementAddBatch() { Connection conn = null; Statement stmt = null; try { conn = getDBConnectionMsSqlBySuperAdmin(); stmt = conn.createStatement(); for(int i = 0; i < 100; i++) { String sql = " sql " + i; stmt.addBatch(sql);

  • java convert string to date type 2010-08-24

    String date = "2006-11-23" SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); java.util.Date cDate = df.parse (date); java.sql.Date dd = new java.sql.Date (cDate.getTime ()); System.out.println ("converted date =" + d

  • JAVA string to SQL date 2011-07-23

    / / Inorder to get the SQL Date type like date, use the following steps: String date = "2007-11-23" SimpleDateFormat df = new SimpleDateFormat ("yyyy-MM-dd"); java.util.Date Date = df.parse (date); java.sql.Date formatDate = new java.s

iOS 开发

Android 开发

Python 开发

JAVA 开发

开发语言

PHP 开发

Ruby 开发

搜索

前端开发

数据库

开发工具

开放平台

Javascript 开发

.NET 开发

云计算

服务器

Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.562 (s). 12 q(s)