溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

鼠標(biāo)事件監(jiān)聽(tīng)器的創(chuàng)建和使用

發(fā)布時(shí)間:2020-07-05 05:49:41 來(lái)源:網(wǎng)絡(luò) 閱讀:402 作者:何小俊love 欄目:網(wǎng)絡(luò)安全

    鼠標(biāo)操作是圖形操作系統(tǒng)最常用操作,用戶使用鼠標(biāo)單擊,雙擊,右擊,拖動(dòng)等操作實(shí)現(xiàn)與軟件的交互。 鼠標(biāo)事件監(jiān)聽(tīng)器 鼠標(biāo)事件監(jiān)聽(tīng)器由MouseListener接口和MouseMotionListener接口定義,分別定義鼠標(biāo)捕獲不同的鼠標(biāo)操作方法。 MouseListener監(jiān)聽(tīng)器方法說(shuō)明 mouseClicked(MouseEvent e) 處理鼠標(biāo)單擊事件方法

mouseEntered(MouseEvent e) 鼠標(biāo)進(jìn)入組件區(qū)域時(shí)執(zhí)行方法 mouseExited(MouseEvent e) 鼠標(biāo)離開(kāi)組件區(qū)域執(zhí)行方法 mousePressed(MouseEvent e) 按下鼠標(biāo)按鍵時(shí)執(zhí)行方法 mouseRelease(MouseEvent e) 釋放鼠標(biāo)按鍵時(shí)執(zhí)行方法

MouseListener監(jiān)聽(tīng)器的方法,基本滿足大多數(shù)程序需求。

MouseMotionListener接口定義兩個(gè)有關(guān)鼠標(biāo)移動(dòng)和拖動(dòng)事件的處理方法。 MouseMotionListener監(jiān)聽(tīng)器方法說(shuō)明

 mouseMoved(MouseEvent e) 處理鼠標(biāo)移動(dòng)事件的方法 mouseDragged(MouseEvent e) 處理鼠標(biāo)拖動(dòng)事件的方法 鼠標(biāo)事件處理 兩個(gè)鼠標(biāo)事件監(jiān)聽(tīng)器中的方法都定義了MouseEvent類(lèi)型的形參,MouseEvent類(lèi)是鼠標(biāo)事件類(lèi),是被監(jiān)聽(tīng)器捕獲的用戶操作所生成的事件對(duì)象,該實(shí)例對(duì)象包含了許多鼠標(biāo)事件發(fā)生時(shí)的參數(shù)信息。例如鼠標(biāo)的坐標(biāo)位置,鼠標(biāo)的按鍵等。

常用方法有: getButton() 返回更改了狀態(tài)的鼠標(biāo)按鍵

getClickCount() 返回與此事件關(guān)聯(lián)的鼠標(biāo)單擊次數(shù)

getLocationOnScreen() 返回鼠標(biāo)相對(duì)于屏幕的絕對(duì)x,y坐標(biāo)

getPoint() 返回事件相對(duì)于源組件的x,y坐標(biāo)

translatePoint() 通過(guò)將事件坐標(biāo)加上指定x,y偏移量,將事件坐標(biāo)平移到新位置 以下代碼,演示了兩個(gè)接口的作用,通過(guò)讀代碼,就會(huì)理解到各自方法的作用:

 
import javax.swing.*; 
import java.awt.event.*; 
 
public class MyMouse extends JFrame { 
    public JLabel jl = new JLabel("鼠標(biāo)暫無(wú)操作"); 
 
    public MyMouse() { 
        setBounds(100, 100, 350, 80); 
        getContentPane().add("South", jl); 
        addMouseListener(new MouseListener() { 
 
            public void mouseClicked(MouseEvent arg0) { 
                jl.setText("鼠標(biāo)在界面中單擊了" + jl.getText() + arg0.getClickCount() 
                        + "次"); 
            } 
 
            public void mouseEntered(MouseEvent arg0) { 
                jl.setText("鼠標(biāo)進(jìn)入了窗體界面"); 
            } 
 
            public void mouseExited(MouseEvent arg0) { 
                jl.setText("鼠標(biāo)離開(kāi)了窗體界面"); 
            } 
 
            public void mousePressed(MouseEvent arg0) { 
                jl.setText("鼠標(biāo)在窗體界面中按下了鍵" + arg0.getButton()); 
            } 
 
            public void mouseReleased(MouseEvent arg0) { 
                jl.setText("鼠標(biāo)在窗體界面中釋放了鍵" + arg0.getButton()); 
            } 
 
        }); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
 
    public static void main(String[] args) { 
        MyMouse test = new MyMouse(); 
        test.setVisible(true); 
    } 
 

以下代碼,演示了MouseMotionListener類(lèi),組件在界面中,可以拖動(dòng):

 
import javax.swing.*; 
import java.awt.FlowLayout; 
import java.awt.event.*; 
 
public class MyMouse extends JFrame { 
    public JButton jb = new JButton("鼠標(biāo)可拖動(dòng)按鈕"); 
    public JTextField jt = new JTextField(); 
 
    public MyMouse() { 
        jb.setBounds(100, 100, 330, 175); 
        jt.setColumns(20); 
        setBounds(100, 100, 350, 280); 
        getContentPane().setLayout(new FlowLayout()); 
        getContentPane().add(jb); 
        getContentPane().add(jt); 
        addMouseMotionListener(new MouseMotionListener() { 
 
            /** 
             * 處理鼠標(biāo)拖動(dòng)事件 
             * */
 
            public void mouseDragged(MouseEvent arg0) { 
                mouseMoved(arg0); 
                jb.setLocation(arg0.getPoint()); 
            } 
 
            /** 
             * 處理鼠標(biāo)移動(dòng)事件 
             * */
 
            public void mouseMoved(MouseEvent arg0) { 
 
                jt.setText(arg0.getPoint().toString()); 
 
            } 
 
        }); 
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 
    } 
 
    public static void main(String[] args) { 
        MyMouse test = new MyMouse(); 
        test.setVisible(true); 
    } 
 

 


向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI