化工网站建站模板,wordpress locahost,网站备案需先做网站吗,河南网站优化建设第四节#xff1a;事件处理一个图形界面完的成只是程序开发中起步的工作#xff0c;因为要想让每一个组件都发挥其作用#xff0c;就必须对所有的组件进行事件处理。那么什么是事件处理#xff0c;所谓事件就表示一个对象发生状态变化。例如#xff0c;每当按下一个按钮时…第四节事件处理一个图形界面完的成只是程序开发中起步的工作因为要想让每一个组件都发挥其作用就必须对所有的组件进行事件处理。那么什么是事件处理所谓事件就表示一个对象发生状态变化。例如每当按下一个按钮时实际上按钮的状态就发生了变化而如果要想处理此事件就需要监听者不断地进行监听事件的变化并根据时间进行相应的处理。事件要想被处理必须使用事件监听器所有的事件监听器都是以接口的形式出现的处理时只要实现此接口就行。整个事件处理流程如下图所示下面通过几个事件来进一步说明事件的处理流程。窗体事件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{Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowOpened--窗口被打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowClosing--窗口关闭);}Overridepublic void windowClosed(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowClosed--窗口被关闭);System.exit(1);//系统退出}Overridepublic void windowIconified(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowIconifed--窗口最小化);}Overridepublic void windowDeiconified(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowDeiconified--窗口最小化恢复);}Overridepublic void windowActivated(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowActivated--窗口被选中);}Overridepublic void windowDeactivated(WindowEvent e) {// TODO Auto-generated method stubSystem.out.println(windowDeactivated--窗口被选中);}}单单只有一个监听器是不够的还需要在组件上注册监听这样才可以处理直接使用窗体的addWindowListener(监听对象)方法即可注册。import java.awt.Color;import javax.swing.JFrame;public class MyWindowEventJFrame01 {public static void main(String args[]){JFrame jFramenew 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{Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口被打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosed(e);System.out.println(窗口关闭);}}注册事件监听import java.awt.Color;import javax.swing.JFrame;public class MyWindowEventJFrame02 {public static void main(String args[]){JFrame jFramenew 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 jFramenew JFrame(Welcome to MichaelLee!);//此时直接使用WindowAdapter的子类完成监听的处理jFrame.addWindowListener(new WindowAdapter() {//覆写窗口的关闭方法Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口被打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.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 jframenew JFrame(Welcome to MichaelLee!);private JButton btnnew JButton(显示);private JLabel labnew JLabel();private JTextField textnew JTextField(10);//定义一个文本域private JPanel pannew JPanel();//定义一个版面public ActionHandler(){Font fontnew Font(Serif,Font.ITALICFont.BOLD,28);lab.setFont(font);lab.setText(等待用户输入);btn.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()btn){//判断触发源是否是按钮lab.setText(text.getText());//将文本文字设置到标签}}});//此时直接使用WindowAdapter的子类完成监听的处理jframe.addWindowListener(new WindowAdapter() {//覆写窗口的关闭方法Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口被打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.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.namename;this.passwordpassword;}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 jFramenew JFrame(Welcome to MichaelLee!);private JButton submitnew JButton(登录);private JButton resetnew JButton(重置);private JLabel nameLabelnew JLabel(用户名);private JLabel passJLabelnew JLabel(密码);private JLabel infoLabelnew JLabel(用户登录系统);private JTextField nameFieldnew JTextField();private JPasswordField passFieldnew JPasswordField();public ActionHandle(){Font fontnew Font(Serif,Font.BOLD,12);infoLabel.setFont(font);submit.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()submit){//判断触发源是否是提交按钮//System.out.println(NO!);String tnamenameField.getText();String tpasspassField.getText();logincheck lognew logincheck(tname,tpass);if(log.validate()){//对用户名和密码进行验证infoLabel.setText(登陆成功欢迎光临);}else{infoLabel.setText(登录失败错误的用户名或密码);}}}});reset.addActionListener(new ActionListener() {Overridepublic void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubif(e.getSource()reset){//判断触发源是否是重置按钮nameField.setText();//清空文本框内容passField.setText();//清空密码框内容infoLabel.setText(用户登录);}}});jFrame.addWindowListener(new WindowAdapter() {Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosing(e);System.out.println(窗口关闭);}Overridepublic void windowIconified(WindowEvent e) {// TODO Auto-generated method stubsuper.windowIconified(e);System.out.println(窗口最小化);}Overridepublic void windowDeiconified(WindowEvent e) {// TODO Auto-generated method stubsuper.windowDeiconified(e);System.out.println(取消窗口最小化);}Overridepublic void windowActivated(WindowEvent e) {// TODO Auto-generated method stubsuper.windowActivated(e);System.out.println(选中窗口);}Overridepublic void windowDeactivated(WindowEvent e) {// TODO Auto-generated method stubsuper.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 textAreanew JTextArea();public MykeyHandle(){super.setTitle(Welcome to MichaelLee!);JScrollPane srcnew 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() {Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosing(e);System.out.println(窗口关闭);}});}Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stubSystem.out.println(输入的内容是e.getKeyChar()\n);//输出到后台textArea.append(输入的内容是e.getKeyChar()\n);//输入到多行文本框中}Overridepublic void keyPressed(KeyEvent e) {// TODO Auto-generated method stubSystem.out.println(键盘KeyEvent.getKeyText(e.getKeyCode())键按下\n);//输出到后台textArea.append(键盘KeyEvent.getKeyText(e.getKeyCode())键按下\n);//输入到多行文本框中}Overridepublic void keyReleased(KeyEvent e) {// TODO Auto-generated method stubSystem.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适配器完成键盘事件的监听使用KeyAdapterimport 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可以理解为JFrameprivate JTextArea textAreanew JTextArea();public Mykeyhandle2(){super.setTitle(Welcome to MichaelLee!);JScrollPane srcnew JScrollPane(textArea);src.setBounds(5, 5, 300, 200);super.add(src);textArea.addKeyListener(new KeyAdapter() {//直接使用KeyAdapter完成监听可以选择需要的方法进行覆写Overridepublic void keyTyped(KeyEvent e) {// TODO Auto-generated method stubsuper.keyTyped(e);System.out.println(输入的内容是e.getKeyChar()\n);textArea.append(输入的内容是e.getKeyChar()\n);}});super.setSize(310, 210);super.setVisible(true);super.addWindowListener(new WindowAdapter() {Overridepublic void windowOpened(WindowEvent e) {// TODO Auto-generated method stubsuper.windowOpened(e);System.out.println(窗口打开);}Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.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 textAreanew JTextArea();public MyMousehandle(){super.setTitle(Welcome to MichaelLee!);JScrollPane srcnew 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() {Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosing(e);System.exit(1);}});}Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubint ce.getButton();String mouseInfonull;if(cMouseEvent.BUTTON1){mouseInfo左键;}else if(cMouseEvent.BUTTON3){mouseInfo右键;}else if(cMouseEvent.BUTTON2){mouseInfo滚轴;}textArea.append(鼠标单击mouseInfo\n);}Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标按下。\n);}Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标松开。\n);}Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标进入组件。\n);}Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stubtextArea.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 textAreanew JTextArea();public MyMousehandle(){super.setTitle(Welcome to MichaelLee!);JScrollPane srcnew 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() {Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosing(e);System.exit(1);}});}Overridepublic void mouseClicked(MouseEvent e) {// TODO Auto-generated method stubint ce.getButton();String mouseInfonull;if(cMouseEvent.BUTTON1){mouseInfo左键;}else if(cMouseEvent.BUTTON3){mouseInfo右键;}else if(cMouseEvent.BUTTON2){mouseInfo滚轴;}textArea.append(鼠标单击mouseInfo\n);}Overridepublic void mousePressed(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标按下。\n);}Overridepublic void mouseReleased(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标松开。\n);}Overridepublic void mouseEntered(MouseEvent e) {// TODO Auto-generated method stubtextArea.append(鼠标进入组件。\n);}Overridepublic void mouseExited(MouseEvent e) {// TODO Auto-generated method stubtextArea.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 textAreanew JTextArea();public MyMousehandle3(){super.setTitle(Welcome to MichaelLee!);JScrollPane scrnew JScrollPane(textArea);scr.setBounds(5, 5, 300, 200);super.add(scr);textArea.addMouseListener(new MouseAdapter() {Overridepublic void mouseClicked(MouseEvent e) {//z只覆写mouseClicked方法// TODO Auto-generated method stubsuper.mouseClicked(e);int ce.getButton();String mouseInfonull;if(cMouseEvent.BUTTON1){mouseInfo左键;}else if(cMouseEvent.BUTTON3){mouseInfo右键;}else{mouseInfo滚轴;}textArea.append(鼠标单击mouseInfo\n);}});super.setSize(310,210);super.setVisible(true);super.addWindowListener(new WindowAdapter() {Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.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() {Overridepublic void mouseMoved(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println(鼠标移动到窗体);}Overridepublic void mouseDragged(MouseEvent e) {// TODO Auto-generated method stubSystem.out.println(鼠标拖也到Xe.getX(),Ye.getY());}});/*JButton ibtnew JButton(按钮);super.add(ibt);*/super.setSize(310,210);super.setVisible(true);super.addWindowListener(new WindowAdapter() {Overridepublic void windowClosing(WindowEvent e) {// TODO Auto-generated method stubsuper.windowClosing(e);System.exit(1);}});}}public class MyMouseMotionEventDemon01 {public static void main(String args[]){new MyMouseMotionhandle();}}程序运行后发现只要鼠标一向窗体移动就会触发mouseMoved()事件只要是在窗体上拖曳就会触发mouseDragged事件。