现有对象状态的任何变化都被称为事件,事件处理程序的设计是为了侦听特定事件并相应地执行一些逻辑操作,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中文网其他相关文章!