現有物件狀態的任何變更稱為事件,事件處理程序的設計是為了偵聽特定事件並相應地執行一些邏輯操作,AWT 元件可以註冊到偵聽使用者事件的所需偵聽器,然後相應地處理事件。在本主題中,我們將學習 Java 中的事件處理。
開始您的免費軟體開發課程
網頁開發、程式語言、軟體測試及其他
以下是如何使用 AWT 事件處理程序的語法:
// importing awt package import java.awt.*; // create a class extending Frame component class <className> extends Frame implements <ListenerInterface>{ // override methods of implemented interface @Override public void <methodName>(){ // do required actions on event happened } <className>(){ component.addActionListerner(listenerClassobject); // register component with listener }}
上面的語法展示如何在 java awt 中使用監聽器。
在上面的語法中
以下是 java awt 中可用的不同類型的偵聽器:
Event | ListenerInterface | Description |
ActionEvent | ActionListener | Produced on click of a button, selection of an item from menu or other. |
MouseEvent | MouseListener | Produced when mouse event takes place like moved, pressed, click, double-click or enter/exit of mouse pointer into a specified area. |
KeyEvent | KeyListener | Produced on the press of the key. |
ItemEvent | ItemListener | Produced when the checkbox is checked or unchecked or item present in a list is clicked. |
WindowEvent | WindowListener | Produced on different operations performed on a window. |
ComponentEvent | ComponnetEventListener | Produced when a component is made visible, hidden, moved or changes are made in component size. |
ContainerEvent | ContainerListener | Produced when a component is inserted or deleted from a container. |
FocusEvent | FocusListener | Produced when a component attains or loses keyboard focus. |
AdjustmentEvent | AdjustmentListener | Produced when changes are made to using the scrollbar. |
MouseWheelEvent | MouseWheelListener | Produced when the mouse wheel is rotated. |
TextEvent | TextListener | Produced whenever there is a change in the value of textarea or textfield. |
以下是 java awt 中處理事件所涉及的主要步驟:
以下範例展示如何在 java awt 中使用事件處理程序:
代碼:
package com.edubca.awtdemo; // importing important packages import java.awt.*; import java.awt.event.*; public class EventHandlerDemo { private Frame parentFrame; private Label headerTitle; private Label status; private Panel panel; public EventHandlerDemo(){ prepareInterface(); } public static void main(String[] args){ EventHandlerDemo awtdemo = new EventHandlerDemo(); awtdemo.showEventHandlingDemo(); } private void prepareInterface(){ parentFrame = new Frame("Java Event Handling Example"); parentFrame.setSize(400,400); parentFrame.setLayout(new GridLayout(3, 1)); parentFrame.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent windowEvent){ System.exit(0); } }); headerTitle = new Label(); headerTitle.setAlignment(Label.CENTER); status = new Label(); status.setAlignment(Label.CENTER); status.setSize(350,100); panel = new Panel(); panel.setLayout(new FlowLayout()); parentFrame.add(headerTitle); parentFrame.add(panel); parentFrame.add(status); parentFrame.setVisible(true); } private void showEventHandlingDemo(){ headerTitle.setText("Handling Button Click Event"); Button firstButton = new Button("First Button"); Button secondButton = new Button("Second Button"); Button thirdButton = new Button("Third Button"); firstButton.setActionCommand("First Button Clicked"); secondButton.setActionCommand("Second Button Clicked"); thirdButton.setActionCommand("Third Button Clicked"); //registering button with listener firstButton.addActionListener(new ButtonClickEventListener()); //registering button with listener secondButton.addActionListener(new ButtonClickEventListener()); //registering button with listener thirdButton.addActionListener(new ButtonClickEventListener()); panel.add(firstButton); panel.add(secondButton); panel.add(thirdButton); parentFrame.setVisible(true); } // inner class implementing Action Listener private class ButtonClickEventListener implements ActionListener{ // overriding method public void actionPerformed(ActionEvent e) { String command = e.getActionCommand(); // do different actions according to different commands if( command.equals( "First Button Clicked" )) { status.setText ("First Button Clicked."); } else if( command.equals( "Second Button Clicked" ) ) { status.setText ("Second Button Clicked."); } else { status.setText ("Third Button Clicked."); } } } }
上面的程式展示如何在java中使用awt事件處理程序。它涉及實現所需的偵聽器介面並實現其方法,然後向指定的偵聽器註冊元件。
在上面的範例中,我們有三個按鈕,點擊按鈕即可更改頁腳中的標籤。上述程式執行後,彈出如下視窗:
點擊第一個按鈕時,將在下面產生以下文字:
按一下第二個按鈕時,將產生以下輸出:
點選第三個按鈕時,將產生以下輸出:
因此我們可以看到,不同按鈕的點擊事件被監聽器偵測到,並執行不同的功能。
上面的文章讓我們對java awt中的事件處理程序有一個清晰的了解。
以上是Java 中的事件處理的詳細內容。更多資訊請關注PHP中文網其他相關文章!