Java Swing介面編程(25)---事件處理:滑鼠事件及監聽處理

來源:互聯網
上載者:User

標籤:監聽   article   介面   sys   ati   dem   jsb   csdn   click   

假設想對一個滑鼠的操作進行監聽,假設滑鼠按下、鬆開等。則能夠使用MouseListener介面。

package com.beyole.util;import java.awt.event.MouseEvent;import java.awt.event.MouseListener;import java.awt.event.WindowAdapter;import java.awt.event.WindowEvent;import javax.swing.JFrame;import javax.swing.JScrollPane;import javax.swing.JTextArea;class MyMouseHandle extends JFrame implements MouseListener {private JTextArea text = new JTextArea();public MyMouseHandle() {super.setTitle("Crystal");// 設定標題JScrollPane pane = new JScrollPane(text);// 增加滾動欄pane.setBounds(5, 5, 300, 200);// 設定絕對位置super.add(pane);// 向視窗中增加組件text.addMouseListener(this);// 增加mouse監聽super.setSize(310, 210);super.setVisible(true);super.addWindowListener(new WindowAdapter() {public void windowClosing(WindowEvent arg0) {System.exit(1);}});}public void mouseClicked(MouseEvent e)// 按一下滑鼠事件{int c = e.getButton();// 得到按下的滑鼠鍵String mouseInfo = null;// 接收資訊if (c == MouseEvent.BUTTON1)// 推斷是滑鼠左鍵按下{mouseInfo = "左鍵";} else if (c == MouseEvent.BUTTON3) {// 推斷是滑鼠右鍵按下mouseInfo = "右鍵";} else {mouseInfo = "滾軸";}text.append("按一下滑鼠:" + mouseInfo + ".\n");}public void mouseEntered(MouseEvent e)// 滑鼠進入組件{text.append("滑鼠進入組件.\n");}public void mouseExited(MouseEvent e)// 滑鼠退出組件{text.append("滑鼠退出組件.\n");}public void mousePressed(MouseEvent e)// 滑鼠按下{text.append("滑鼠按下.\n");}public void mouseReleased(MouseEvent e)// 滑鼠鬆開{text.append("滑鼠鬆開.\n");}}public class MyMouseEventDemo {public static void main(String[] args) {new MyMouseHandle();}}

程式:

Java Swing介面編程(25)---事件處理:滑鼠事件及監聽處理

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.