In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to realize the four events of AWT by Java". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "how Java realizes the four major events of AWT"!
Classification of common events
Java AWT events can be simply divided into form events (WindowEvent), mouse events (MouseEvent), keyboard events (KeyEvent), action events (ActionEvent) and other events.
Form event
Form events are the foundation of GUI applications, where other components are usually placed directly or indirectly in the form. When opening, closing, activating, and deactivating in a form, JDK provides a class WindowEvent to represent these form events, defines a class with a WindowListener interface as the form listener, and then binds the form object to the form listener through the addWindowlistener () method.
Package AWT; import java.awt.*; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; public class ShiPeiQi {public static void main (String [] args) {final Frame f=new Frame ("WindowEvent"); f.setSize (400300); f.setLocation (300200); f.setVisible (true) F.addWindowListener (new WindowListener () {@ Override public void windowOpened (WindowEvent e) {System.out.println ("windowOpened---- forms Open event") } @ Override public void windowClosing (WindowEvent e) {System.out.println ("windowClosing---- form closing event"); ((Window) e.getComponent ()) .dispose () } @ Override public void windowClosed (WindowEvent e) {System.out.println ("windowClosed---- forms close event");} @ Override public void windowIconified (WindowEvent e) {System.out.println ("windowIconified--- forms icon event") } @ Override public void windowDeiconified (WindowEvent e) {System.out.println ("windowDeiconified- forms de-iconing event");} @ Override public void windowActivated (WindowEvent e) {System.out.println ("windowActivated---- forms Activation event") } @ Override public void windowDeactivated (WindowEvent e) {System.out.println ("windowDeactivated---- forms deactivation event");}});}}
After running the program, clicking the minimize button on the form, the icon on the taskbar, and clicking the close button on the form will trigger the events in the screenshot below.
Mouse event
We usually select computers, switch interfaces and other operations, which are called mouse events, including mouse press, mouse release, mouse click, etc., so we provide a MouseEvent class to represent mouse events. Basically all components can generate mouse events. When dealing with mouse events, we need to implement the MouseLinstener interface to define listeners or inherit the adapter MouseAdapter class. The AddMouseListener () method is then called to bind the listener to the event source object.
Package AWT; import java.awt.*; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; public class Example12 {public static void main (String [] args) {final Frame f=new Frame ("MouseEvent"); f.setLayout (new FlowLayout ()); f.setSize (300200); f.setLocation (300200); f.setVisible (true); Button but=new Button ("Button"); f.add (but) / / set a listener but.addMouseListener (new MouseListener () {@ Override public void mouseClicked (MouseEvent e) {System.out.println ("mouseClicked---- mouse complete click event") for the button mouse) } @ Override public void mousePressed (MouseEvent e) {System.out.println ("mousePressed- mouse down event");} @ Override public void mouseReleased (MouseEvent e) {System.out.println ("mouseReleased---- mouse release event") } @ Override public void mouseEntered (MouseEvent e) {System.out.println ("mouseEntered- Mouse enters Button area event");} @ Override public void mouseExited (MouseEvent e) {System.out.println ("mouseExited- Mouse remove Button area event") });}}
When the mouse clicks the button and then moves the mouse out of the button area, the following monitoring events are triggered
Triggered event
When the mouse acts on the button, the listener gets the corresponding event object, which triggers the corresponding event and prints it out.
Public void mouseClicked (MouseEvent e) {if (e.getButton () = = e.BUTTON1) {System.out.println ("mouse completes Zuoji event");} if (e.getButton () = = e.BUTTON2) {System.out.println ("mouse completion middle click event");} if (e.getButton () = = e.BUTTON3) {System.out.println ("mouse completion right click event") }}
The MouseEvent class defines different constants for the mouse keys. The constant key value of the operated key is obtained by the getButton () method of the MouseEvent class to determine which key is operated, and the number of clicks can be obtained through the getClickCount () method of the MouseEvent object.
Keyboard event
In the course of our operation, the keyboard often interacts with the user, such as pressing and releasing the keyboard. These operations are defined as keyboard events. A KeyEvent class is defined in JDK to represent keyboard events. Listener objects that deal with KeyEvent time need to implement the KeyListener interface or inherit the KeyAdapter class.
Package AWT; import java.awt.*; import java.awt.event.KeyAdapter; import java.awt.event.KeyEvent; public class Example13 {public static void main (String [] args) {Frame f=new Frame ("KeyEvent"); f.setLayout (new FlowLayout ()); f.setSize (400300); f.setLocation (300200); / / create text box object TextField tf=new TextField (30); f.add (tf) F.setVisible (true); tf.addKeyListener (new KeyAdapter () {@ Override public void keyPressed (KeyEvent e) {/ / returns the integer value int KeyCode=e.getKeyCode () corresponding to the key pressed; / / returns the string description of the key String s=KeyEvent.getKeyText (KeyCode) System.out.print ("input is:" + s + ","); System.out.println ("corresponding KeyCode is:" + KeyCode);}});}}
Running result
TextField only allows you to edit single-line text, through the
Input characters to trigger keyboard events. The KeyEvent class returns the integer value corresponding to the input content by calling the getKeyCode () method. There is a static method getKeyText (int keyCode) in the KeyEvent class, which can return the keystroke content in the form of String. The following figure shows the KeyCode value corresponding to the input in the screenshot above.
Action event
An action event indicates that an action has occurred, for example, when a file is closed, either through the keyboard or with the mouse, that is, the action event is triggered when the button is closed. Action events are represented by the ActionEvent class, and the listener object that handles the ActionEvent event needs to implement the ActionListener interface. When the listener object listens to the action, it handles events similar to events such as "button press".
At this point, I believe you have a deeper understanding of "Java how to achieve the four major events of AWT". 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.