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 monitor operation in javaGUI programming

2025-03-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

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

How to monitor in javaGUI programming. In view of this problem, this article introduces the corresponding analysis and answer in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.

When you click on the component component in Frame, it will have a corresponding effect, but it must be monitored to determine which object, which operation, but if you use cup for active monitoring, it will consume a lot of resources, so there is passive monitoring, that is, the corresponding events will automatically execute the relevant code.

Button snooping:

Instance 1:

Import java.awt.*;import java.awt.event.*;public class MoniterStart {public static void main (String [] args) {Frame f = new Frame (); Button b = new Button ("Press"); f.add (bBorderLayout.centter); MyMonitor bh = new MyMonitor (); b.addActionListener (bh); f.setBounds (100,100,300,300); f.setVisible (true);}} class MyMonitor implements ActionListener {public void actionPerformed (ActionEvent e) {System.out.println ("aAH");}}

For passive monitoring, you have to determine where and what type of componet is sent. We cannot provide all the information, so we have built an event corresponding to an object, which implements an interface. When an event occurs, the corresponding event method will be called. That is, (implements ActionListener), and the corresponding implementation method requires it to provide event actions.

Instance 2:

Import java.awt.*;import java.awt.event.*;public class Monitor2 {public static void main (String [] args) {Frame F1 = new Frame (); Button b1 = new Button ("Start"); Button b2 = new Button ("Stop"); b1.setActionCommand ("The End"); f1.add (b1m BorderLayout.CENTER); f1.add (b2Mer BorderLayout.SOUTH); MyMonitor1 bh = new MyMonitor1 (); b1.addActionListener (bh); b2.addActionListener (bh); f1.setBounds (100,100,300,300) F1.setVisible (true);}} class MyMonitor1 implements ActionListener {public void actionPerformed (ActionEvent e) {System.out.println ("aAH you get it" + e.getActionCommand ());}}

Note: when a listener listens to multiple button objects, we can distinguish it with setActionCommand, which is equivalent to an attribute or a tag.

TextField (text input monitoring)

Instance 3

Import java.awt.*;import java.awt.event.*;public class TfMonitor {public static void main (String [] args) {new TFFrame ();}} class TFFrame {TFFrame () {Frame f = new Frame ("TF"); TextField tf = new TextField (); f.add (tf); tf.addActionListener (new TFFrameMonitor ()); f.setBounds (100,100,300,300); f.setVisible (true);} / * class TFFrame extends Frame {TFFrame () {/ Frame f = new Frame ("TF") TextField tf = new TextField (); add (tf); tf.addActionListener (new TFFrameMonitor ()); / / f.setBounds (100,100,300,300); setBounds (100,100,300,300); setVisible (true);} * / class TFFrameMonitor implements ActionListener {public void actionPerformed (ActionEvent e) {TextField text = (TextField) e.getSource (); System.out.println (text.getText ()); / / text.setText (null);}}

TextField belongs to a component of input text class, one of which can be directly constructed by Frame construction method to generate an object of Frame, and the other can directly inherit Frame, which is equivalent to the existence of a Frame class, and you can directly use the methods in Frame.

Note: the setText (null) above is equivalent to automatically emptying the data in the input box when the enter key is pressed at the end of the input. If not, it will continue to exist.

Instance 4:

Import java.awt.Frame;import java.awt.TextField;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;public class TFFrame2Monitor {public static void main (String [] args) {new TFFrame2 ();}} class TFFrame2 extends Frame {TFFrame2 () {TextField tf = new TextField (); add (tf); tf.addActionListener (new TFFrameMonitor2 ()); tf.setEchoChar ('*'); setBounds (100,100,300,300); setVisible (true) }} class TFFrameMonitor2 implements ActionListener {public void actionPerformed (ActionEvent e) {TextField text = (TextField) e.getSource (); System.out.println (text.getText ()); text.setText (null);}}

Just add a setEchoChar to the Text Filed object to represent the surface display after input. If not, what is entered will be displayed, and if added, it will be overwritten as well as entering a password.

This is the answer to the question about how to monitor and operate in javaGUI programming. I hope the above content can be of some help to you. If you still have a lot of doubts to be solved, you can follow the industry information channel to learn more about it.

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