Swing读书笔记事件处理

2016-02-19 15:34 15 1 收藏

今天图老师小编给大家展示的是Swing读书笔记事件处理,精心挑选的内容希望大家多多支持、多多分享,喜欢就赶紧get哦!

【 tulaoshi.com - 编程语言 】

  ++ Swing读书笔记事件处理

  2-1:事件处理:

  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窗口,处理窗口的最大、最小化,处理窗口的关闭

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  、打开、退出等。

  以上所述的每种EventListener都是一种interface,里面只有定义这个EventListener的interface,然后在这个类中,把要处理事

  件的程序代码写在此interface的方法中。这是处理事件的标准操作,当然还有其他编写模式,如:利用inner class的匿名类方法

  ,就可以不用在class后面加上implements EventListener的表示法,或是利用Adapter类,就不用一一将interface中的每种方法都

  实现。

  让我们来了解一下,每一种EventListener的interface到底提供了哪几种方法要我们实现(Implement)!我们把事件区分成是由

  Swing或是AWT所引发:

(本文来源于图老师网站,更多请访问http://www.tulaoshi.com/bianchengyuyan/)

  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都可以使用,看下面的例子就知道了:

  EventDemo5.java

  

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo5 extends WindowAdapter implements ActionListener
{
  JButton b1 = null;
  JButton b2 = null;
  
  public EventDemo5()
  {
    JFrame f = new JFrame("EventDemo5");
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new GridLayout(1,2));
    b1 = new JButton("按我有声音喔");
    b2 = new JButton("按我可开新窗口");
    b1.addActionListener(this);
    b2.addActionListener(this);
    contentPane.add(b1);
    contentPane.add(b2);
    f.pack();
    f.show();
    f.addWindowListener(this);
  }
  public void actionPerformed(ActionEvent e)
  {
    if(e.getSource()==b1)//getSource判断哪个按钮被按下了。
      Toolkit.getDefaultToolkit().beep();
    if(e.getSource()==b2)
    {
      JFrame newF = new JFrame("新窗口");
      newF.setSize(200,200);
      newF.show();
    }
  }
  
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
  
  public static void main(String args[])
  {
    new EventDemo5();
  }
}

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

  EventDemo6.java 

  

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class EventDemo6 extends WindowAdapter implements ActionListener
{
  JButton b1 = null;
  JButton b2 = null;
  
  public EventDemo6()
  {
    JFrame f = new JFrame("EventDemo6");
    Container contentPane = f.getContentPane();
    contentPane.setLayout(new GridLayout(1,2));
    b1 = new JButton("按我有声音喔");
    b2 = new JButton("按我可开新窗口");
    b1.addActionListener(this);
    b2.addActionListener(this);
    contentPane.add(b1);
    contentPane.add(b2);
    f.pack();
    f.show();
    f.addWindowListener(this);
  }
  public void actionPerformed(ActionEvent e)
  {
    if((e.getActionCommand()).equals("按我有声音喔")) //getActionCommand()方法会返回按钮上的文字字符串。
      Toolkit.getDefaultToolkit().beep();
    if((e.getActionCommand()).equals("按我可开新窗口"))
    {
      JFrame newF = new JFrame("新窗口");
      newF.setSize(200,200);
      newF.show();
    }
  }
  
  public void windowClosing(WindowEvent e)
  {
    System.exit(0);
  }
  
  public static void main(String args[])
  {
    new EventDemo6();
  }
}

来源:http://www.tulaoshi.com/n/20160219/1609929.html

延伸阅读
标签: Web开发
基本的CSS选择器 熟悉web开发的人员一定对css的选择功能相当的了解,通过css选择功能,我们可以在css样式文件里轻松的将样式应用到选择的页面元素上。jQuery也正是采用了这样的机制,使jQuery的元素选择能力易常强大。 a——选择所有a元素 #specialID——选择匹配id为specialID的元素 .specialClass——选择匹配拥有css类sepcialClass的元素 a...
Introducing LANs and their components 基本内容:描述网络拓扑描述局域网介质描述网络设备 总线拓扑是典型的局域网拓扑结构,所有的设备通过连接到一条同轴电缆或双绞线来进行交流星型拓扑被广泛用于今天的局域网网络环境,它的具体结构是以集线器或中间件为中心向各个方向扩展,以 便连接到不同的设备.智能化的集线器可以控制端口间传输的信...
Describing Ethernet interfaces 基本内容:描述以太网概念描述以太网的帧使用网络的效用 以太网标准实施在TCP/IP协议模型的网络接口层以太网网络的三个重要组成部分是:1,以太网包又称为帧,这些数据单元在网络上传输2,以太网访问方法CSMA/CD用来控制包的传输和信息的流量控制3,硬件电缆,连接器,电路,用于和网络之间的数据传输 CSMA/CD...
Introducing the TCP/IP model 基本内容:描述网络模式的基本原理描述TCP/IP模型的层次描述基本对等交流和相关的协议 数据传输协议是用于设备间交流信息的规则,描述了:语法:数据格式和编码语义:控制信息和纠错能力时间选择:速度的匹配和先后顺序 TCP/IP模式是提供给solaris系统用于数据交流的协议栈下面的映射关系使你更加清楚的理解网络模...
  最近一直在看《Unix网络编程》这本书,觉得这本书很适合初学socket编程的人(比如说我自己 ^_^ )。我一直对于多播的实现比较模糊,在看了书上的多播部分的介绍,才发现在程序中实现一个基本的多播功能是如此简单。   在这里我不想照本宣科的大说什么理论,概念的东东,我的目的是说明在我们的代码中如何实现多播功能。   开...

经验教程

605

收藏

56
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部