Java interface definition, usage, and event listener methods

Source: Internet
Author: User
Tags event listener

As is known to all, classes are definitions of methods and properties that are common to all objects. That is, a class is an abstract rule that defines a class as nothing more than two uses, or a generated object calls its methods, or is used to inherit from other classes, and for the latter purpose we need to define an interface when the subclass must override the method of the parent class. The
interface itself is also a class, but the interface defines a method that does not have any method-body implementations, and this is the rule of the interface definition, which can only define method names, return values, and parameter tables, not implementations. Also, an interface cannot be used to create objects directly.
When defining interfaces, a class class can implement multiple interfaces, and a class class inherits only one class, which means that we can implement multivariate inheritance of class classes by defining the interface's methods to accomplish multiple valid events.
Java code

public interface netjava{  
    //Create a Netjava interface public  
    final static String comname= "CQ";  
    public void Draw (int x1,int y1,int x2,int y2);  
}  
Public interface Other {  
    //Create a other interface  
    String doother ();  
}  
      
public class Impoval implements netjava,other{  
    //executing method body in two interfaces public  
    void Draw (int x1,int y1,int x2,int y2) { C12/>system.out.println ("I Draw a circle");  
        System.out.println ("producer" +netjava.comname);  
    }  
    The public string  DoOther () {  
        string s= "implements methods in other classes";  
        return s;  
    }  
}  
      
public class Text {public  
    static void Main (String args[]) {  
        impoval im=new impoval ();  
        Im.draw (0, 0, 0, 0);  
        Im.doother ();  
        Other cs=new impoval ();  
        Cs.doother ();  
    }  

An event in the process of processing often need to listen to the steps that the event is being processed, such as we write a landing box in Java, how to click on the login by the login button to respond to the event, it is very important, in future advanced programming will need to perform such a step, Below I will simply implement the basic monitoring method of the event, the code is as follows:

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/Java/

Java code

Import Java.awt.FlowLayout;  
Import javax.swing.*;  
        public class frame{public static void Main (String args[]) {frame jf=new frame ();  
    Jf.showframe ();  
        public void Showframe () {JFrame frame=new JFrame ();  
        Create a streaming layout manager FlowLayout fl=new FlowLayout ();  
        Frame.setlayout (FL);  
        Create a window component JLabel zla=new JLabel ("account");  
        JLabel mla=new JLabel ("password");  
        JTextField tf=new JTextField (21);  
        JPasswordField pf=new JPasswordField (21);  
        JButton loginbu=new JButton ("login");  
        JButton cancelbu=new JButton ("cancellation");  
        Jcheckbox cb=new Jcheckbox ("Remember Account");  
        Add Component Frame.add (ZLA);  
        Frame.add (TF);  
        Frame.add (MLA);  
        Frame.add (PF);  
        Frame.add (Loginbu);  
        Frame.add (CANCELBU);  
        Frame.add (CB);  
        Frame.settitle ("interface 2.0");  
        Frame.setsize (300, 200); Frame.setlocationrelativeto (NULL); 
        Frame.setresizable (FALSE);  
        Frame.setdefaultcloseoperation (3);  
        Frame.setvisible (TRUE);  
        Sets the response of the button Loginbu.setactioncommand ("Login");  
        Cancelbu.setactioncommand ("Cancel");  
        Listener lis=new Listener ();  
        Loginbu.addactionlistener (LIS);  
        Cancelbu.addactionlistener (LIS); }  
}

Java code

Import java.awt.event.ActionEvent;  
public class Listener implements java.awt.event.actionlistener{public  
    void actionperformed (ActionEvent e) {  
        String Com=e.getactioncommand ();  
        if (com.equals ("login")) {  
            System.out.println ("Login succeeded");  
        }  
        else if (Com.equals ("Cancel")) {  
            System.out.println ("Cancel");}}}  

Through the first two weeks of Java learning does feel like a benefit, in Lanji learning let me find the Learning programming language courage and confidence, the first one, in the future road, I believe that will encounter many difficulties and frustrations, here in addition to summing up the previous knowledge points, but also to give themselves a little encouragement, Hope that in the future study can continue to work hard to create good results.

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.