小技巧:Java Swing中使用双击事件
来源:互联网 发布:mac辣椒红试色 编辑:程序博客网 时间:2024/05/01 10:34
直接在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);
}
}
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);
}
}
- 小技巧:Java Swing中使用双击事件
- Java Swing中使用双击事件
- Swing之table中响应鼠标双击(单击)事件
- java swing鼠标双击和右键事件处理
- Java Swing JList 添加双击事件(Double Click)
- java Swing的鼠标双击事件如何响应
- Java Swing中的键盘事件处理技巧
- [android开发中使用的小技巧]eclipse双击变量高亮显示开关
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- java小程序中鼠标事件的响应处理(包含右键、双击等)
- 如何在java中实现双击事件
- Java Swing中键盘事件的处理
- Java Swing使用注解绑定事件监听
- AWT/Swing中处理鼠标双击(多击)以及右键点击的事件处理办法
- 博为峰Java技术文章 ——JavaSE Swing列表框双击事件的处理Ⅰ
- 博为峰Java技术文章 ——JavaSE Swing列表框双击事件的处理Ⅱ
- 对Java 中Swing的小总结
- Java使用小技巧
- 怎样去获取文章简介
- Java2下Applet数字签名具体实现方法
- Asp.Net 2.0 中回发或回调参数无效的解决方案
- 注射器,救命的注射器
- Applet调用JavaScript函数
- 小技巧:Java Swing中使用双击事件
- MAPI,SAPI,and TAPI Developer's Guide(NO.1)
- 用Java Swing作一个日历控制程序
- 《javascript高级程序设计》学习笔记(二 ):javascript 不常用语法拾遗
- JDBC基础-从应用程序创建applet
- 电话、手机通信开发的TAPI笔记
- Applet做数字签名,授予访问本地资源
- Mount your widows Partitions and make it read/writable in ubuntu
- 一些有用的网站