import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
public abstract class ClickListener extends MouseAdapter {
private int count = 0;
public void onLongClick() {
}
public abstract void onClick();
private Timer timer;
public void mousePressed(MouseEvent e) {
timer = new Timer(500, new ActionListener() {
public void actionPerformed(ActionEvent e) {
count++;
if (count >= 10) {
// 长按事件
System.out.println("长按事件触发了");
onLongClick();
timer.stop();
}
}
});
timer.start();
}
public void mouseReleased(MouseEvent e) {
timer.stop();
if (count < 10) {
// 点击事件
System.out.println("单机事件触发了");
onClick();
}
count = 0;
}
}
/**
* 按钮
*/
public static JButton getJButton(int x, int y, int w, int h, String text, ClickListener actionListener) {
JButton label = new JButton(text);
label.setBounds(x, y, w, h);
label.addMouseListener(actionListener);
return label;
}