這裏是你如何可以通過繼承的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();
}
}
}
它加速了。
希望有所幫助。
沒有簡單的方法。我認爲你用一個定時執行的線程勾畫的輪詢按鈕是唯一的方法。 – 2008-11-21 18:22:15