`

javaSwing的监听探索

阅读更多

事件处理:
Source                                    Event                                      Event Listener
--------------------------------------------------------------------------------------------------------------
AbstractButton                        ActionEvent                                  ActionListener
(JButton,JtoggleButton,           ChangeEvent                               ChangeListener
JCheckBox,JRadioButton          ItemEvent                                    ItemListener
-------------------------------------------------------------------------------------------------------
JTextField                                 ActionEvent                                  ActionListener
JpasswordField                        CaretEvent                                   CaretListener
                                                DocumentEvent                            DocumentListener
                                                UndoableEvent                             UndoableListener
-------------------------------------------------------------------------------------------------------
JTextArea                                CaretEvent                                   CaretListener
                                                DocumentEvent                            DocumentListener
                                                UndoableEvent                             UndoableListener
-------------------------------------------------------------------------------------------------------
JTextPane                              CaretEvent                                     CaretListener
JEditorPane                            DocumentEvent                              DocumentListener
                                              UndoableEvent                               UndoableListener
                                              HyperlinkEvent                                HyperlinkListener
-------------------------------------------------------------------------------------------------------
JComboBox                            ActionEvent                                  ActionListener
                                              ItemEvent                                    ItemListener
-------------------------------------------------------------------------------------------------------
JList                                      ListSelectionEvent                          ListSelectionListener
                                             ListDataEvent                                ListDataListener
-------------------------------------------------------------------------------------------------------
JFileChooser                           ActionEvent                                  ActionListener
-------------------------------------------------------------------------------------------------------
JMenuItem                               ActionEvent                                  ActionListener
                                               ChangeEvent                                ChangeListener
                                               ItemEvent                                     ItemListener
                                               MenuKeyEvent                              MenuKeyListener
                                               MenuDragMouseEvent                  MenuDragMouseListener
-------------------------------------------------------------------------------------------------------
JMenu                                     MenuEvent                                    MenuListener
-------------------------------------------------------------------------------------------------------
JPopupMenu                           PopupMenuEvent                          PopupMenuListener
-------------------------------------------------------------------------------------------------------
JProgressBar                          ChangeEvent                                ChangeListener
-------------------------------------------------------------------------------------------------------
JSlider                                    ChangeEvent                               ChangeListener
-------------------------------------------------------------------------------------------------------
JScrollBar                              AdjustmentEvent                          AdjustMentListener
-------------------------------------------------------------------------------------------------------
JTable                                 ListSelectionEvent                           ListSelectionListener
                                           TableModeEvent                             TableModeListener
                                           TableColumnModelEvent                 TableColumnModeListener
                                           CellEditorEvent                               CellEditorListener
-------------------------------------------------------------------------------------------------------
JTabbedPane                      ChangeEvent                                  ChangeListener
-------------------------------------------------------------------------------------------------------
JTree                                  TreeSelectionEvent                          TreeSelectionListener
                                          TreeExpansionEvent                        TreeExpansionListener
                                          TreeWillExpandEvent                       TreeWillExpandListener
                                          TreeModeEvent                                TreeModeListener    
-------------------------------------------------------------------------------------------------------
JTimer                                 ActionEvent                                    ActionListener
-------------------------------------------------------------------------------------------------------    
        底层事件:

---------------------------------------------------------------------------------------------------------------

     Event                                                                     Listener
  ComponentEvent                                                  ComponentListener
  ContainerEvent                                                     ConatainerListener
  FocusEvent                                                            FocusListener
  KeyEvent                                                               KeyListener
  MouseEvent                                                          MouseListener
  MouseMotionEvent                                                MouseMotionListener
  WindowEvent                                                        WindowListener
ComponentEvent:主要处理组件大小的改变,位置的改变,或是可见与不可见状态(hidden or visible)等。
ContainerListener:主要在处理组件的加入或移出容器。
FocusListener:主要在处理焦点的取得或移开焦点等操作。
MouseListener:主要就是在处理鼠标是否在某个组件上,是否按下鼠标键,是否离开某个组件等操作,主要针对鼠标按键与位置
              作处理。
MouseMotionListener:主要就是在追踪鼠标的位置,如(X,Y)坐标的位置,主人是针对鼠标坐标与拖曳操作作处理。
WindowListener:主要在处理窗口的所有操作。如处理active窗口或是de-active窗口,处理窗口的最大、最小化,处理窗口的关闭
               、打开、退出等。

  以上所述的每种EventListener都是一种interface,里面只有定义这个EventListener的interface,然后在这个类中,把要处理事
件的程序代码写在此interface的方法中。这是处理事件的标准操作,当然还有其他编写模式,如:利用inner class的匿名类方法
,就可以不用在class后面加上implements EventListener的表示法,或是利用Adapter类,就不用一一将interface中的每种方法都
实现。
  让我们来了解一下,每一种EventListener的interface到底提供了哪几种方法要我们实现(Implement)!我们把事件区分成是由
Swing或是AWT所引发:
       Swing EventListener                                          Method
----------------------------------------------------------------------------------------------------------------
         CaretListener                                         caretUpdate(CaretEvent e)
--------------------------------------------------------------------------------------------------------------
         CellEditorListener                                   editingCanceled(ChangeEvent e)
                                                                        editingStopped(ChangeEvent e)
--------------------------------------------------------------------------------------------------------------
         ChangeListener                                      stateChanged(ChangeEvent e)
--------------------------------------------------------------------------------------------------------------
         DocumentListener                                   changedUpdate(DocumentEvent e)
                                                                         insertUpdate(DocumentEvent e)
                                                                        removeUpdate(DocumentEvent e)
--------------------------------------------------------------------------------------------------------------
         HyperlinkListener                                    hyperlinkUpdate(HyperlinkEvent e)
--------------------------------------------------------------------------------------------------------------
         ListDataListener                                     contentsChanged(ListdataEvent e)
                                                                        intervalAdded(ListDataEvent e)
                                                                        intervalRemoved(ListDataEvent e)
--------------------------------------------------------------------------------------------------------------
         ListSelectionListener                               valueChanged(ListSelectionEvent e)
--------------------------------------------------------------------------------------------------------------
         MenuDragMouseListener                        menuDragMouseDragged(MenuDragMouseEvent e)
                                                                        menuDragMouseEntered(menuDragMouseEvent e)
                                                                        menuDragMouseExited(MenuDragMouseEvent e)
                                                                        menuDragMouseReleased(MenuDragMouseEvent e)
--------------------------------------------------------------------------------------------------------------
         MenuKeyListener                                    menuKeyPressed(MenuKeyEvent e)
                                                                        menuKeyReleased(MenuKeyEvent e)
                                                                        menuKeyTyped(MenuKeyEvent e)
--------------------------------------------------------------------------------------------------------------
         MenuListener                                         menuCanceled(MenuEvent e)
                                                                       menuDeselected(MenuEvent e)
                                                                       menuSelected(MenuEvent e)
--------------------------------------------------------------------------------------------------------------
         PopupMenuListener                               popupMenuCanceled(PopupMenuEvent e)
                                                                       popupMenuWillBecomeInvisible(PopupMenuEvent e)
                                                                       popupMenuWillBecomeVisible(PopupMenuEvent e)
--------------------------------------------------------------------------------------------------------------
         TableColumnModelListener                    columnAdded(TableColumnModelEvent e)
                                                                       columnMarginChanged(ChangeEvent e)
                                                                       columnMoved(TableColumnModelEvent e)
                                                                       columnRemoved(TableColumnModelEvent e)
                                                                       columnSelectionChanged(ListSelectionEvent e)
--------------------------------------------------------------------------------------------------------------
         TableModelListener                                tableChanged(TableModelEvent e)
--------------------------------------------------------------------------------------------------------------
         TreeExpansionListener                           treeCollapsed(TreeExpansionEvent event)
                                                                        treeExpanded(TreeExpansionEvent event)
---------------------------------------------------------------------------------------------------------------
         TreeModelListener                                  treeNodesChanged(TreeModelEvent e)
                                                                        treeNodesInserted(TreeModelEvent e)
                                                                        treeNodesRemoved(TreeModelEvent e)
                                                                        treeStructureChanged(TreeModelEvent e)
---------------------------------------------------------------------------------------------------------------
         TreeSelectionListener                             valueChanged(TreeSelectionEvent e)
---------------------------------------------------------------------------------------------------------------
         TreeWillExpandListener                          treeWillCollapse(TreeExpansionEvent event)
                                                                        treeWillExpand(TreeExpansionEvent event)
---------------------------------------------------------------------------------------------------------------
         UndoableEditListener                             undoableEditHappened(UndoableEditEvent e)
---------------------------------------------------------------------------------------------------------------
                   





            AWT EventListener                                         Method                      
                 --------------------------------------------------------------------------------------------------------------
          ActionListener                                        actionPerformed(ActionEvent e)
--------------------------------------------------------------------------------------------------------------
          AdjustmentListener                                adjustmentValueChanged(AdjustmentEvent e)
--------------------------------------------------------------------------------------------------------------
          ComponentListener                                componentHidden(ComponentEvent e)
                                                                         componentMoved(ComponentEvent e)
                                                                         componentResized(ComponentEvent e)
                                                                         componentShown(ComponentEvent e)
--------------------------------------------------------------------------------------------------------------
          ContainerListener                                   componentAdded(ContainerEvent e)
                                                                         componentRemoved(ContainerEvent e)
--------------------------------------------------------------------------------------------------------------
          FocusListener                                        focusGained(FocusEvent e)
                                                                        focusLost(FocusEvent e)
--------------------------------------------------------------------------------------------------------------
          ItemListener                                         itemStateChanged(ItemEvent e)
--------------------------------------------------------------------------------------------------------------
          KeyListener                                            keyPressed(KeyEvent e)
                                                                        keyReleased(KeyEvent e)
                                                                        keyTyped(KeyEvent e)
--------------------------------------------------------------------------------------------------------------
          MouseListener                                        mouseClicked(MouseEvent e)
                                                                         mouseEntered(MouseEvent e)
                                                                         mouseExited(MouseEvent e)
                                                                         mousePressed(MouseEvent e)
                                                                         mouseReleased(MouseEvent e)
--------------------------------------------------------------------------------------------------------------
          MouseMotionListener                              mouseDragged(MouseEvent e)
                                                                         mouseMoved(MouseEvent e)
--------------------------------------------------------------------------------------------------------------
          WindowListener                                      windowActivated(WindowEvent e)
                                                                         windowClosed(WindowEvent e)
                                                                         windowClosing(WindowEvent e)
                                                                         windowDeactivated(WindowEvent e)
                                                                         windowDeiconified(WindowEvent e)
                                                                         windowIconified(WindowEvent e)
                                                                         windowOpened(WindowEvent e)
---------------------------------------------------------------------------------------------------------------

常用的Adapter类如下:
              Adapter                                           对应的EventListener
---------------------------------------------------------------------------------------------------------------
         java.awt.event.ComponentAdapter                  ComponentListener
         java.awt.event.ContainerAdapter                     ContainerListener
         java.awt.event.FocusAdapter                            FocusListener
         java.awt.event.KeyAdapter                               KeyListener
         java.awt.event.MouseAdapter                           MouseListener
         java.awt.event.MouseMotionAdapter                MouseMotionListener
         java.awt.event.WindowAdapter                        WindowListener
---------------------------------------------------------------------------------------------------------------
   在java中提供两个方法让你知道到底是哪个组件触发了事件,那就是getSource()与getActionCommane();getActionCommand()
方法是ActionEvent类所提供,而getSource()方法是EventObject类所提供,但ActionEvent类继承了EventObject类,因此这两个
方法ActionEvent都可以使用,看下面的例子就知道了:

package com;

import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 * 
 * @author chenhailong
 *
 */
public class EventDemo extends WindowAdapter implements ActionListener
{
    JButton b1 = null;
    JButton b2 = null;
    
    public EventDemo()
    {
        JFrame f = new JFrame("EventDemo");
        f.setSize(300, 400);
        JPanel panel = new JPanel();
        panel.setBorder(BorderFactory.createTitledBorder("一个简单的程序"));
        panel.setSize(300, 300);
        b1 = new JButton("按我有声音喔");
        b2 = new JButton("按我可开新窗口");
        b1.addActionListener(this);
        b2.addActionListener(this);
        panel.add(b1);
        panel.add(b2);
        f.add(panel);
        f.pack();
        f.setVisible(true);
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.addWindowListener(this);
    }

    public void actionPerformed(ActionEvent e)
    {
    	//getActionCommand()方法会返回按钮上的文字字符串。getSource()是判断组件
        if((e.getActionCommand()).equals("按我有声音喔")) 
            Toolkit.getDefaultToolkit().beep();
        if((e.getActionCommand()).equals("按我可开新窗口"))
        {
            JFrame newF = new JFrame("新窗口");
            newF.setSize(300,300);
            newF.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            newF.setVisible(true);
        } 
    }
    
    public void windowClosing(WindowEvent e)
    {
        System.exit(0);
    }
    
    public static void main(String args[])
    {
        new EventDemo();
    }

}

 

0
0
分享到:
评论

相关推荐

    贪吃蛇java swing

    Java Swing通过事件监听器和事件处理器实现这一交互。例如,可以为键盘按键添加KeyListener,当用户按下方向键时,相应的事件会被触发,蛇的方向会改变。 3. **游戏循环**: 贪吃蛇游戏的核心是游戏循环,它负责更新...

    Java Swing组件综合练习

    Java Swing是Java GUI(图形用户界面)开发的一个重要库,它是Java AWT(Abstract Window Toolkit)的扩展,提供了更为丰富的组件和更灵活的布局管理器。...不断练习和探索,你将在Java Swing的世界里游刃有余。

    java swing界面设计

    - "Java图形用户界面设计(Swing常用组件).ppt"和"JavaSwing界面设计(所有控件及示例).ppt"两个文件中可能包含了各种Swing组件的使用示例和实际应用,建议仔细学习和实践。 9. **学习资源** - Java官方文档:提供...

    java swing深入学习例子

    总的来说,Java Swing的学习涵盖了从基础组件到高级特性的全面探索,它不仅涉及到编程技术,还包括了用户体验设计和软件架构思维。深入理解并熟练运用Java Swing,将使你有能力构建功能强大、用户友好的桌面应用程序...

    java swing 游戏开发文档

    虽然相比专门的游戏引擎,它可能在某些方面显得较为复杂,但如果你熟悉Java语言并且希望掌握3D图形编程,那么这是一个值得探索的领域。通过不断学习和实践,你可以利用这些工具创造出令人印象深刻的3D游戏。

    java swing可视化插件

    Java Swing 是Java平台上的一个图形用户界面(GUI)工具包,它是Java Foundation Classes (JFC)的一部分,用于帮助开发者创建桌面应用程序。Swing 提供了一系列组件,如按钮、文本框、菜单等,允许开发者构建功能丰富...

    Java Swing图形界面开发与案例详解及随书实例代码

    Java Swing是Java编程语言中用于构建桌面应用程序图形用户界面(GUI)的一个库,它是Java Foundation Classes (JFC)的一部分。Swing提供了丰富的组件集,包括按钮、文本框、菜单...祝你在Java Swing的世界里探索愉快!

    JAVA Swing实例讲解.zip

    JAVA Swing 是Java编程语言中用于构建桌面应用用户界面的一个图形组件库。它是Java Foundation Classes (JFC)的一部分,提供了一套...通过这些实例,你可以逐步探索Swing的奥秘,打造出更加美观、易用的桌面应用程序。

    javaSwing初级入门知识

    总结来说,"javaSwing初级入门知识"涵盖了从基础组件的使用到高级特性的探索。IBM的教程是很好的学习资源,通过阅读和实践,初学者可以快速掌握Swing的基本操作,为构建自己的Java桌面应用打下坚实的基础。在学习...

    Java Swing(第二版)

    这本书的源码提供给读者实践和探索Swing API的机会。 1. **Swing组件**:Swing提供了许多组件,如JButton、JLabel、JTextArea、JTable、JTree等,它们比AWT组件更轻量级且功能更强大。例如,JTable可以用来显示和...

    深入浅出java swing程序设计电子书

    这本书将引领读者探索Swing的各种组件,包括按钮、文本框、面板、菜单、滚动条等,以及如何使用这些组件构建出具有专业外观和交互性的用户界面。 首先,书中会详细介绍Swing的基本概念,如事件处理机制,这是理解...

    java swing所有组件展示 (附源码)

    在探索Swing组件时,记得学习和理解各种布局管理器,如FlowLayout、BorderLayout、GridLayout、 MigLayout和GridBagLayout,它们控制着组件在容器中的排列方式。同时,事件处理是Swing应用的核心部分,你需要了解...

    Java Swing 小游戏 迷宫

    在这个特定的项目中,“迷宫”小游戏是利用Java Swing开发的一个交互式应用,允许用户通过键盘输入来探索和解决迷宫问题。 在Swing中,键盘事件的处理通常涉及到键盘监听器(KeyListener)。键盘监听器接口有三个...

    优秀java swing GUI 工作原理.pd

    这些问题鼓励听众深入探索Swing的MVC实现细节,理解每个组成部分的角色和职责,以及如何有效地组织和设计Swing应用程序。 在“基础知识:MVC和Swing”部分,我们进一步了解了MVC模式在Swing中的具体应用。Swing通过...

    java swing权威指南

    - **总结**:本书全面介绍了 Java Swing 的各个方面,不仅适合初学者入门学习,也适合有经验的开发者深入探索。 - **展望**:虽然近年来 JavaFX 和其他现代 GUI 框架逐渐兴起,但 Swing 仍然因其成熟稳定、功能...

    java精通swing程序设计

    Java Swing 是Java GUI(图形用户界面)开发的重要组成部分,它提供了丰富的组件库,使得开发者能够构建功能强大且具有视觉吸引力的桌面应用程序。本教程将深入探讨Java Swing程序设计,帮助你掌握这一关键技术。 ...

    java浏览器+swing嵌套网页+源码免费

    如果你对Java GUI编程和Web技术感兴趣,那么这个项目无疑是一个值得探索的实践案例。 博客链接(http://blog.csdn.net/yjflinchong)可能包含关于该项目的更多详细信息,包括使用教程、问题解答和可能的更新。通过...

    javaswing学习教程

    Java Swing 是Java GUI(图形用户界面)开发的重要库,它属于Java Foundation Classes (JFC) 的一部分。Swing 提供了一系列组件,用于构建美观、功能丰富的桌面应用程序。本教程将全面解析Java Swing的相关知识点,...

    java swing计算器程序idea编程环境

    总的来说,通过这个"java swing计算器程序idea编程环境"的学习,你不仅可以掌握Swing组件的使用,事件监听的处理方式,还能熟悉IntelliJ IDEA的开发流程。这不仅锻炼了你的Java编程技能,也为你在实际项目开发中使用...

    java swing调色板程序

    通过这些知识点的综合运用,我们可以创建一个功能完备且用户友好的Java Swing调色板程序,让用户能够轻松地探索和选择颜色,同时还能持久保存他们的个性化设置。在实际开发过程中,还需要考虑用户体验、错误处理和...

Global site tag (gtag.js) - Google Analytics