滑鼠動作監聽類MouseListener
阿新 • • 發佈:2019-02-16
<span style="font-size:14px;">import java.awt.event.ActionEvent ; import java.awt.event.ActionListener ; import java.awt.event.WindowListener ; import java.awt.event.WindowAdapter ; import java.awt.event.WindowEvent ; import java.awt.event.KeyEvent ; import java.awt.event.MouseEvent ; import java.awt.event.KeyAdapter ; import java.awt.event.KeyListener ; import java.awt.event.MouseListener ; import javax.swing.JTextArea ; import javax.swing.JFrame ; import javax.swing.JScrollPane ; import javax.swing.JPasswordField ; class MyMouse extends JFrame implements MouseListener { JTextArea text = new JTextArea() ; public MyMouse() { super.setTitle("滑鼠精靈") ; JScrollPane scr = new JScrollPane(text) ; scr.setBounds(5,5,300,200) ; super.add(scr) ; text.addMouseListener(this) ; super.addWindowListener(new WindowAdapter() { public void WindowClosing(WindowEvent e) { System.exit(1) ; } }) ; super.setSize(400,300) ; super.setVisible(true) ; } public void mouseClicked(MouseEvent e) //得到滑鼠鍵值 { int c = e.getButton() ; String mouseinfo = null; if(c==MouseEvent.BUTTON1) { mouseinfo = "左鍵\n" ; } if(c==MouseEvent.BUTTON2) { mouseinfo = "滾輪\n" ; } if(c==MouseEvent.BUTTON3) { mouseinfo = "右鍵\n" ; } text.append("被按下的是"+mouseinfo) ; } 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") ; } } ; class Tester { public static void main(String args[]) { MyMouse mhk = new MyMouse() ; } } ; </span>