最近在搞java GUI和J数据库结合的应用程序,所以索性详细研究下窗口,鼠标,键盘事件。
写成文章,详细研究方法见下文。
====请各位路过者不吝留下你的文字=====
研究方法:
一:在JFrame上面放置一个JLabel,用于显示当前触发并相应的事件。
二:继承JRrame窗口类,实现事件监听接口 MouseMotionListener ,MouseListener, WindowListener,
KeyListener ,在 JFame上面添加窗口事件、鼠标 事件,键盘事件监听器。
三:重写(@Override )以上事件各个方法。
四:捕获方法,显示事件方法。
测试代码:
//Code Start
package GUI;
//import java.awt.event.KeyListener;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
import javax.swing.JLabel;
/*实现事件监听接口*/
public class JMessageDemo extends JFrame implements MouseMotionListener,MouseListener,WindowListener,KeyListener{
private JLabel lblCurEvent;
private String strEvent;
public JMessageDemo(){
CreateContent();
AddEventListen();
}
public void CreateContent(){
this.setSize(400, 300);
this.setLocation( this.getToolkit().getScreenSize().width / 2- this.getWidth() / 2,
this.getToolkit().getScreenSize().height/ 2 - this.getHeight() / 2);
this.setLayout(null);
lblCurEvent = new JLabel("");
lblCurEvent.setBackground(Color.orange);
lblCurEvent.setForeground(Color.blue);
lblCurEvent.setBounds(new Rectangle(60,100,200,30));
this.getContentPane().add(lblCurEvent);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
/*添加事件监听器*/
public void AddEventListen(){
//JFrame监听事件,若果是控件(JButton,JTextField……)x.添加事件
this.addMouseMotionListener(this);
this.addMouseListener(this);
this.addWindowListener(this);
this.addKeyListener(this);
}
/*在text上面显示当前事件*/
public void SetEventToLable(String strEvent){
lblCurEvent.setText(strEvent);
}
/*在Console上面显示当前事件*/
public void SetEventToConsole(String strEvent){
System.out.println("当前事件:" + strEvent);
}
/*窗口事件*/
@Override
public void windowActivated(WindowEvent e) {
strEvent = "WindowActivated!";
SetEventToConsole(strEvent);
}
@Override
public void windowClosed(WindowEvent e) {
strEvent = "WindowClosed!";
SetEventToConsole(strEvent);
}
@Override
public void windowClosing(WindowEvent e) {
strEvent = "WindowClosing!";
SetEventToConsole(strEvent);
}
@Override
public void windowDeactivated(WindowEvent e) {
strEvent = "WindowDeactivated!";
SetEventToConsole(strEvent);
}
@Override
public void windowDeiconified(WindowEvent e) {
strEvent = "WindowDeiconified!";
SetEventToConsole(strEvent);
}
@Override
public void windowIconified(WindowEvent e) {
strEvent = "WindowIconified!";
SetEventToConsole(strEvent);
}
@Override
public void windowOpened(WindowEvent e) {
strEvent = "WindowOpened!";
SetEventToConsole(strEvent);
}
/*鼠标事件*/
@Override
public void mouseDragged(MouseEvent e) {
//获取客户区坐标
strEvent = "MouseDragged: X=" + e.getX() + " Y=" + e.getY();
//strEvent = "MouseDragged:X=" + e.getXOnScreen() + "Y=" + e.getYOnScreen();//获取在屏幕上面的坐标
SetEventToLable(strEvent);
}
@Override
public void mouseMoved(MouseEvent e) {
strEvent = "MouseMoved: X=" + e.getX() + " Y=" + e.getY();
SetEventToLable(strEvent);
}
@Override
public void mouseClicked(MouseEvent e) {
strEvent = "MouseClicked!";
SetEventToLable(strEvent);
}
@Override
public void mouseEntered(MouseEvent e) {
strEvent = "MouseEntered!";
SetEventToLable(strEvent);
}
@Override
public void mouseExited(MouseEvent e) {
strEvent = "MouseExited!";
SetEventToLable(strEvent);
}
@Override
public void mousePressed(MouseEvent e) {
strEvent = "MousePressed!";
SetEventToLable(strEvent);
}
@Override
public void mouseReleased(MouseEvent e) {
strEvent = "MouseReleased!";
SetEventToLable(strEvent);
}
/*键盘事件*/
@Override
public void keyPressed(KeyEvent e) {
strEvent = "keyPressed!";
SetEventToLable(strEvent);
}
@Override
public void keyReleased(KeyEvent e) {
strEvent = "keyReleased!";
SetEventToLable(strEvent);
}
@Override
public void keyTyped(KeyEvent e) {
strEvent = "keyTyped: + The Key is: ";
strEvent += e.getKeyChar();
SetEventToLable(strEvent);
}
public static void main(String[] args) {
JMessageDemo JMD = new JMessageDemo();
}
}
//Code End
测试效果:
窗口事件:
鼠标事件:
键盘事件:
小节:
本测试演示的基本的控件事件响应的机制很基础,也很重要,重点在理解事件,消息驱动响应机制,驱动方
式,方法。