• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

Java Swing中使用双击事件

发布: 2007-7-04 13:34 | 作者: admin | 来源:  网友评论 | 查看: 12次 | 进入软件测试论坛讨论

领测软件测试网 今天自己写个小程序碰到了这个问题,在网上搜了一下人家的思路,整理了一下代码可以运行,感觉不错!
直接在JBuilder中新建一个应用程序,至于具体步骤就不描述了,此代码没有main函数无法直接运行。下面我贴出在Frame1中的代码:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import com.borland.jbcl.layout.*;
import java.util.Date;

public class Frame1
extends JFrame {
JPanel contentPane;
BorderLayout borderLayout1 = new BorderLayout();
JPanel jPanel1 = new JPanel();
XYLayout xYLayout1 = new XYLayout();
JScrollPane jScrollPane1 = new JScrollPane();
JList jList1 = new JList();
//初始化的JList中的数据
String[] strData = {
"One", "Tow", "Three"};
//保存点击按钮的时间
long clickTime = 0;

//Construct the frame
public Frame1() {
enableEvents(AWTEvent.WINDOW_EVENT_MASK);
try {
jbInit();
}
catch (Exception e) {
e.printStackTrace();
}
}

//Component initialization
private void jbInit() throws Exception {
contentPane = (JPanel)this.getContentPane();
contentPane.setLayout(borderLayout1);
this.setSize(new Dimension(532, 468));
this.setTitle("Frame Title");

jPanel1.setLayout(xYLayout1);
jList1.addMouseListener(new Frame1_jList1_mouseAdapter(this));
contentPane.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(jScrollPane1, new XYConstraints(18, 34, 209, 326));
jScrollPane1.getViewport().add(jList1, null);
jList1.setListData(strData);
}

//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);
}
}

//具体实现按钮双击的功能的方法, 很简单的算法,不做解释了
public boolean checkClickTime() {
long nowTime = (new Date()).getTime();

if ( (nowTime - clickTime) < 300) {
clickTime = nowTime;
return true;
}
clickTime = nowTime;
return false;

}

void jList1_mouseReleased(MouseEvent e) {
//判断是否双击,是的话写你要实现的功能
if (checkClickTime()) {
System.out.println("Click Double");

}
}
}

class Frame1_jList1_mouseAdapter
extends java.awt.event.MouseAdapter {
Frame1 adaptee;

Frame1_jList1_mouseAdapter(Frame1 adaptee) {
this.adaptee = adaptee;
}

public void mouseReleased(MouseEvent e) {
adaptee.jList1_mouseReleased(e);
}
}


延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网