gpt4 book ai didi

java - 用于检测三次单击而不触发双击或单击的通用类

转载 作者:行者123 更新时间:2023-11-29 03:12:15 25 4
gpt4 key购买 nike

编辑——根据已接受的答案更正的代码显示在这篇文章的底部。

我找到了一种方法 ( written by Andremoniy ) 来捕捉双击和三次点击而不触发单击和双击。它非常适合我的目的。

我对其进行了修改,以便实现可以调整点击间隔。这也很好用。

我的修改包括使它成为一个抽象类,定义实现必须定义的 5 个 abstract 方法(single、double、triple 和“many”的方法“点击次数以及点击间隔调整器)。

这是修改后的版本(println 语句具有指导意义):

public abstract class Click123 extends JPanel  
{
public abstract void singleClick();
public abstract void doubleClick();
public abstract void tripleClick();
public abstract void manyClick();
public abstract int getFreq();

public Click123()
{
addMouseListener
(
new MouseAdapter()
{
Thread cp = null;

public void mouseClicked(MouseEvent e)
{
if (cp != null && cp.isAlive())
cp.interrupt();

if (e.getClickCount() == 1)
{
cp = new Thread(new ClickProcessor(new Callable<Void>() {
@Override public Void call() throws Exception {
singleClick();
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 2)
{
cp = new Thread(new ClickProcessor(new Callable<Void>() {
@Override public Void call() throws Exception {
doubleClick();
return null;
}
}));
cp.start();
}
else if (e.getClickCount() == 3)
{
cp = new Thread(new ClickProcessor(new Callable<Void>()
{
@Override public Void call() throws Exception {
tripleClick();
return null;
}
})
);
cp.start();
}
else manyClick();
} // mouseClicked
} // new MouseAdapter
); // add mouseListener
} // Click123

class ClickProcessor implements Runnable
{
Callable<Void> eventProcessor;

ClickProcessor(Callable<Void> eventProcessor)
{
this.eventProcessor = eventProcessor;
}

@Override public void run()
{
try
{
Thread.sleep(getFreq());
eventProcessor.call();
} catch (InterruptedException e) { System.out.println(e);}
catch (Exception e) { System.out.println(e);}
} // run
} // class ClickProcessor
} // class Click123

实现方案如下:

public class NewMain1 {
static int INITIAL_CLICK_FREQUENCY = ((Integer)Toolkit.getDefaultToolkit()
.getDesktopProperty("awt.multiClickInterval"));

public static int CLICK_FREQUENCY;

static JSpinner spinner = new JSpinner();
static final JLabel ch = new JLabel("Click here to test");

public static void main(String[] args) {

CLICK_FREQUENCY = INITIAL_CLICK_FREQUENCY;
spinner = new JSpinner();
spinner.setModel(new SpinnerNumberModel(CLICK_FREQUENCY, 200, 900, 50));
spinner.addChangeListener(new ChangeListener() {
@Override
public void stateChanged(ChangeEvent e) {
CLICK_FREQUENCY = (int) spinner.getValue();
}
});

Click123 frame = new Click123(){
public void singleClick(){
JOptionPane.showMessageDialog(null,"Single click at " + CLICK_FREQUENCY);
}
public void doubleClick(){
JOptionPane.showMessageDialog(null,"Double click at " + CLICK_FREQUENCY);
}
public void tripleClick(){
JOptionPane.showMessageDialog(null,"Triple click at " + CLICK_FREQUENCY);
}
public void manyClick(){
JOptionPane.showMessageDialog(null,"Many clicks at " + CLICK_FREQUENCY);
}
public int getFreq(){
return CLICK_FREQUENCY;
}
};
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
frame.setLayout(new FlowLayout());
frame.setSize(500, 300);
ch.setBorder(new EtchedBorder());
frame.add(ch);
frame.add(spinner);
frame.setVisible(true);
} // main
}

如最初所写,Click123 扩展了 JFrame。我修改了它(通过将顶行更改为 extends JTextField)以使用 JTextField 类型的网格。然后我添加了一个按钮来动态更改点击间隔。所以现在我有三个相同的(除了扩展类型)巨大的代码块。

我的问题是:我如何修改 Click123 以便在不做任何改动的情况下使任何组件都可以识别单、双和三连击?

编辑——必要更改的摘要(对于我自己的应用程序,抽象 方法需要传递 MouseEvent 以确定哪个组件触发了点击):

类定义:

 public abstract class Click123<T extends Component>  
{
public abstract void singleClick(MouseEvent e); // same for 3 others
...

public Click123(T target)
{
target.addMouseListener
(
new MouseAdapter() ...
} ...
}

final 添加为修饰符以传递 MouseEvent:

    public void mouseClicked(final MouseEvent e) 

传递 MouseEvent:

    singleClick(e); // same for doubleClick and tripleClick and manyClick

接收 MouseEvent 以确定触发点击的组件:

  public void doubleClick(MouseEvent e)

实现:

frame = new JPanel();
new Click123(frame) {

最佳答案

泛型不是必需的。您只需将 MouseListener 添加到 Component

public abstract class Click123 // doesn't extend anything
{
public Click123(Component comp)
{
comp.addMouseListener(new MouseAdapter() {
// ...
}));
}
}

现在您可以只传递 JFrame,或您想要的任何 Component:

Click123 handler = new Click123(myJFrame){ 
public void singleClick(){
JOptionPane.showMessageDialog(null,"Single click at " + CLICK_FREQUENCY);
}
// Other implementing methods here
}

关于java - 用于检测三次单击而不触发双击或单击的通用类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28731675/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com