Interface and listener in java
1, Definition of inheritance
Before we understand the interface, I think we need to make clear the concept of inheritance. java is an object-oriented programming software, so we need to define classes in java. As we all know, classes have methods and properties. Therefore, when we encounter some classes whose properties and methods are the same, in order not to define these properties repeatedly in these classes Sex and method. At this time, we need to define a parent class, so that these classes can obtain (inherit) these properties and methods from. Just like teachers and students, they are two different classes, but they have the same properties and methods, such as name, gender and age. The definition of inheritance is that a subclass inherits from a parent class the properties and methods whose protection types are public and protected
The inherited syntax format is:
Data protection type + class + class name + extensions + parent class name +
For example, we can look at the program
//Define the first parent class, Fatherstyle public class Fatherstyle{ int age; //Define age String name; //Define name int number; //Defining student number public void show(){ System.out.println("full name: "+name+" Age: "+age+" Student number: "+number); } }
Then we define a subclass and inherit properties and methods from the parent class. After inheritance, we can call the method in the parent class in the direct subclass.
//Define a subclass Sonstyle public class Sonstyle extends Fatherstyle{ public static void main(String[] args) { Sonstyle ui=new Sonstyle(); ui.name="Zhang San"; ui.age=14; ui.number=12367; ui.show(); } }
At this time, we can run the program in the Console and see that the output is
Name: Zhang San age: 14 student ID: 12367
In general (personal understanding):
The advantage that inheritance can bring to us is that it can save many similar codes. For those categories with the same properties, by inheritance, we don't need to write their properties and methods separately for each class, which saves many complicated steps and makes our code easy to understand, However, we need to note that inheritance cannot be multi inheritance. In addition to inheriting java's own classes, classes in java can only inherit one more parent class. (it can be understood that there is only one biological father, and there will be no second one like public class subclass name extends parent class name 1 parent class name 2...) In this way, java will report an error when inheriting. In the java API document, there are many categories that will introduce other methods inherited from other parent classes besides their own unique properties and methods.
2, Interface
After finishing inheritance, let's take a look at the interface, * * some things in our life are fixed features. For example, as we say, everyone has a specific name, every city has a fixed zip code, every university has a specific name, and every express delivery will send to a specific address **These things are quite similar to some fixed things. They can't be changed in a short time. We will always use them. They can't be changed at will. Then we think of the interface
Baidu Encyclopedia defines the interface as:
Java interface is the declaration of a series of methods, which is a collection of some method characteristics. An interface has only the characteristics of a method, and there is no method implementation. Therefore, these methods can be implemented by different classes in different places, and these implementations can have different behaviors (functions)
From this we can know:
In Java, an interface is a method for storing specific properties and defining some abstract methods. This is the format of the interface
1. How to define an interface
public interface interface name
2. The interface properties are defined as follows:
Public static value type property name = value;
Public final value type property name = value;
The meaning of static and final is similar to that of static, final and unchangeable, that is, the attribute in the interface is a fixed value
3. How to define methods in an interface:
public abstract return value type method name (parameter 1, parameter 2, parameter 3);
4. The format for adding an interface to a class is:
public class class name implements interface name 1, interface name 2
In particular, it should be noted that
In the interface, we can add multiple interfaces, which is different from the inheritance mentioned earlier. The properties in the interface can be directly referenced by this class. However, because the methods defined in the interface are abstract, we must rewrite all the methods in the interface in the class that references the interface, We can see the interface in the following program because it is abstract, so it cannot define an object!
Or the above program. Let's use it as an example
public interface Fatherhomeland { public static int postcode=410000; public final String address ="Changsha City, Hunan Province"; }
public interface Fatherschool { public static String Schoolname="Central South University"; public static String Englishname="Center South University"; public static String Adderss="Yuelu District, Changsha City"; //Defining the method abstract in the interface means abstract public abstract void show1(int a); }
Let's refer to the interface in the current class
public class Sonstyle extends Fatherstyle implements Fatherhomeland,Fatherschool{ //When overriding methods in an interface, you need to remove abstract public void show1(int a) { System.out.println("School Name:"+Sonstyle.Schoolname); System.out.println("English name:"+Sonstyle.Englishname); System.out.println("School address:"+Sonstyle.Adderss); } public static void main(String[] args) { Sonstyle ui=new Sonstyle(); ui.name="Zhang San"; ui.age=14; ui.number=12367; ui.show(); //From this statement, we can see that the class and property name can be directly used in the interface to refer to the properties in the interface System.out.println("Postal Code:"+Sonstyle.postcode); System.out.println("Address:"+Sonstyle.address); ui.show1(3); } }
The final output in the Console is
Name: Zhang San age: 14 student ID: 12367
Postal Code: 410000
Address: Changsha, Hunan Province
School Name: Central South University
English Name: Center South University
School address: Yuelu District, Changsha City
For interfaces
We can know that its attribute is a specific value, and the method in the interface is an abstract method, which needs to be rewritten in the class in the interface, so it has a close relationship with the listener we will talk about next. We can see that when we click the mouse or press the keyboard, there will be different reactions. We can't write a listener for each reaction In fact, the listener we are going to talk about next is the interface. By rewriting the methods in the listener, different reactions are formed. Let's take a look at the listener
Event listener in java
Generally speaking, the listener is a response mechanism, that is, when we have any operation, the listener will accept some information, and then pass it to another class to execute specific methods. In java, listeners are set for each type of event. The common ones are mouse listener, action listener and KeyListener
The implementation steps of event listening are as follows:
Step 1: determine the event source.
Step 2: determine the event type
Step 3: create a class to implement the corresponding type interface.
Step 4: create the listener object and add the event listener to the event source component
For the listener: we can make a login interface similar to QQ. When we input the account and password, we can judge whether the account and password are correct through the listener, and then another interface will pop up.
Write in front
In the program code of the login interface, we also need package knowledge, because if we want to write a login interface, we need some classes in the Java API document, such as JFrame, JButton, JTextField When we want to use these classes, we need to use import at the beginning of the class to declare the classes we need to use. The two most commonly used packages are in the Java API document javax.swing Package and java.awt Package, these interface components we need are generally in this, and all components must be put into the top-level container component (JFrame). Next, we Look at the procedure:
import javax.swing.JPasswordField;//This is the password box that allows you to enter single line and multi word text import java.awt.Color;//This is a class for setting colors import java.awt.Dimension; import java.awt.FlowLayout;//Layout method class, the default layout in the top-level content is absolute layout import javax.swing.JFrame; //Top level container component class import javax.swing.JLabel; //Label class import javax.swing.JTextField; //Input box to allow entry of single line text import javax.swing.ImageIcon; //An implementation of Icon interface, which draws Icon according to Image. Use to add pictures import javax.swing.JButton; //Button public class loginbegain { //How to display the interface public void showUI() { //Create form object JFrame loginFrame = new JFrame(); //Set the properties of the form //Set the size of the form loginFrame.setSize(600, 450); //Set form center loginFrame.setLocationRelativeTo(null); //Set the layout of the form //Flowlayout is a flow layout class. FlowLayout layout = new FlowLayout(); loginFrame.setLayout(layout); //Create the size of the image object label Dimension Size=new Dimension(600,270); //Create a picture object ImageIcon icon = new ImageIcon("C:\\Users\\zr The beauty of honor\\Pictures\\92.jpg"); //When adding picture objects, pay attention to changing the file format to the form of file name.jpg JLabel iconLabel = new JLabel(icon); iconLabel.setPreferredSize(Size); //Add to form loginFrame.add(iconLabel); //Create account label JLabel Login = new JLabel(" account number"); Login.setForeground(Color.BLUE); loginFrame.add(Login); //Create input box object JTextField nameIn = new JTextField(); //Set the size of the input box Dimension inputSize = new Dimension(400, 30); nameIn.setPreferredSize(inputSize); //Add to form loginFrame.add(nameIn); //Create a label for the registered account JLabel registerLabel = new JLabel("Join us "); registerLabel.setForeground(Color.BLUE); //Add to form loginFrame.add(registerLabel); //Create password label JLabel mm=new JLabel("password"); mm.setForeground(Color.BLUE); loginFrame.add(mm); //Create password box JPasswordField mmk=new JPasswordField(); mmk.setPreferredSize(inputSize); loginFrame.add(mmk); JLabel ccgb=new JLabel("Forget password"); ccgb.setForeground(Color.BLUE); loginFrame.add(ccgb); //Set the size of the button box Dimension andx=new Dimension(100,30); //Create a login button JButton an=new JButton("Sign in"); an.setForeground(Color.BLACK); an.setPreferredSize(andx); loginFrame.add(an); //Set form visible loginFrame.setVisible(true); loginFrame.setVisible(true); response1 judge=new response1(); judge.k1=nameIn; nameIn.addActionListener(judge); judge.k2=mmk; mmk.addActionListener(judge); an.addActionListener(judge); } //Main method public static void main(String[] fdsfds) { //Create interface objects loginbegain ui = new loginbegain(); //Call the method of display interface ui.showUI(); } }
Let's look at the listeners used in this:
The listener is as follows in the code:
response1 judge=new response1(); //The text box object is equal to the interface text box object in order to get the text judge.k1=nameIn; //Text box add action listener nameIn.addActionListener(judge); judge.k2=mmk; //Add action listener to password box object mmk.addActionListener(judge); //Button box add action listener an.addActionListener(judge);
//In this code area, we write the specific response of the listener import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.JPasswordField; import javax.swing.JTextField; public class response1 implements ActionListener { JTextField k1; JPasswordField k2; //Input box String account; //Password box String code; //This is to rewrite all methods of the action listener (Interface) public void actionPerformed(ActionEvent e) { //Get the text of account input box account=k1.getText(); //Use System to test whether this usage is correct or not. There are many methods in java. Sometimes we don't know which one to use, so we can use System to test System.out.println(account); //Get the text of the password box code=k2.getText(); System.out.println(code); //Judge with if statement if("22416".contentEquals(account)) { System.out.println("The user name is correct"); if("123456".contentEquals(code)) { System.out.println("Login succeeded!"); newJFrame pi=new newJFrame(); pi.show(); } else { System.out.println("Wrong password! Please re-enter!"); } } else { System.out.println("The user name is incorrect, please re-enter"); } } }
Supplement the code of newJFrame class in the third program slice
import javax.swing.JFrame; public class newJFrame { public void show() { //Create a new form object JFrame Topface= new JFrame(); //Set the size of the top-level container Topface.setSize(200,200); //Set center display Topface.setLocationRelativeTo(null); //When setup closes, the form closes Topface.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); try { Thread.sleep(1000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } //Set form visible Topface.setVisible(true); } }
Now we can see the effect of running the program. Open the program, enter the defined account and password
As shown in the figure:
In the new interface, we can play our imagination and create more things
Let's understand the monitor
In the login interface, you can also see that only the action listener is used in the code, which roughly means that when we press the login button, java will obtain the account information and password information, and judge whether they are correct, so as to achieve the purpose of login. The listener is equivalent to an action responder, that is, when we have an operation, it is caught by the listener, and then the listener will perform further operations (personal understanding).
Last words:
What's wrong in the blog? If you have time, you can point it out for the editor. I'll revise it according to the problem. Many of the above opinions about the monitor and interface are personal understandings. As the saying goes, "a thousand Hamlets out of a thousand readers" hopes that these things can help you. It's not easy to write. If you have time, leave your precious comments and praise. Your praise is my biggest motivation!