直接在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);
}
}
小技巧:Java Swing中使用雙擊事件
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章
Applet調用JavaScript函數
wangyongligood
2020-02-20 12:57:10
Java2下Applet數字簽名具體實現方法
wangyongligood
2020-02-20 12:57:10
Applet調用JavaScript函數
wangyongligood
2020-02-20 12:57:10
Java2下Applet數字簽名具體實現方法
wangyongligood
2020-02-20 12:57:10