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

}

 

你可能感兴趣的:(java,swing,F#)