免费观看又色又爽又黄的小说免费_美女福利视频国产片_亚洲欧美精品_美国一级大黄大色毛片

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

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

崇仁網(wǎng)站建設(shè)公司成都創(chuàng)新互聯(lián),崇仁網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為崇仁1000+提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\外貿(mào)網(wǎng)站制作要多少錢,請(qǐng)找那個(gè)售后服務(wù)好的崇仁做網(wǎng)站的公司定做!

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

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

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

 mouseMoved(MouseEvent e) 處理鼠標(biāo)移動(dòng)事件的方法 mouseDragged(MouseEvent e) 處理鼠標(biāo)拖動(dòng)事件的方法 鼠標(biāo)事件處理 兩個(gè)鼠標(biāo)事件監(jiān)聽器中的方法都定義了MouseEvent類型的形參,MouseEvent類是鼠標(biāo)事件類,是被監(jiān)聽器捕獲的用戶操作所生成的事件對(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() 通過將事件坐標(biāo)加上指定x,y偏移量,將事件坐標(biāo)平移到新位置 以下代碼,演示了兩個(gè)接口的作用,通過讀代碼,就會(huì)理解到各自方法的作用:

 
import javax.swing.*; 
import java.awt.event.*; 
 
public class MyMouse extends JFrame { 
    public JLabel jl = new JLabel("鼠標(biāo)暫無操作"); 
 
    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)離開了窗體界面"); 
            } 
 
            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類,組件在界面中,可以拖動(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); 
    } 
 

 

當(dāng)前標(biāo)題:鼠標(biāo)事件監(jiān)聽器的創(chuàng)建和使用
本文鏈接:http://m.newbst.com/article0/gdcsio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供虛擬主機(jī)、網(wǎng)站設(shè)計(jì)公司、企業(yè)建站、域名注冊(cè)、網(wǎng)站制作網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站