2014-01-29 170 views
0

我想知道是否可以使用事件监听器而不是鼠标监听器来双击JButton。考虑下面的代码;事件监听器和鼠标监听器

public void actionPerformed(ActionEvent arg0){ 
    if (arg0.getClickCount() == 2){ 
     System.out.println("You Doubled clicked"); 
    } 
} 

我收到一个错误,说getClickCount() is undefined for the type ActionEvent。鼠标的点击或双击不被视为事件吗?思考。

回答

0

你想用一个MouseAdapter。它允许你不要用不必要的方法混淆你的代码(mouseDraggedmouseEntered等)。

public class MyClass extends MouseAdapter { 
    @Override 
    public void mouseClicked(MouseEvent e) { 
     if (e.getClickCount() == 2) { 
      // Double click 
     } else { 
      // Simple click 
     } 
    }  
} 

另外,如果你的类已经扩展了另一个类,试试这个代码:

public class MyClass extends MyBaseClass { 
    private MouseAdapter ma; 

    public MyClass() { 
     final MyClass that = this; 
     ma = new MouseAdapter() { 

      @Override 
      public void mouseClicked(MouseEvent e) { 
       that.myMouseClickedHandler(e); 
      } 
     }; 
    } 

    public void myMouseClickedHandler(MouseEvent e) { 
     if (e.getClickCount() == 2) { 
      // Double click 
     } else { 
      // Simple click 
     }   
    } 
} 
+0

答案取决于OP是否只想知道鼠标点击或想要继续支持正常的交互(如[enter]或[space]) – MadProgrammer

1

你不能。如果您不确定,请阅读文档。方法OnClickCount不存在于Action Event类中,它仅在MouseEvent类中可用。如果你想要,然后写你自己的方法。

请参见以下文档以供参考

http://docs.oracle.com/javase/7/docs/api/java/awt/event/ActionEvent.html

http://docs.oracle.com/javase/7/docs/api/java/awt/event/MouseEvent.html

+0

有关如何创建我自己的方法来检查是否连续单击JButton或双击的任何建议?道歉,如果这是一个天真的问题。我对Java仍然很陌生。 – Hustl3r28

+1

你为什么要重新发明轮子?使用此方法 btn.addMouseListener(新java.awt.event.MouseAdapter(){ 公共无效的mouseClicked(java.awt.event.MouseEvent中EVT){ 如果(evt.getClickCount()== 2){ //做你想在这里的东西 } } }); – Hamza

0

动作事件不具有 “getClickCount()” 方法。
参见文档:ActionEvent API Doc

你可以在actionPerformed方法定义一个变量“numClicks”:

public void actionPerformed(ActionEvent e) { 
    numClicks++; 

那么,如果“numClicks”等于“2”,发生了鼠标的双击,那么你可以将其设置回零等...

+0

虽然这种方法更好地满足了用户的要求,但它仍然存在如何确定用户是否只点击一次,离开一段时间后再点击 – MadProgrammer

0

答案取决于。你只想知道按钮什么时候被“点击”两次或者什么时候被“按下”两次?

它一般不提倡到MouseListener附加到一个按钮,该按钮可以以多种方式被触发,包括编程

你所需要的是能够做到的,是不是只算次数actionPerformed被调用,但也知道点击之间的时间。

您可以记录最后一次点击时间,并将其与当前时间进行比较,并以此方式进行确定,或者您可以简单地使用javax.swing.Timer这将为您做。

下面的例子还检查,看是否ActionEvent的最后一个来源是一样的电流源,如果它不是计数器复位......

这也使得鼠标点击,按键和方案触发器...

import java.awt.BorderLayout; 
import java.awt.EventQueue; 
import java.awt.GridBagLayout; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import javax.swing.JButton; 
import javax.swing.JFrame; 
import javax.swing.Timer; 
import javax.swing.UIManager; 
import javax.swing.UnsupportedLookAndFeelException; 

public class TimerButton { 

    public static void main(String[] args) { 
     new TimerButton(); 
    } 

    public TimerButton() { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName()); 
       } catch (ClassNotFoundException ex) { 
       } catch (InstantiationException ex) { 
       } catch (IllegalAccessException ex) { 
       } catch (UnsupportedLookAndFeelException ex) { 
       } 

       JButton btn = new JButton("Testing"); 
       btn.addActionListener(new ActionHandler()); 

       JFrame frame = new JFrame("Test"); 
       frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
       frame.setLayout(new GridBagLayout()); 
       frame.add(btn); 
       frame.pack(); 
       frame.setLocationRelativeTo(null); 
       frame.setVisible(true); 
      } 
     }); 
    } 

    public class ActionHandler implements ActionListener { 

     private Timer timer; 
     private int count; 
     private ActionEvent lastEvent; 

     public ActionHandler() { 
      timer = new Timer(250, new ActionListener() { 
       @Override 
       public void actionPerformed(ActionEvent e) { 
        System.out.println("Tick " + count); 
        if (count == 2) { 
         doubleActionPerformed(); 
        } 
        count = 0; 
       } 
      }); 
      timer.setRepeats(false); 
     } 

     @Override 
     public void actionPerformed(ActionEvent e) { 
      if (lastEvent != null && lastEvent.getSource() != e.getSource()) { 
       System.out.println("Reset"); 
       count = 0; 
      } 
      lastEvent = e; 
      ((JButton)e.getSource()).setText("Testing"); 
      count++; 
      System.out.println(count); 
      timer.restart(); 
     } 

     protected void doubleActionPerformed() { 
      Object source = lastEvent.getSource(); 
      if (source instanceof JButton) { 
       ((JButton)source).setText("Double tapped"); 
      } 
     } 


    } 

} 
+0

我想知道是否按了两次按钮。 – Hustl3r28

+0

是的,这会告诉你它是否被“按下”两次,不管它是通过鼠标,键盘还是以编程方式 – MadProgrammer