2008-11-21 55 views
5

我正在使用Swing創建觸摸屏應用程序,並請求更改其中一個按鈕,以便按鈕按下時它的行爲就像鍵盤一樣。 (首先,我不確定觸摸屏將允許用戶「按住」按鈕,但假裝他們現在可以)如何讓JButton在按下時重複動作?

我要走的路上開始當調用mousePressed,然後在調用mouseReleased時結束循環。這將涉及啓動一個線程並且必須處理同步以及invokeLater()以使事件回到EventQueue

有沒有一個非常簡單的方法來做我想要的?我希望我只是沒有看到API來做到這一點。

+0

沒有簡單的方法。我認爲你用一個定時執行的線程勾畫的輪詢按鈕是唯一的方法。 – 2008-11-21 18:22:15

回答

5

我會做這樣的:

  • 聽和的mousePressed安排java.util.Timer中在稍後時間推出。
  • 計時器執行操作並將其自身設置爲再次計劃。
  • 收聽mouseReleased以取消定時器。
0

我去了java.swing.Timer,因爲它會自動回發到Swing EventQueue,這就是我正在尋找的。謝謝您的幫助。

0

這裏是你如何可以通過繼承的JButton做到這一點:

import java.awt.Font; 
import java.awt.event.ActionEvent; 
import java.awt.event.ActionListener; 
import java.awt.event.MouseEvent; 
import java.awt.event.MouseListener; 

import javax.swing.Action; 
import javax.swing.BorderFactory; 
import javax.swing.JButton; 
import javax.swing.SwingUtilities; 

public class TypomaticButton extends JButton implements MouseListener { 
    private boolean autotype = false; 
    private static Thread theThread = null; 
    private String myName = "unknown"; 
    private int 
     speed = 150, 
     wait = 300, 
     decrement = (wait - speed)/10; 

    TypomaticButton(Action action){ 
     super(action); 
     myName = action.getValue(Action.NAME).toString(); 
     addMouseListener(this); 
    } 

    TypomaticButton(String text){ 
     super(text); 
     setBorder(BorderFactory.createEmptyBorder(6, 6, 6, 6)); 

     myName = text; 
     addMouseListener(this); 
    } 

    @Override 
    public void mouseClicked(MouseEvent arg0) {} 

    @Override 
    public void mouseEntered(MouseEvent arg0) { } 

    @Override 
    public void mouseExited(MouseEvent arg0) { } 

    @Override 
    public void mousePressed(MouseEvent arg0) { 
     autotype = true; 
     theThread = new Thread(new Runnable() { // do it on a new thread so we don't block the UI thread 
      @Override 
      public void run() { 
       for (int i = 10000; i > 0 && autotype; i--) { // don't go on for ever 
        try { 
         Thread.sleep(wait);  // wait awhile 
        } catch (InterruptedException e) { 
         break; 
        } 
        if(wait != speed){ 
         wait = wait - decrement;  // gradually accelerate to top speed 
         if(wait < speed) 
          wait = speed; 
        } 
        SwingUtilities.invokeLater(new Runnable() { // run this bit on the UI thread 
         public void run() { 
          if(!autotype) // it may have been stopped meanwhile 
           return; 
          ActionListener[] als = getActionListeners(); 
          for(ActionListener al : als){ // distribute to all listeners 
           ActionEvent aevent = new ActionEvent(getClass(), 0, myName); 
           al.actionPerformed(aevent); 
          } 
         } 
        }); 
       } 
       autotype = false; 
      } 
     }); 
     theThread.start(); 
    } 

    @Override 
    public void mouseReleased(MouseEvent arg0) { 
     autotype = false; 
     wait = 300; 
    } 

    void speed(int millisecs){ 
     speed = millisecs; 
     decrement = (wait - speed)/10; 
    } 

    void stop(){ 
     autotype = false; 
     if(theThread != null){ 
      theThread.interrupt(); 
     } 
    } 

} 

它加速了。

希望有所幫助。

相關問題