熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

Java獲取窗口鼠標坐標以及鍵盤按鍵

2022-06-13   來源: Java核心技術 

  Java中的鼠標和鍵盤事件

  使用MouseListener借口處理鼠標事件

  鼠標事件有按下鼠標鍵釋放鼠標鍵點擊鼠標鍵鼠標進入和鼠標退出

  鼠標事件類型是MouseEvent主要方法有

  getX()getY() 獲取鼠標位置

  getModifiers() 獲取鼠標左鍵或者右鍵

  getClickCount() 獲取鼠標被點擊的次數

  getSource() 獲取鼠標發生的事件源

  事件源獲得監視器的方法是addMouseListener()移去監視器的方法是removeMouseListener()

  處理事件源發生的時間的事件的接口是MouseListener 接口中有如下的方法

  mousePressed(MouseEvent) 負責處理鼠標按下事件

  mouseReleased(MouseEvent) 負責處理鼠標釋放事件

  mouseEntered(MouseEvent) 負責處理鼠標進入容器事件

  mouseExited(MouseEvent) 負責處理鼠標離開事件

  mouseClicked(MouseEvent) 負責處理點擊事件

  使用MouseMotionListener接口處理鼠標事件

  事件源發生的鼠標事件有拖動鼠標和鼠標移動

  鼠標事件的類型是MouseEvent

  事件源獲得監視器的方法是addMouseMotionListener()

  處理事件源發生的事件的接口是MouseMotionListener 接口中有如下的方法

  mouseDragged() 負責處理鼠標拖動事件

  mouseMoved() 負責處理鼠標移動事件

  控制鼠標的指針形狀

  setCursor(CursorgetPreddfinedCursor(Cursor鼠標形狀定義)) 鼠標形狀定義見(書 P

  鍵盤事件

  鍵盤事件源使用addKeyListener 方法獲得監視器

  鍵盤事件的接口是KeyListener 接口中有個方法

  public void keyPressed(KeyEvent e) 按下鍵盤按鍵

  public void keyReleased(KeyEvent e) 釋放鍵盤按鍵

  public void keyTypde(KeyEvent e) 按下又釋放鍵盤按鍵

  package mouseenvent;

  import javaawt*;

  import javaxswing*;

  import javaawtevent*;

  public class Mouse extends JFrame implements MouseListener{

  JPanel jp = new JPanel();

  JLabel statusbar = new JLabel();

  static final long serialVersionUID=;

  public Mouse(){

  setTitle(Mouse Event);

  getContentPane()add(jpBorderLayoutCENTER);

  getContentPane()add(statusbar BorderLayoutSOUTH);

  jpaddMouseListener(this);

  }

  public static void main(String[] args){

  Mouse frame = new Mouse();

  framesetSize();

  framesetVisible(true);

  framesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  }

  public void mousePressed(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = 鼠標在坐標+(+x++y+)+處按下;

  statusbarsetText(s);

  }

  public void mouseReleased(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = 鼠標在坐標+(+x++y+)+處釋放;

  statusbarsetText(s);

  }

  public void mouseClicked(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = 鼠標在坐標+(+x++y+)+處點擊;

  statusbarsetText(s);

  }

  public void mouseEntered(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = 鼠標在坐標+(+x++y+)+處進入;

  statusbarsetText(s);

  }

  public void mouseExited(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = 鼠標在坐標+(+x++y+)+處離開;

  statusbarsetText(s);

  }

  }

  package mouseenvent;

  import javaawt*;

  import javaxswing*;

  import javaawtevent*;

  public class MouseMotion extends JFrame implements MouseMotionListener{

  static final long serialVersionUID=;

  JLabel bar = new JLabel();

  JTextField text = new JTextField();

  public MouseMotion(){

  setTitle(Mouse Event);

  getContentPane()add(textBorderLayoutNORTH);

  getContentPane()add(bar BorderLayoutSOUTH);

  addMouseMotionListener(this);

  }

  public void mousePressed(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = x = +x+y = +y;

  barsetText(s);

  textsetText(s);

  }

  public void mouseDragged(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = x = +x+ y = +y;

  barsetText(s);

  textsetText(s);

  }

  public void mouseMoved(MouseEvent e){

  int x = egetX();

  int y = egetY();

  String s = x = +x+ y = +y;

  barsetText(s);

  textsetText(s);

  }

  public static void main(String args[])throws Exception{

  MouseMotion mouse = new MouseMotion();

  mousesetSize();

  mousesetVisible(true);

  mousesetDefaultCloseOperation(JFrameEXIT_ON_CLOSE);

  }

  }

  package mouseenvent;

  import javaawtevent*;

  import javaxswing*;

  import javaawt*;

  public class Key extends JFrame implements KeyListener{

  JLabel bar ;

  public Key(){

  bar = new JLabel();

  getContentPane()add(barBorderLayoutCENTER);

  addKeyListener(this);

  }

  public void keyPressed(KeyEvent e){

  char c = egetKeyChar();

  String s = c+ ;

  barsetText(s);

  }

  public void keyReleased(KeyEvent e){

  char c = egetKeyChar();

  }

  public void keyTyped(KeyEvent e){

  char c = egetKeyChar();

  }

  public static void main(String args[])throws Exception{

  Key key = new Key();

  keysetSize();

  keysetVisible(true);

  keysetDefaultCloseOperation(JFrameDISPOSE_ON_CLOSE);

  }

  }


From:http://tw.wingwit.com/Article/program/Java/hx/201311/25830.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.