java

位置:IT落伍者 >> java >> 浏览文章

小技巧:Java Swing中使用双击事件


发布日期:2024年05月17日
 
小技巧:Java Swing中使用双击事件

直接在JBuilder中新建一个应用程序至于具体步骤就不描述了此代码没有main函数无法直接运行下面我贴出在Frame中的代码

import javaawt*;

import javaawtevent*;

import javaxswing*;

import comborlandjbcllayout*;

import javautilDate;

public class Frame

extends JFrame {

JPanel contentPane;

BorderLayout borderLayout = new BorderLayout();

JPanel jPanel = new JPanel();

XYLayout xYLayout = new XYLayout();

JScrollPane jScrollPane = new JScrollPane();

JList jList = new JList();

//初始化的JList中的数据

String[] strData = {

One Tow Three};

//保存点击按钮的时间

long clickTime = ;

//Construct the frame

public Frame() {

enableEvents(AWTEventWINDOW_EVENT_MASK);

try {

jbInit();

}

catch (Exception e) {

eprintStackTrace();

}

}

//Component initialization

private void jbInit() throws Exception {

contentPane = (JPanel)thisgetContentPane();

contentPanesetLayout(borderLayout);

thissetSize(new Dimension( ));

thissetTitle(Frame Title);

jPanelsetLayout(xYLayout);

jListaddMouseListener(new Frame_jList_mouseAdapter(this));

contentPaneadd(jPanel BorderLayoutCENTER);

jPaneladd(jScrollPane new XYConstraints( ));

jScrollPanegetViewport()add(jList null);

jListsetListData(strData);

}

//Overridden so we can exit when window is closed

protected void processWindowEvent(WindowEvent e) {

superprocessWindowEvent(e);

if (egetID() == WindowEventWINDOW_CLOSING) {

Systemexit();

}

}

//具体实现按钮双击的功能的方法很简单的算法不做解释了

public boolean checkClickTime() {

long nowTime = (new Date())getTime();

if ( (nowTime clickTime) < ) {

clickTime = nowTime;

return true;

}

clickTime = nowTime;

return false;

}

void jList_mouseReleased(MouseEvent e) {

//判断是否双击是的话写你要实现的功能

if (checkClickTime()) {

Systemoutprintln(Click Double);

}

}

}

class Frame_jList_mouseAdapter

extends javaawteventMouseAdapter {

Frame adaptee;

Frame_jList_mouseAdapter(Frame adaptee) {

thisadaptee = adaptee;

}

public void mouseReleased(MouseEvent e) {

adapteejList_mouseReleased(e);

}

}               

上一篇:Java Swing也惊艳之二:环环相套

下一篇:用JDOM完成Java更新XML文件