国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

Java基礎(chǔ):看看流行的和過(guò)時(shí)的鼠標(biāo)事件

2019-11-18 15:34:57
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友
MouseListener方法接口中的mouseClicked()方法和mousePRessed()方法有什么區(qū)別?

這是一個(gè)熱門的話題,讓我們來(lái)深入的找尋答案,java’s AWT庫(kù)提供了兩個(gè)接口來(lái)監(jiān)聽(tīng)和接收鼠標(biāo)事件。一個(gè)是java.awt.event.MouseListener: 

public interface MouseListener extends EventListener

{

public void mousePressed(MouseEvent e);

public void mouseReleased(MouseEvent e);

public void mouseClicked(MouseEvent e);

public void mouseEntered(MouseEvent e);

public void mouseExited(MouseEvent e);  

}

另一個(gè)是 java.awt.event.MouseMotionListener:  

public interface MouseMotionListener extends EventListener

{

public void mouseDragged(MouseEvent e);

public void mouseMoved(MouseEvent e);

} 



Swing 提供了 MouseInputListener,這個(gè)接口的同時(shí)擴(kuò)展了MouseMotionListener和MouseListener 接口。

讓我們來(lái)看看這些接口中的方法:

◆mousePressed() 當(dāng)用戶按下鼠標(biāo)按鈕時(shí)發(fā)生。  

◆mouseReleased() 當(dāng)用戶松開(kāi)鼠標(biāo)按鈕時(shí)發(fā)生。

◆mouseClicked() 當(dāng)用戶按下并松開(kāi)鼠標(biāo)按鈕時(shí)發(fā)生。用戶在選擇或雙擊圖標(biāo)的時(shí)候通常會(huì)點(diǎn)擊鼠標(biāo)按鈕。 用戶假如在松開(kāi)鼠標(biāo)之前移動(dòng)鼠標(biāo),點(diǎn)擊不會(huì)導(dǎo)致鼠標(biāo)相應(yīng)事件出現(xiàn)。  

◆因?yàn)辄c(diǎn)擊鼠標(biāo)是按下鼠標(biāo)和松開(kāi)鼠標(biāo)的結(jié)合,在事件分配給 mouseClicked() 方法之前,mousePressed() 和 mouseReleased() 方法已同時(shí)被調(diào)用。  

◆mouseEntered() 當(dāng)鼠標(biāo)離開(kāi)當(dāng)前組件并進(jìn)入你所監(jiān)聽(tīng)的組件時(shí)激活事件。

◆mouseExited() 當(dāng)鼠標(biāo)離開(kāi)你所監(jiān)聽(tīng)的組件時(shí)發(fā)生。  

◆mouseDragged() 當(dāng)用戶按下鼠標(biāo)按鈕并在松開(kāi)之前進(jìn)行移動(dòng)時(shí)發(fā)生。在mouseDragged() 后松開(kāi)鼠標(biāo)不會(huì)導(dǎo)致mouseClicked()。  

◆mouseMoved() 當(dāng)鼠標(biāo)在組件上移動(dòng)而 不時(shí)拖動(dòng)時(shí)發(fā)生。  

要監(jiān)聽(tīng)鼠標(biāo)事件就必須調(diào)用這些接口之一,或擴(kuò)展一個(gè)鼠標(biāo)適配器(mouse adapters) 類。AWT 提供了兩種監(jiān)聽(tīng)適配器(listener adapters): java.awt.event.MouseAdapter 和java.awt.event.MouseMotionAdapter。

Swing 為MouseInputListener提供了一個(gè)叫做javax.swing.event.MouseInputAdapter的適配器。有了適配器你就不必調(diào)用接口中每個(gè)方法了。 取而代之,你只需簡(jiǎn)單的擴(kuò)展適配器并重寫你要監(jiān)聽(tīng)的方法就可以了。

假如你還是很迷惑,再看看API 并寫個(gè)小測(cè)試程序。 有時(shí)嘗試?yán)斫獠煌录淖詈梅椒╠ifferent events。我寫了一個(gè)小小的應(yīng)用程序來(lái)表現(xiàn)兩種鼠標(biāo)事件的區(qū)別。這里是完整的代碼:

import java.awt.*;

import java.awt.event.*;

public class MouseTest extends Frame

{

public MouseTest()

{

Button b = new Button("JavaWorld JavaQ&A");

b.addActionListener( new ActionListener() {

public void actionPerformed(ActionEvent e)

{

System.exit(0);

}

}

);

add(b,BorderLayout.NORTH);

addMouseListener(new MouseTest.MouseHandler());

addMouseMotionListener(new MouseTest.MouseMotionHandler());

}

// MouseHandler is an inner class that implements the MouseListener.

// Each method simply prints out a message to the command line.

private class MouseHandler implements MouseListener

{

public void mousePressed(MouseEvent e)

{

System.out.println("mouse pressed");

}

public void mouseClicked(MouseEvent e)

{

System.out.println("moused clicked");

}

public void mouseReleased(MouseEvent e)

{

System.out.println("mouse released");

}

public void mouseEntered(MouseEvent e)

{

System.out.println("mouse entered");

}

public void mouseExited(MouseEvent e)

{

System.out.println("mouse exited");

}

}

// MouseMotionHandler is an inner class that implements the MouseMotionListener.

// Each method simply prints out a message to the command line.

private class MouseMotionHandler implements MouseMotionListener

{

public void mouseMoved(MouseEvent e)

{

System.out.println("mouse moved");

}

public void mouseDragged(MouseEvent e)

{

System.out.println("mouse dragged");

}

}

public static void main(String[] args)

{

new MouseTest().show();

}

}



這個(gè)應(yīng)用程序建立了一個(gè)窗口和一個(gè)按鈕。當(dāng)你移動(dòng)鼠標(biāo)并按下按鈕時(shí),應(yīng)用程序就會(huì)在命令行打印出文本消息,讓你看到鼠標(biāo)響應(yīng)和事件間的對(duì)應(yīng)關(guān)系。  



發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 改则县| 郁南县| 乌兰察布市| 东兴市| 政和县| 兰西县| 莱西市| 永靖县| 治县。| 巩义市| 东宁县| 建水县| 南汇区| 镇赉县| 长乐市| 桑日县| 通海县| 逊克县| 汨罗市| 平遥县| 林甸县| 杭锦旗| 南漳县| 泸定县| 永嘉县| 囊谦县| 亳州市| 汪清县| 外汇| 西乌珠穆沁旗| 板桥市| 南京市| 长垣县| 诸城市| 马边| 潢川县| 寿光市| 中阳县| 卢龙县| 庆城县| 阳原县|