The basics of Java's event handling mechanism

Source: Internet
Author: User

The Java event-handling mechanism is what I should be building on swing, because in the SE phase, the Java event processing is to listen to the graphical interface, I said the general, of course, there are special, for events, for I should not be unfamiliar with the significance of this event and B/ s development inside the event is the same, the same also has the event source, event monitoring and event response, this mechanism is to better complete human-computer interaction, this thing is also in the drawing of the focus of anti-technology, the principle is not difficult to understand, difficult to understand is the underlying factors of event processing, for this, the same, You should finish the function first and then do some understanding. Here are some of the code that I learned in the process:

Package Test;import Java.awt.*;import javax.swing.*;//import java.awt.event.*;import java.awt.event.ActionEvent; Import java.awt.event.actionlistener;//time processing mechanism to use the package import java.awt.event.*;
public class Shijian extends JFrame implements Actionlistener{jpanel MP = NULL; JButton Jb1=null; JButton jb2=null;public static void Main (string[] args) {Shijian p1=new Shijian (); Public Shijian () {mp=new JPanel (); Jb1=new JButton ("Black"); Jb2=new JButton ("Red"); This.add (Jb1,borderlayout.north); Mp.setbackground (Color.Black); This.add (MP) This.add (Jb2,borderlayout.south);//Register a listener Jb1.addactionlistener (this ); Jb2.addactionlistener (this); Jb1.setactioncommand ("AA"); Jb2.setactioncommand ("BB"); This.setsize (200,150); This.setdefaultcloseoperation (Jframe.exit_on_close); this.setvisible (true);} @Overridepublic void actionperformed (ActionEvent e) {if (E.getactioncommand (). Equals ("AA")) {System.out.println (" You clicked on the Black ");} else if (E.getactioncommand () Equals ("BB")) {System.out.println ("you clicked Red");} else {System.out.print ("you clicked Other");}}}



The basics of Java's event handling mechanism

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.