分享
 
 
 

Swing读书笔记事件处理

王朝system·作者佚名  2008-05-31
窄屏简体版  字體: |||超大  

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

、打开、退出等。

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

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

}

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有