首页 > 代码库 > 第四节 Java Swing事件处理
第四节 Java Swing事件处理
第四节:事件处理
一个图形界面完的成只是程序开发中起步的工作,因为要想让每一个组件都发挥其作用,就必须对所有的组件进行事件处理。那么什么是事件处理,所谓事件就表示一个对象发生状态变化。例如,每当按下一个按钮时,实际上按钮的状态就发生了变化,而如果要想处理此事件,就需要监听者不断地进行监听事件的变化,并根据时间进行相应的处理。事件要想被处理,必须使用事件监听器,所有的事件监听器都是以接口的形式出现的,处理时只要实现此接口就行。整个事件处理流程如下图所示:
下面通过几个事件来进一步说明事件的处理流程。
窗体事件
WindowsListener是专门处理窗体的事件监听接口,一个窗体的所有变化,如窗体的打开,关闭等都可以使用这个接口进行监听。此接口定义的方法如下:
public void windowOpened(WindowEvent e)//窗口打开时触发
public void windowClosing(WindowEvent e)//窗口正在关闭时触发
public void windowIconified(WindowEvent e)//窗口最小化时触发
public void windowDeiconified(WindowEvent e)//窗口从最小化恢复到正常状态时触发
public void windowActivated(WindowEvent e)//将窗口变为活动窗口时触发
public void windowDeactivated(WindowEvent e)//将窗口变为不活动窗口时触发
建立一个监听器
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class MyWindowEventHandle implements WindowListener{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowOpened-->窗口被打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowClosing-->窗口关闭");
}
@Override
public void windowClosed(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowClosed-->窗口被关闭");
System.exit(1);//系统退出
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowIconifed-->窗口最小化");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowDeiconified-->窗口最小化恢复");
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowActivated-->窗口被选中");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
System.out.println("windowDeactivated-->窗口被选中");
}
}
单单只有一个监听器是不够的,还需要在组件上注册监听,这样才可以处理,直接使用窗体的addWindowListener(监听对象)方法即可注册。
import java.awt.Color;
import javax.swing.JFrame;
public class MyWindowEventJFrame01 {
public static void main(String args[]){
JFrame jFrame=new JFrame("Welcome to MichaelLee!");
//将此窗口加入到一个窗口监听器中,这样监听器就可以根据时间进行处理
jFrame.addWindowListener(new MyWindowEventHandle());
jFrame.setSize(400,300);
jFrame.setBackground(Color.black);
jFrame.setLocation(500,300);
jFrame.setVisible(true);
}
}
程序运行结果:
windowActivated-->窗口被选中
windowOpened-->窗口被打开
windowIconifed-->窗口最小化
windowDeactivated-->窗口被选中
windowDeiconified-->窗口最小化恢复
windowActivated-->窗口被选中
windowClosing-->窗口关闭
windowDeactivated-->窗口被选中
程序运行后会显示一个窗体,此时对窗体进行相应的状态变化,则在后台会打印出以上的信息。
监听适配器:
大致了解事件处理的基本流程后,大家可能会有这样一个疑问,如果现在只需对关闭窗口的事件进行监听,其他的操作根本就不关心,那末还有必要覆写那么多的方法吗?能不能只根据个人的需要来进行覆写?答案是肯定的。要想解决这个问题,可以使用Adapter(适配器)类。以WindowAdapter为例,用户只要继承了此类,就可以根据自己的需要覆写方法。比如现在我们只关心关闭窗口和打开窗口。
通过WindowAdapter实现监听
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class MyWindowAdapterHandler extends WindowAdapter{
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口被打开!");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosed(e);
System.out.println("窗口关闭!");
}
}
注册事件监听
import java.awt.Color;
import javax.swing.JFrame;
public class MyWindowEventJFrame02 {
public static void main(String args[]){
JFrame jFrame=new JFrame("Welcome to MichaelLee!");
jFrame.addWindowListener(new MyWindowAdapterHandler());
jFrame.setSize(500,400);
jFrame.setLocation(300,400);
jFrame.setBackground(Color.BLUE);
jFrame.setVisible(true);
}
}
程序运行结果:
窗口被打开!
窗口关闭!
此时只监听窗口被打开以及被关闭事件,但是这样一来又会出现一个新的问题,如果此监听处理只需操作一次,那末就没必要将其设置成一个单独的类,此时就可以利用匿名内部类来完成。
使用匿名内部类:
import java.awt.Color;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JFrame;
public class MyWindowEventJframe03 {
public static void main(String args[]){
JFrame jFrame=new JFrame("Welcome to MichaelLee!");
//此时直接使用WindowAdapter的子类完成监听的处理
jFrame.addWindowListener(new WindowAdapter() {
//覆写窗口的关闭方法
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口被打开!");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosed(e);
System.out.println("窗口关闭!");
}
});
jFrame.setSize(500,400);
jFrame.setLocation(300,400);
jFrame.setBackground(Color.BLUE);
jFrame.setVisible(true);
}
}
效果与上面的一样,可以看出直接编写匿名内部类可以减少监听类的定义,这在开发中是较为常见的一种做法。
动作事件及监听处理:
要想让一个按钮变得有意义,就必须使用事件处理。在Swing事件处理中可以使用ActionListener接口处理按钮的动作事件,ActionListener接口只定义了一个方法
Void actiongPerformed(ActionEvent e)//发生操作时调用
是用以上接口监听按钮的单击事件
import java.awt.Color;
import java.awt.Font;
import java.awt.Frame;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
class ActionHandler{
private JFrame jframe=new JFrame("Welcome to MichaelLee!");
private JButton btn=new JButton("显示");
private JLabel lab=new JLabel();
private JTextField text=new JTextField(10);//定义一个文本域
private JPanel pan=new JPanel();//定义一个版面
public ActionHandler(){
Font font=new Font("Serif",Font.ITALIC+Font.BOLD,28);
lab.setFont(font);
lab.setText("等待用户输入!");
btn.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==btn){//判断触发源是否是按钮
lab.setText(text.getText());//将文本文字设置到标签
}
}
});
//此时直接使用WindowAdapter的子类完成监听的处理
jframe.addWindowListener(new WindowAdapter() {
//覆写窗口的关闭方法
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口被打开!");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosed(e);
System.out.println("窗口关闭!");
}
});
jframe.setLayout(new GridLayout(2,1));
pan.setLayout(new GridLayout(1,2));
pan.add(text);
pan.add(btn);
jframe.add(pan);
jframe.add(lab);
jframe.add(lab);
jframe.pack();//根据组件自动调整窗口
jframe.setLocation(500,400);
jframe.setBackground(Color.BLUE);
jframe.setVisible(true);
}
}
public class MyActionEventDemon01 {
public static void main(String args[]){
new ActionHandler();
}
}
运行程序首先会出现这样一个界面
在文本框中输入内容后,单击显示,界面会变成这样
此时只要单击按钮就会触发监听器
了解了动作事件之后,实际上就可以使用此事件完成一个简单的用户登录操作。例如在程序中输入的用户名为MichaelLee密码为2014/7/9
则认为是合法用户,提示登录成功的信息。反之,则提示登陆失败的信息。代码如下:
先建一个类
class logincheck {
private String name;
private String password;
public logincheck(String name,String password){
this.name=name;
this.password=password;
}
public boolean validate(){
if("MichaelLee".equals(name)&&"2014/7/9".equals(password)){
return true;
}else{
return false;
}
}
}
登录操作实现:
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
class ActionHandle{//监听事件一旦形成,就一直处于监听状态,除非窗口关闭
private JFrame jFrame=new JFrame("Welcome to MichaelLee!");
private JButton submit=new JButton("登录");
private JButton reset=new JButton("重置");
private JLabel nameLabel=new JLabel("用户名");
private JLabel passJLabel=new JLabel("密码");
private JLabel infoLabel=new JLabel("用户登录系统");
private JTextField nameField=new JTextField();
private JPasswordField passField=new JPasswordField();
public ActionHandle(){
Font font=new Font("Serif",Font.BOLD,12);
infoLabel.setFont(font);
submit.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==submit){//判断触发源是否是提交按钮
//System.out.println("NO!");
String tname=nameField.getText();
String tpass=passField.getText();
logincheck log=new logincheck(tname,tpass);
if(log.validate()){//对用户名和密码进行验证
infoLabel.setText("登陆成功,欢迎光临!");
}else{
infoLabel.setText("登录失败,错误的用户名或密码");
}
}
}});
reset.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getSource()==reset){//判断触发源是否是重置按钮
nameField.setText("");//清空文本框内容
passField.setText("");//清空密码框内容
infoLabel.setText("用户登录");
}
}
});
jFrame.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.out.println("窗口关闭");
}
@Override
public void windowIconified(WindowEvent e) {
// TODO Auto-generated method stub
super.windowIconified(e);
System.out.println("窗口最小化");
}
@Override
public void windowDeiconified(WindowEvent e) {
// TODO Auto-generated method stub
super.windowDeiconified(e);
System.out.println("取消窗口最小化");
}
@Override
public void windowActivated(WindowEvent e) {
// TODO Auto-generated method stub
super.windowActivated(e);
System.out.println("选中窗口");
}
@Override
public void windowDeactivated(WindowEvent e) {
// TODO Auto-generated method stub
super.windowDeactivated(e);
System.out.println("取消窗口选中");
}
});
jFrame.setLayout(null);
nameLabel.setBounds(5,5,60,20);
passJLabel.setBounds(5,30,60, 20);
infoLabel.setBounds(5,65,220,30);
nameField.setBounds(65,5,100,20);
passField.setBounds(65,30,100,20);
submit.setBounds(165,5,60,20);
reset.setBounds(165,30,60,20);
jFrame.add(infoLabel);
jFrame.add(nameField);
jFrame.add(nameLabel);
jFrame.add(passField);
jFrame.add(passJLabel);
jFrame.add(reset);
jFrame.add(submit);
jFrame.setSize(380,130);
jFrame.setVisible(true);
}
}
public class MyActionExentDemon03 {
public static void main(String args[]){
new ActionHandle();
}
}
程序运行结果
输入正确的用户名和密码则会出现提示登陆成功,否则提示登录失败,单击重置按钮,内容被清空。
键盘事件及监听处理
在Swing中可以直接使用KeyListener接口对键盘的操作进行监听
KeyListener接口的方法
public void keyTyped(KeyEvent e)//输入某个键时调用
public void keyPressed(KeyEvent e)//键盘按下时调用
public void keyReleased(KeyEvent e)//键盘松开时调用
public char getKeyChar()//返回输入的字符,只针对与keyTyped有意义
public int getKeyCode()//返回输入字符的键码
public static String getKeyText(int keyCode)//返回此键的信息,如‘HOME’‘F1’或‘A’等
实现键盘监听
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MykeyHandle extends JFrame implements KeyListener{
public JTextArea textArea=new JTextArea();
public MykeyHandle(){
super.setTitle("Welcome to MichaelLee!");
JScrollPane src=http://www.mamicode.com/new JScrollPane(textArea);
src.setBounds(5, 5, 300, 200);
super.add(src);
textArea.addKeyListener(this);
super.setSize(310, 210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.out.println("窗口关闭");
}
});
}
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("输入的内容是:"+e.getKeyChar()+"\n");//输出到后台
textArea.append("输入的内容是:"+e.getKeyChar()+"\n");//输入到多行文本框中
}
@Override
public void keyPressed(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键按下\n");//输出到后台
textArea.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键按下\n");//输入到多行文本框中
}
@Override
public void keyReleased(KeyEvent e) {
// TODO Auto-generated method stub
System.out.println("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键松开\n");//输出到后台
textArea.append("键盘"+KeyEvent.getKeyText(e.getKeyCode())+"键松开\n");//输入到多行文本框中
}
}
public class MyKeyEventDemon01 {
public static void main(String args[]){
new MykeyHandle().textArea.append("hahhahahhahah!");
}
}
运行结果
This表示当前对象,以上程序中,MyKeyHandle实现了KeyListener监听接口,所以此类也是监听操作类,这样当JTextArea增加事件时直接使用This关键字,如下所示:
text.addKeyListener(this);
This表示当前对象,此时将this加入到监听器中,就表示将一个监听处理类加入到监听器中。
在键盘监听中也可以使用KeyAdapter适配器完成键盘事件的监听
使用KeyAdapter
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class Mykeyhandle2 extends JFrame{
//此类直接继承了JFrame,以下的super可以理解为JFrame
private JTextArea textArea=new JTextArea();
public Mykeyhandle2(){
super.setTitle("Welcome to MichaelLee!");
JScrollPane src=http://www.mamicode.com/new JScrollPane(textArea);
src.setBounds(5, 5, 300, 200);
super.add(src);
textArea.addKeyListener(new KeyAdapter() {//直接使用KeyAdapter完成监听,可以选择需要的方法进行覆写
@Override
public void keyTyped(KeyEvent e) {
// TODO Auto-generated method stub
super.keyTyped(e);
System.out.println("输入的内容是:"+e.getKeyChar()+"\n");
textArea.append("输入的内容是:"+e.getKeyChar()+"\n");
}
});
super.setSize(310, 210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowOpened(WindowEvent e) {
// TODO Auto-generated method stub
super.windowOpened(e);
System.out.println("窗口打开");
}
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.out.println("窗口关闭");
}
});
}
}
public class MyKeyEventDemon02 {
public static void main(String args[]){
new Mykeyhandle2();
}
}
运行效果
对鼠标事件进行监听可以使用MouseListener接口,常用方法如下:
public void mouseClicked(MouseEvent e)//鼠标单击时调用
public void mousePressed(MouseEvent e)//按下时调用
public void mouseReleased(MouseEvent e)//松开时调用
public void mouseEntered(MouseEvent e)//鼠标进入到组件时调用
public void mouseExited(MouseEvent e)//鼠标离开组件时调用
Public static final int BUTTON1//表示鼠标左键的常量
Public static final int BUTTON2//表示鼠标滚轴的常量
Public static final int BUTTON3//表示鼠标右键的常量
Public int getButton()//以数字形式返回按下的鼠标键
Public int getClickCount()//返回鼠标的单击次数
Public int getX()//返回鼠标操作的X坐标
Public int getY()//返回鼠标操作的Y坐标
实现鼠标监听
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyMousehandle extends JFrame implements MouseListener{
private JTextArea textArea=new JTextArea();
public MyMousehandle(){
super.setTitle("Welcome to MichaelLee!");
JScrollPane src=http://www.mamicode.com/new JScrollPane(textArea);
src.setBounds(5, 5, 300, 200);
super.add(src);
textArea.addMouseListener(this);
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(1);
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int c=e.getButton();
String mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左键";
}else if(c==MouseEvent.BUTTON3){
mouseInfo="右键";
}else if(c==MouseEvent.BUTTON2){
mouseInfo="滚轴";
}
textArea.append("鼠标单击:"+mouseInfo+"\n");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标按下。\n");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标松开。\n");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标进入组件。\n");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标离开组件。\n");
}
}
public class MyMouseEventdemon01 {
public static void main(String args[]){
new MyMousehandle();
}
}
对鼠标事件进行监听可以使用MouseListener接口,常用方法如下:
public void mouseClicked(MouseEvent e)//鼠标单击时调用
public void mousePressed(MouseEvent e)//按下时调用
public void mouseReleased(MouseEvent e)//松开时调用
public void mouseEntered(MouseEvent e)//鼠标进入到组件时调用
public void mouseExited(MouseEvent e)//鼠标离开组件时调用
Public static final int BUTTON1//表示鼠标左键的常量
Public static final int BUTTON2//表示鼠标滚轴的常量
Public static final int BUTTON3//表示鼠标右键的常量
Public int getButton()//以数字形式返回按下的鼠标键
Public int getClickCount()//返回鼠标的单击次数
Public int getX()//返回鼠标操作的X坐标
Public int getY()//返回鼠标操作的Y坐标
实现鼠标监听
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyMousehandle extends JFrame implements MouseListener{
private JTextArea textArea=new JTextArea();
public MyMousehandle(){
super.setTitle("Welcome to MichaelLee!");
JScrollPane src=http://www.mamicode.com/new JScrollPane(textArea);
src.setBounds(5, 5, 300, 200);
super.add(src);
textArea.addMouseListener(this);
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(1);
}
});
}
@Override
public void mouseClicked(MouseEvent e) {
// TODO Auto-generated method stub
int c=e.getButton();
String mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左键";
}else if(c==MouseEvent.BUTTON3){
mouseInfo="右键";
}else if(c==MouseEvent.BUTTON2){
mouseInfo="滚轴";
}
textArea.append("鼠标单击:"+mouseInfo+"\n");
}
@Override
public void mousePressed(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标按下。\n");
}
@Override
public void mouseReleased(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标松开。\n");
}
@Override
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标进入组件。\n");
}
@Override
public void mouseExited(MouseEvent e) {
// TODO Auto-generated method stub
textArea.append("鼠标离开组件。\n");
}
}
public class MyMouseEventdemon01 {
public static void main(String args[]){
new MyMousehandle();
}
}
也可以通过MouseAdapter实现指定鼠标操作的监听
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JFrame;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
class MyMousehandle3 extends JFrame{
private JTextArea textArea=new JTextArea();
public MyMousehandle3(){
super.setTitle("Welcome to MichaelLee!");
JScrollPane scr=new JScrollPane(textArea);
scr.setBounds(5, 5, 300, 200);
super.add(scr);
textArea.addMouseListener(new MouseAdapter() {
@Override
public void mouseClicked(MouseEvent e) {//z只覆写mouseClicked方法
// TODO Auto-generated method stub
super.mouseClicked(e);
int c=e.getButton();
String mouseInfo=null;
if(c==MouseEvent.BUTTON1){
mouseInfo="左键";
}else if(c==MouseEvent.BUTTON3){
mouseInfo="右键";
}else{
mouseInfo="滚轴";
}
textArea.append("鼠标单击:"+mouseInfo+"\n");
}
});
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(1);
}
});
}
}
public class MyMouseEventDemon02 {
public static void main(String args[]){
new MyMousehandle();
}
}
鼠标拖曳事件及监听处理
在一般的图形界面经常可以看到鼠标拖曳操作的处理,在Swing事件处理方法中可以使用MouseMotionListener接口完成鼠标的拖曳操作。此接口常用方法:
Void mouseDragged(MouseEvent e)//在组件上按下并拖动时调用
Void MouseMoved(MouseEvent e)//鼠标移动到组件时调用
观察鼠标拖曳操作
鼠标拖曳事件及监听处理
在一般的图形界面经常可以看到鼠标拖曳操作的处理,在Swing事件处理方法中可以使用MouseMotionListener接口完成鼠标的拖曳操作。此接口常用方法:
Void mouseDragged(MouseEvent e)//在组件上按下并拖动时调用
Void MouseMoved(MouseEvent e)//鼠标移动到组件时调用
观察鼠标拖曳操作
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import javax.swing.JButton;
import javax.swing.JFrame;
class MyMouseMotionhandle extends JFrame{
public MyMouseMotionhandle(){
super.setTitle("Welcome to MichaelLee!");
super.addMouseMotionListener(new MouseMotionListener() {
@Override
public void mouseMoved(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标移动到窗体");
}
@Override
public void mouseDragged(MouseEvent e) {
// TODO Auto-generated method stub
System.out.println("鼠标拖也到:X="+e.getX()+",Y="+e.getY());
}
});
/*JButton ibt=new JButton("按钮");
super.add(ibt);*/
super.setSize(310,210);
super.setVisible(true);
super.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
super.windowClosing(e);
System.exit(1);
}
});
}
}
public class MyMouseMotionEventDemon01 {
public static void main(String args[]){
new MyMouseMotionhandle();
}
}
程序运行后发现,只要鼠标一向窗体移动就会触发mouseMoved()事件,只要是在窗体上拖曳,就会触发mouseDragged事件。