Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to understand Java event response

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly introduces "how to understand Java event response". In daily operation, I believe many people have doubts about how to understand Java event response. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubts about "how to understand Java event response"! Next, please follow the editor to study!

In GUI, we saw how to use a graphical tree to organize a graphical interface. However, such a graphical interface is static. We can't operate the interface interactively. The graphical elements of GUI need to add event response (event handling) in order to get a dynamic graphical interface.

Elements, events, listeners

We mentioned many graphic elements in the article GUI. There are events (Event) that can occur on these graphical elements, such as:

Click the button

Drag the scroll bar

Select menu

Events in Java are represented by objects, such as ActionEvent. Graphical objects that work for each event, such as buttons, scroll bars, menus.

The so-called interactive GUI means that when the above events occur, there will be corresponding actions, such as:

Change the color

Change the contents of the window

Pop-up menu

Each action is for an event. We put the action in a listener (ActionListener) and then let the listener monitor (a graphical object) for events. When an event occurs, the action in the listener occurs.

Therefore, a responsive GUI is the result of the interaction among graphic objects, event objects and listening objects. We already know how to create drawing objects. We need to add listeners to graphical objects and let listeners capture events.

Button response

Let's implement a responsive button. After clicking the button, the color of the panel changes, as shown below:

Import javax.swing.*;import java.awt.event.*;import java.awt.*;public class HelloWorldSwing {private static void createAndShowGUI () {JFrame frame = new JFrame ("HelloWorld"); frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE); / / Pane's layout Container cp = frame.getContentPane (); cp.setLayout (new FlowLayout ()); / / add interactive panel to ContentPane cp.add (new ButtonPanel ()) / / show the window frame.pack (); frame.setVisible (true);} public static void main (String [] args) {Runnable tr = new Runnable () {public void run () {createAndShowGUI ();}}; javax.swing.SwingUtilities.invokeLater (tr) }} / * JPanel with Event Handling * / class ButtonPanel extends JPanel {public ButtonPanel () {JButton yellowButton = new JButton ("Yellow"); JButton redButton = new JButton ("Red"); this.add (yellowButton); this.add (redButton); / * * register ActionListeners * / ColorAction yellowAction = new ColorAction (Color.yellow) ColorAction redAction = new ColorAction (Color.red); yellowButton.addActionListener (yellowAction); redButton.addActionListener (redAction);} / * ActionListener as an inner class * / private class ColorAction implements ActionListener {public ColorAction (Color c) {backgroundColor = c } / * Actions * / public void actionPerformed (ActionEvent event) {setBackground (backgroundColor); / / outer object, JPanel method repaint ();} private Color backgroundColor;}}

Above, we use an inner class ColorAction to implement the ActionListener interface. This is done to make it easier for listeners to call members of graphical objects, such as the setBackground () method.

The actionPerformed () method of ActionListener must be overridden. This method contains the corresponding actions for the event. The argument to this method is the event object, which listens for events of type ActionEvent. ActionEvent is a high-level class, and Java finds typical events (clicks) that occur to graphical objects (buttons) as events.

The object generated by ColorAction is the listener object.

We added the corresponding listener objects for the two buttons JButton. When an event occurs, the corresponding action will occur.

At this point, the study on "how to understand Java event response" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report