簡體   English   中英

在 Java Swing 中生成雙擊鼠標事件

[英]Generate a double click mouse event in Java Swing

我正在嘗試在 EDT 上生成雙擊鼠標事件,如下所示:

SwingUtilities.invokeLater(new Runnable() {
    public void run() {
        component.dispatchEvent(new MouseEvent(
            component,                                      
            MouseEvent.MOUSE_CLICKED,
            System.currentTimeMillis(),
            InputEvent.BUTTON1_MASK,
            x, y,
            2, // click count
            false
        ));
    }
});

即使我將點擊次數設置為 2,這似乎也不會發送雙擊事件。

有什么建議或例子嗎?

考慮到:

final JButton clickTwiceButton = new JButton();
final JButton fireEventButton = new JButton();

聽眾:

clickTwiceButton.addMouseListener(new java.awt.event.MouseAdapter() {
    public void mouseClicked(java.awt.event.MouseEvent evt) {
        if (evt.getClickCount() == 2) {
            JOptionPane.showMessageDialog(null, "Double clicked!");
        }
    }
});

fireEventButton.addActionListener(new java.awt.event.ActionListener() {
    public void actionPerformed(java.awt.event.ActionEvent evt) {
        // Invoking later for no reason, just to simulate your code
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                clickTwiceButton.dispatchEvent(new MouseEvent(
                     fireEventButton,
                     MouseEvent.MOUSE_CLICKED, 
                     1,
                     MouseEvent.BUTTON1, 
                     0, 0, 
                     2, 
                     false
                ));
            }
        });
    }         
});

當我單擊fireEventButtonMouseEvent被正確分派到clickTwiceButton ,對話框按預期顯示。

所以,正如@Andrew指出的那樣,問題似乎是要么將事件觸發到錯誤的組件,要么使用已注冊的MouseListener / MouseAdapter代碼來解決問題。

使用component.getMouseListeners()檢查組件偵聽器並調試處理其事件的代碼。

方法非常簡單。 你應該得到第一次點擊的時間和第二次點擊的時間,然后你可以在兩者之間做一個條件。 方法代碼如下:

private boolean state=false;
private long first_pressed;
JButton btnAdd = new JButton("add");
btnAdd.addMouseListener(new MouseAdapter() {
        @Override
        public void mousePressed(MouseEvent e) {
            
            if(state==false) {
                first_pressed=e.getWhen();
                state=true;
            }
            if(first_pressed!=e.getWhen())
            {
                JOptionPane.showConfirmDialog(null,"doubel click","Click",JOptionPane.YES_NO_OPTION);
                state=false;
            }
        }
 });
   public class TestMouseListener implements MouseListener {    
      private boolean leftClick;
      private int clickCount;
      private boolean doubleClick;
      private boolean tripleClick;
      public void mouseClicked(MouseEvent evt) {
        if (evt.getButton()==MouseEvent.BUTTON1){
            leftClick = true; clickCount = 0;
            if(evt.getClickCount() == 2) doubleClick=true;
            if(evt.getClickCount() == 3){
                doubleClick = false;
                tripleClick = true;
            }
            Integer timerinterval = (Integer)Toolkit.getDefaultToolkit().getDesktopProperty("awt.multiClickInterval");

                     Timer  timer = new Timer(timerinterval, new ActionListener() {
                        public void actionPerformed(ActionEvent evt) { 

                            if(doubleClick){
                                System.out.println("double click.");                                    
                                clickCount++;
                                if(clickCount == 2){
                                    doubleClick();   //your doubleClick method
                                    clickCount=0;
                                    doubleClick = false;
                                    leftClick = false;
                                }

                            }else if (tripleClick) { 

                                System.out.println("Triple Click.");
                                clickCount++;
                                if(clickCount == 3) {
                                   tripleClick();  //your tripleClick method
                                    clickCount=0;
                                    tripleClick = false;
                                    leftClick = false;
                                }

                            } else if(leftClick) {                                      
                                System.out.println("single click.");
                                leftClick = false;
                            }
                        }               
                    });
                    timer.setRepeats(false);
                    timer.start();
                    if(evt.getID()==MouseEvent.MOUSE_RELEASED) timer.stop();
        }           
  }


      public static void main(String[] argv) throws Exception {

        JTextField component = new JTextField();
        component.addMouseListener(new TestMouseListener());
        JFrame f = new JFrame();

        f.add(component);
        f.setSize(300, 300);
        f.setVisible(true);

        component.addMouseListener(new TestMouseListener());
      }

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM