当前位置: 技术问答>java相关
请问如何在双击的时候不触发单击事件?谢谢
来源: 互联网 发布时间:2015-09-18
本文导语: 如题 | 我试了一下,看来是激发两次事件,所以设置标志是没用的,但是可以通过时间延迟判断是否是双击,其实我觉得系统也是通过判断两次单击的时间间隔来确定是否是双击,但是具体两...
如题
|
我试了一下,看来是激发两次事件,所以设置标志是没用的,但是可以通过时间延迟判断是否是双击,其实我觉得系统也是通过判断两次单击的时间间隔来确定是否是双击,但是具体两次双击之间事件间隔是多少我就不清楚了,但是这样一来可能会出现单击时事件处理会有点延迟,再就是你这种处理好象有点不太符合常规,因为一般除了处理双击时不会出现单击的处理,我觉得你最好还是改一下方案:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame3 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
long oneClick = 0;
long doubleClick = 0;
//Construct the frame
public Frame3() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame3.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton1_mouseClicked(e);
}
});
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
this_mousePressed(e);
}
});
contentPane.add(jButton1, new XYConstraints(118, 102, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_mouseClicked(MouseEvent e) {
this.doubleClick = e.getWhen();
System.out.println(oneClick);
System.out.println(doubleClick);
if((this.doubleClick - this.oneClick)
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
public class Frame3 extends JFrame {
JPanel contentPane;
XYLayout xYLayout1 = new XYLayout();
JButton jButton1 = new JButton();
long oneClick = 0;
long doubleClick = 0;
//Construct the frame
public Frame3() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
}
//Component initialization
private void jbInit() throws Exception {
//setIconImage(Toolkit.getDefaultToolkit().createImage(Frame3.class.getResource("[Your Icon]")));
contentPane = (JPanel) this.getContentPane();
jButton1.setText("jButton1");
jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
public void mouseClicked(MouseEvent e) {
jButton1_mouseClicked(e);
}
});
contentPane.setLayout(xYLayout1);
this.setSize(new Dimension(400, 300));
this.setTitle("Frame Title");
this.addMouseListener(new java.awt.event.MouseAdapter() {
public void mousePressed(MouseEvent e) {
this_mousePressed(e);
}
});
contentPane.add(jButton1, new XYConstraints(118, 102, -1, -1));
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
void jButton1_mouseClicked(MouseEvent e) {
this.doubleClick = e.getWhen();
System.out.println(oneClick);
System.out.println(doubleClick);
if((this.doubleClick - this.oneClick)