今天图老师小编给大家展示的是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
看过《Swing读书笔记事件处理》的人还看了以下文章 更多>>