2.6 事件處理
-
- 2.6 事件處理
-
- 2.6.1 GUI事件處理機制
- 2.6.2 GUI中常見事件和事件監聽器
-
- 2.6.2.1 事件
- 2.6.2 事件監聽器
- 2.6.3 案例
2.6 事件處理
前面介紹了如何放置各種元件,進而得到了豐富多彩的圖形界面,但這些界面還不能響應使用者的任何操作。比如單擊前面所有視窗右上角的“X”按鈕,但視窗依然不會關閉。因為在 AWT 程式設計中 ,所有使用者的操作,都必須都需要經過一套事件處理機制來完成,而 Frame 群組件本身并沒有事件處理能力 。
2.6.1 GUI事件處理機制
定義:
當在某個元件上發生某些操作的時候,會自動的觸發一段代碼的執行。
在GUI事件處理機制中涉及到4個重要的概念需要了解:
事件源(Event Source):操作發生的場所,通常指某個元件,例如按鈕、視窗等;
事件(Event):在事件源上發生的操作可以叫做事件,GUI會把事件都封裝到一個Event對象中,如果需要知道該事件的詳細資訊,就可以通過Event對象來擷取。
事件監聽器(Event Listener):當在某個事件源上發生了某個事件,事件監聽器就可以對這個事件進行處理。
注冊監聽:把某個事件監聽器(A)通過某個事件(B)綁定到某個事件源©上,當在事件源C上發生了事件B之後,那麼事件監聽器A的代碼就會自動執行。
使用步驟:
1.建立事件源元件對象;
2.自定義類,實作XxxListener接口,重寫方法;
3.建立事件監聽器對象(自定義類對象)
4.調用事件源元件對象的addXxxListener方法完成注冊監聽
案例:
完成下圖效果,點選确定按鈕,在單行文本域内顯示 hello world:
示範代碼:
public class EventDemo1 {
Frame frame = new Frame("這裡測試事件處理");
//事件源
Button button = new Button("确定");
TextField tf = new TextField(30);
public void init(){
//注冊監聽
button.addActionListener(new MyActionListener());
//添加元件到frame中
frame.add(tf);
frame.add(button,BorderLayout.SOUTH);
//設定frame最佳大小并可見
frame.pack();
frame.setVisible(true);
}
//自定義事件監聽器類
private class MyActionListener implements ActionListener{
@Override
public void actionPerformed(ActionEvent e) {
System.out.println("使用者點選了确定按鈕");
tf.setText("hello world");
}
}
public static void main(String[] args) {
new EventDemo1().init();
}
}
package demo01;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class EventDemo1 {
Frame frame=new Frame("這裡測試事件處理");
TextField tf=new TextField(30);
Button ok =new Button("确定");
public void init() {
//注冊監聽(通過内部類實作)
ok.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e) {
System.out.println("ok按鈕被執行了...");
tf.setText("hello world");
}
});
//把tf和ok放入到Frame中
frame.add(tf,BorderLayout.NORTH);
frame.add(ok);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new EventDemo1().init();
}
}
2.6.2 GUI中常見事件和事件監聽器
事件監聽器必須實作事件監聽器接口, AWT 提供了大量的事件監聽器接口用于實作不同類型的事件監聽器,用于監聽不同類型的事件 。 AWT 中提供了豐富的事件類,用于封裝不同元件上所發生的特定操作, AWT 的事件類都是 AWTEvent 類的子類 , AWTEvent是 EventObject 的子類。
2.6.2.1 事件
AWT把事件分為了兩大類:
1.低級事件:這類事件是基于某個特定動作的事件。比如進入、點選、拖放等動作的滑鼠事件,再比如得到焦點和失去焦點等焦點事件。
事件 | 觸發時機 |
---|---|
ComponentEvent | 元件事件 , 當 元件尺寸發生變化、位置發生移動、顯示/隐藏狀态發生改變時觸發該事件。 |
ContainerEvent | 容器事件 , 當容器裡發生添加元件、删除元件時觸發該事件 。 |
WindowEvent | 視窗事件, 當窗 口狀态發生改變 ( 如打開、關閉、最大化、最 小化)時觸發該事件 。 |
FocusEvent | 焦點事件 , 當元件得到焦點或失去焦點 時觸發該事件 。 |
KeyEvent | 鍵盤事件 , 當按鍵被按下、松開、單擊時觸發該事件。 |
MouseEvent | 滑鼠事件,當進行單擊、按下、松開、移動滑鼠等動作 時觸發該事件。 |
PaintEvent | 元件繪制事件 , 該事件是一個特殊的事件類型 , 當 GUI 元件調 用 update/paint 方法 來呈現自身時觸發該事件,該事件并非專用于事件處理模型 。 |
2.進階事件:這類事件并不會基于某個特定動作,而是根據功能含義定義的事件。
事件 | 觸發時機 |
---|---|
ActionEvent | 動作事件 ,當按鈕、菜單項被單擊,在 TextField 中按 Enter 鍵時觸發 |
AjustmentEvent | 調節事件,在滑動條上移動滑塊以調節數值時觸發該事件。 |
ltemEvent | 選項事件,當使用者選中某項, 或取消選中某項時觸發該事件 。 |
TextEvent | 文本事件, 當文本框、文本域裡的文本發生改變時觸發該事件。 |
2.6.2 事件監聽器
不同的事件需要使用不同的監聽器監聽,不同的監聽器需要實作不同的監聽器接口, 當指定事件發生後 , 事件監聽器就會調用所包含的事件處理器(執行個體方法)來處理事件 。
事件類别 | 描述資訊 | 監聽器接口名 |
---|---|---|
ActionEvent | 激活元件 | ActionListener |
ItemEvent | 選擇了某些項目 | ItemListener |
MouseEvent | 滑鼠移動 | MouseMotionListener |
MouseEvent | 滑鼠點選等 | MouseListener |
KeyEvent | 鍵盤輸入 | KeyListener |
FocusEvent | 元件收到或失去焦點 | FocusListener |
AdjustmentEvent | 移動了滾動條等元件 | AdjustmentListener |
ComponentEvent | 對象移動縮放顯示隐藏等 | ComponentListener |
WindowEvent | 視窗收到視窗級事件 | WindowListener |
ContainerEvent | 容器中增加删除了元件 | ContainerListener |
TextEvent | 文本字段或文本區發生改變 | TextListener |
2.6.3 案例
案例一:
通過ContainerListener監聽Frame容器添加元件;
通過TextListener監聽TextFiled内容變化;
通過ItemListener監聽Choice條目選中狀态變化;
示範代碼一:
import java.awt.*;
import java.awt.event.ContainerAdapter;
import java.awt.event.ContainerEvent;
import java.awt.event.TextEvent;
import java.awt.event.TextListener;
public class ListenerDemo1 {
public static void main(String[] args) {
Frame frame = new Frame("這裡測試監聽器");
//建立元件(事件源)
TextField tf = new TextField(30);
Choice names=new Choice();
names.add("lixu");
names.add("love");
names.add("tanghao");
//給文本域添加TextListener,監聽内容的變化
tf.addTextListener(new TextListener() {
@Override
public void textValueChanged(TextEvent e) {
String text=tf.getText();
System.out.println("目前内容:"+tf.getText());;
}
});
//給下拉選擇框加ItemListener,監聽條目選項的變化
names.addItemListener(new ItemListener() {
public void itemStateChanged(ItemEvent e) {
Object item=e.getItem();
System.out.println("目前選中的條目為:"+item);
}
});
//給frame注冊ContainerListener監聽器,監聽容器中元件的添加
frame.addContainerListener(new ContainerAdapter() {
@Override
public void componentAdded(ContainerEvent e) {
Component child = e.getChild();
System.out.println("容器中添加了新元件:"+child);
}
});
//添加tf到frame
Box hbox=Box.createHorizontalBox();
hbox.add(names);
hbox.add(tf);
frame.add(hbox);
//設定frame最佳大小并可見
frame.pack();
frame.setVisible(true);
}
}
案例2:
給Frame設定WindowListner,監聽使用者點選 X 的動作,如果使用者點選X,則關閉目前視窗
示範代碼2:
import java.awt.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
public class ListenerDemo2 {
public static void main(String[] args) {
Frame frame = new Frame("這裡測試WindowListener");
frame.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
frame.setBounds(200,200,500,300);
frame.setVisible(true);
}
}