這是一個(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);
}
讓我們來(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();
}
}
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注