In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article focuses on "what is the way to handle Java GUI events". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "what is the way to handle Java GUI events?"
Event handling mainly involves: event source, event handler
In GUI, the event source is the place where the event occurs, usually the various components, such as the clicked button; the event is the user's operation to the interface, such as the keyboard event triggered by the operation of the keyboard; and the event handler is the program that handles the received event, also known as the listener.
Related classes and interfaces that implement event handling in the java.awt.event package
Classes ending in Event: event classes, such as ActionEvent, WindowEvent, MouseEvent, KeyEvent
Interfaces ending in Listener: are listener interfaces related to specific events. Each interface defines methods that need to be implemented by a specific listener, and is a concrete implementation of event handlers, such as ActionListener, WindowListener, MouseListerer, KeyListener.
Classes ending in Adapter (that is, adapter classes) are special interfaces that have implemented all the methods and are meant to simplify the listener approach introduced by the code by rewriting the required methods. However, due to the single inheritance feature of Java, if you want to use multiple listeners or this class already has a parent class, you cannot inherit the adapter class, such as WindowAdapter, MouseAdapter, KeyListener, no ActionEvent.
Note:
Event handlers, that is, listeners, in order to be able to handle a certain type of event, must implement the interface relative to the event type, that is, become a class object that implements a certain interface.
The event is passed in through the method contained by the event handler, which is the method that must be implemented when implementing the interface.
Such as the void actionPerformed (ActionEvent e) method in the ActionListener interface.
For example, clicking the button corresponds to the action event (ActionEvent). The button event handler is the class object that implements the Listener interface corresponding to the action event. You need to call the addActionListener () method of the button to register. This class is to override the void actionPerformed (ActionEvent e) method in the ActionListener interface.
Import javax.swing.*;import java.awt.*;import java.awt.event.ActionEvent;import java.awt.event.ActionListener; public class Test {public static void main (String [] args) {JFrame frame = new JFrame ("understanding event handling"); frame.setDefaultCloseOperation (3); frame.setLayout (null); / / create button object JButton button = new JButton ("Please click this button") Button.setBounds (120Cool 60ju 120je 30); frame.add (button); / / create and register button listeners with the parameter of event handler object ButtonHandler buttonHandler = new ButtonHandler (); button.addActionListener (buttonHandler); / / methods of authorization handling associated with click events frame.setBounds (400200400200); frame.setVisible (true) }} class ButtonHandler implements ActionListener {public void actionPerformed (ActionEvent e) {System.out.println ("Click the button once");}}
Effect picture:
As you can see from the above example, writing event handling can be roughly divided into three steps:
Create a component object and consider which event or events the component object is related to. For example, to create a button object, the related event is the action event, that is, ActionEvent.
Write the event handler class of the component object, that is, implement the listener interface corresponding to the event, for example, write the event handler ButtonHandler class, implement the ActionListener interface corresponding to ActionEvent, specifically, implement the void actionPerformed (ActionEvent e) method in the interface, and add the code to handle the event in this method.
Create an instance of the event handler class and call the add method of the corresponding event of the component object to register the listener, such as calling the button's addActionListener (ActionListener 1) method to add an instance of the ButtonHandler class.
Example:
Import javax.swing.*;import java.awt.*;import java.awt.event.*; public class Test {public static void main (String [] args) {JFrame frame = new JFrame ("in depth event handling"); frame.setDefaultCloseOperation (3); frame.setLayout (null); / / create prompt message JLabel label1 = new JLabel ("Please move the mouse within the form") Label1.setBounds (155pence200pc25); frame.add (label1); JLabel label2 = new JLabel ("or hold down the left mouse button to drag the mouse"); label2.setBounds (15pence30pence200pc25); frame.add (label2); / / create a text box object to detect JTextField text = new JTextField (30); text.setBounds (1555jing200jing30); frame.add (text) / / register the listener. The parameter is event handler object MouseListenerImp mouse = new MouseListenerImp (text); / / event handler class instantiates frame.addMouseListener (mouse); frame.addMouseMotionListener (mouse); frame.addWindowListener (mouse); frame.setBounds (500250300150); frame.setVisible (true) }} / / write event handling object classes to implement the relevant interfaces of mouse forms class MouseListenerImp implements MouseListener, MouseMotionListener, WindowListener {JTextField text; public MouseListenerImp (JTextField text) {this.text = text;} public void mouseDragged (MouseEvent e) {/ / provide mouse coordinates String s = "drag mouse, coordinate: X =" + e.getX () + "y =" + e.getY () Text.setText (s); / / output} public void mouseEntered (MouseEvent e) {/ / check if the mouse is inside the form String s = "mouse left the form"; text.setText (s); / / output} public void mouseExited (MouseEvent e) {/ / check if the mouse is inside the form String s = "mouse entered the form" Text.setText (s); / / output} public void windowClosing (WindowEvent e) {/ / in order to close System.exit (1) normally in the text box } / / A method that is not intended to be implemented, but because the interface is implemented So to write public void mouseMoved (MouseEvent e) {} public void mouseClicked (MouseEvent e) {} public void mousePressed (MouseEvent e) {} public void mouseReleased (MouseEvent e) {} public void windowOpened (WindowEvent e) {} public void windowClosed (WindowEvent e) {} public void windowIconified (WindowEvent e) {} public void windowDeiconified (WindowEvent e) {} public void windowActivated (WindowEvent e) {} public void windowDeactivated (WindowEvent e) {}}
Effect picture:
The class MouseListenerImp implements MouseListener, MouseMotionListener and WindowListener of the above code implement three interfaces at the same time. To implement the interface, you need to write all the methods in the interface, but if you replace one of the interfaces with the Adapter class, you can] optimize the code, that is, extends MouseAdapter.
Types of events:
Component event ComponentEvent: changes and movements of component sizes
Container event ContainerEvent: components are added or moved
Window event WindowEvent: close window, activate window closure, maximize, minimize
Focus event FocusEvent: the gain and loss of focus
Keyboard event KeyEvent: press or release of key
Mouse event MouseEvent: mouse click and move
Action event ActionEvent: click the button and press enter in the text box
Project event ItemEvent: select an item from the selection box or list box
Adjust event AdjustEvent: move the slider on the scroll bar to adjust the value
Text event TextEvent: changes to text objects
At this point, I believe you have a deeper understanding of "what is the way to handle Java GUI events". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.