Java获取窗口鼠标坐标以及键盘

  1、使用MouseListener借口处理鼠标事件

  鼠标事件有5种:按下鼠标键,释放鼠标键,点击鼠标键,鼠标进入和鼠标退出

  鼠标事件类型是MouseEvent,主要方法有:

  getX(),getY() 获取鼠标位置

  getModifiers() 获取鼠标左键或者右键

  getClickCount() 获取鼠标被点击的次数

  getSource() 获取鼠标发生的事件源


  事件源获得监视器的方法是addMouseListener(),移去监视器的方法是removeMouseListener()

  处理事件源发生的时间的事件的接口是MouseListener 接口中有如下的方法

  mousePressed(MouseEvent) 负责处理鼠标按下事件

  mouseReleased(MouseEvent) 负责处理鼠标释放事件

  mouseEntered(MouseEvent) 负责处理鼠标进入容器事件

  mouseExited(MouseEvent) 负责处理鼠标离开事件

  mouseClicked(MouseEvent) 负责处理点击事件

  

2、使用MouseMotionListener接口处理鼠标事件

  事件源发生的鼠标事件有2种:拖动鼠标和鼠标移动

  鼠标事件的类型是MouseEvent

  事件源获得监视器的方法是addMouseMotionListener()

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

  mouseDragged() 负责处理鼠标拖动事件

  mouseMoved() 负责处理鼠标移动事件

  3、控制鼠标的指针形状

  setCursor(Cursor.getPreddfinedCursor(Cursor.鼠标形状定义)) 鼠标形状定义见(书 P 210)

  4、键盘事件

  键盘事件源使用addKeyListener 方法获得监视器

  键盘事件的接口是KeyListener 接口中有3个方法

  public void keyPressed(KeyEvent e) 按下键盘按键

  public void keyReleased(KeyEvent e) 释放键盘按键

  public void keyTypde(KeyEvent e) 按下又释放键盘按键

  package mouseenvent;

  import java.awt.*;

  import javax.swing.*;

  import java.awt.event.*;

  public class Mouse extends JFrame implements MouseListener{

  JPanel jp = new JPanel();

  JLabel statusbar = new JLabel();

  static final long serialVersionUID=0;


public Mouse(){

  setTitle("Mouse Event");

  getContentPane().add(jp,BorderLayout.CENTER);

  getContentPane().add(statusbar, BorderLayout.SOUTH);

  jp.addMouseListener(this);

  }

  public static void main(String[] args){

  Mouse frame = new Mouse();

  frame.setSize(500,500);

  frame.setVisible(true);

  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  public void mousePressed(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

  String s = "鼠标在坐标"+"("+x+","+y+")"+"处按下";

  statusbar.setText(s);

  }

  public void mouseReleased(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

  String s = "鼠标在坐标"+"("+x+","+y+")"+"处释放";

  statusbar.setText(s);

  }

  public void mouseClicked(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

  String s = "鼠标在坐标"+"("+x+","+y+")"+"处点击";

  statusbar.setText(s);

  }

  public void mouseEntered(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

  String s = "鼠标在坐标"+"("+x+","+y+")"+"处进入";

  statusbar.setText(s);

  }

  public void mouseExited(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

  String s = "鼠标在坐标"+"("+x+","+y+")"+"处离开";

  statusbar.setText(s);

  }

  }

 package mouseenvent;

  import java.awt.*;

  import javax.swing.*;

  import java.awt.event.*;

  public class MouseMotion extends JFrame implements MouseMotionListener{

  static final long serialVersionUID=0;

  JLabel bar = new JLabel();

  JTextField text = new JTextField();

  public MouseMotion(){

  setTitle("Mouse Event");

  getContentPane().add(text,BorderLayout.NORTH);

  getContentPane().add(bar, BorderLayout.SOUTH);

  addMouseMotionListener(this);

  }

  public void mousePressed(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

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

  bar.setText(s);

  text.setText(s);

  }

  public void mouseDragged(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

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

  bar.setText(s);

  text.setText(s);

  }

  public void mouseMoved(MouseEvent e){

  int x = e.getX();

  int y = e.getY();

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

  bar.setText(s);

  text.setText(s);

  }

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

  MouseMotion mouse = new MouseMotion();

  mouse.setSize(500,500);

  mouse.setVisible(true);

  mouse.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

  }

  }

 package mouseenvent;

  import java.awt.event.*;

  import javax.swing.*;

  import java.awt.*;

  public class Key extends JFrame implements KeyListener{

  JLabel bar ;

  public Key(){

  bar = new JLabel();

  getContentPane().add(bar,BorderLayout.CENTER);

  addKeyListener(this);

  }

  public void keyPressed(KeyEvent e){

  char c = e.getKeyChar();

  String s = c+"" ;

  bar.setText(s);

  }

  public void keyReleased(KeyEvent e){

  char c = e.getKeyChar();

  }

  public void keyTyped(KeyEvent e){

  char c = e.getKeyChar();

  }

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

  Key key = new Key();

阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

加入CSDN,享受更精准的内容推荐,与500万程序员共同成长!
关闭
关闭