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);
}
}