Authentication login for interface and event graphical interface

Source: Internet
Author: User
Tags define abstract

Introduction to the concept:

1. Definition of the interface
Defines the interface keyword: interface
Format:
Public interface Interface name extends interface,... {


Define constants (constant names must all uppercase)
public static final data type constant name = value;

Defining abstract methods
Public abstract return value type method name (data type parameter name,...);
}
Attention:
1. The interface has only one access modifier public.
2. The keywords provided by the interface are: public, static, final, abstract
3. An interface cannot instantiate an object. (The biggest difference between the interface and the class)

2. Class implementation Interface (class inherits interface)
The inheritance of a class is a single inheritance, and with an interface, you can allow a class to inherit multiple interfaces (rich inheritance of classes).
Implementing the Interface keyword: implements
To implement the format of an interface:
public class class name extends class name implements interface name,... {

All the abstract methods in the interface must be implemented (the methods in the interface's parent interface must also be implemented)

}
Note: Subclasses must implement all the abstract methods in the interface.

2. Event mechanism (emphasis)
1. Event Source Object
1. What can be an event source object?
All components (container components and element components) can be event source objects.
2. How do you determine who is the event source object on the interface?
Which component you are acting on, then this component is your event source object. An action has only one event source. Example interface authentication login, other password input and so on is not the event source, the event source is the login button.

2. Event Monitoring method
addActionListener (ActionListener L);
Action monitoring method, this method is mainly used to monitor whether a mouse click action occurs on a similar button event source object, or a keyboard carriage return action occurs on a similar input box event source object, and if the action is monitored, the parameter
ActionListener an object of the event-handling class, the object automatically invokes the event-handling method.

Addmouselistener (MouseListener L);
Mouse monitoring method, the method is mainly used to listen to the event source object on the mouse down, release, click, enter and leave the action, if you listen to the action, will be handed to the parameter MouseListener event processing class
Object, the object automatically invokes the event-handling method.

Addmousemotionlistener (Mousemotionlistener L);
Mouse movement monitoring method, this method is mainly used to listen to the event source object on whether there is mouse movement or drag action, if you listen to the action, will be handed to the parameter Mousemotionlistener event processing class
The object will automatically invoke the event-handling method as it is handled.

Addkeylistener (KeyListener L);
Keyboard monitoring method, this method is mainly used to listen to the event source object on whether there is a keyboard button press, release and percussion action, if you listen to the action, will be handed to the parameter KeyListener event-handling class
The object will automatically invoke the event-handling method as it is handled.

3. Event interface (event handler Class)
ActionListener Action Event Interface
MouseListener Mouse Event interface
Mousemotionlistener Mouse Move Event interface
KeyListener Keyboard Event interface

Can an interface instantiate an object? No
So to define an event-handling class to implement an event interface

4. Event execution Process

Target: Click the login button on the login screen to display a new interface.
1. Event Source object: Login button
2. Event Monitoring method: addActionListener (ActionListener L);
3. Event interface (Event handling Class): ActionListener

Development steps:
1. Define the Loginlistener event handler class, which implements the ActionListener action event interface to implement an abstract method in the interface.
2. In the event-handling method (the abstract method of implementation), a new interface is displayed
3. In the interface class, instantiate the object of the Loginlistener event processing class LL.
4. Add an action listener method to the event source Butlogin, specifying the object of the event handling class LL.

Once the login is successful, the login screen will not need to be displayed again.
The event handler class does not have a login interface form object, which you need to define methods to borrow form objects from the login interface class.

5 Implementation of graphical interface

Graphical interface classes provided by 1.Java
1.AWT (Basic)
The components of AWT are drawn by the operating system's drawing mechanism.
The components of AWT are under the java.awt package.

2.SWING (Extended)
The components of swing are under the Javax.swing package.
Swing's components are new components that are refactored on the basis of AWT components.

2. Common component Classes
1. Container Component class: The container component class that can add container components or element components.
JFrame Form container Component class top-level container

2. Element Component class: The component that is commonly used to display text, pictures, and accept input is the element component class.
JLabel Tag element component class used to display picture, problem component class
The JTextField text input box component class is used to receive input information and is displayed directly
The JPasswordField Password input box component class is used to receive input information, but instead of displaying a symbol
Jcheckbox check box element Component class has a selection box and can also display text or pictures
The JButton button element component class is used to display pictures or text, and you can click

3. Helper class: A class that helps a component complete a function.
The FlowLayout streaming layout class is similar to Word files.
Dimension class for package width and height
ImageIcon icon class loading a picture file into the program

The class provided in 1-2 above is the class of the javax.swing package. You must add the package name when you write.
FlowLayout and dimension are under the java.awt package.
ImageIcon is under the javax.swing package.

3. Implement a login interface
1. Create a new Login.java class, and then define the main function and the method of initializing the interface in the class.
2. In the main function, instantiate the object of the login class and invoke the method of initializing the interface.
3. Instantiate a jframe top-level Container form object, set the property value of the form object: Size, title, display position, layout, close, visible
4. Create an object of the component class and add the component to the form.

First, consider the concept of an interface:

Package xxj.interface0528;

Defines a shape graphics interface that can inherit multiple parent interfaces
Public interface Shape extends Java.awt.event.ActionListener {

Defining constants
public static final String NAME = "Brother Bear";

Define abstract methods (cannot have method body)
public abstract void Draw ();

}

Package xxj.interface0528;

Import java.awt.event.ActionEvent;

Defines a Shapeimpl class that implements a shape interface, and Shapeimp can also inherit a class to implement one or more interfaces
public class Shapeimpl extends Object implements Shape {

public static final String NAME1 = "Brother Bear";//Although there is no "NAME1" attribute in Shapeimpl, but his interface Shap has this property, it can be called
All the abstract methods in the interface must be implemented (the methods in the interface's parent interface must also be implemented)
Implement the draw abstraction method in the Shape interface
public void Draw () {
System.out.println (name+ "is drawing graphics!) ");
}
Implement the Actionperformed abstract method in the ActionListener interface
public void actionperformed (ActionEvent e) {

}

public static void Main (string[] args) {
Shapeimpl shape = new Shapeimpl ();//interface cannot be instantiated, but class can
Shape.draw ();
}

}

Actual operation:

Practice
1. login function to implement login interface
2. Extension: According to the input account and password to verify, if the input account and password is correct, display the drawing interface, if the input account and password error, display the message prompt box interface.

public class Jiemian {
1. Entry main function of the program

public static void Main (string[] args) {
2. In the method of the main function, instantiate the object of the Jiemian class and invoke the method of initializing the interface.
Jiemian j = new Jiemian ();
J.tuxiang ();
}

1. How to set the login interface
public void Tuxiang () {
3. Instantiate a jframe top-level container form object,
JFrame JF = new Javax.swing.JFrame ();
3. Set the property values of the form object: Size, title, display position, layout, close, visible
Jf.setsize (300, 400);
Jf.settitle ("interface");
Jf.setlocationrelativeto (null);//sets the display position of the form, and Null indicates that the form is displayed in the center of the screen
Jf.setdefaultcloseoperation (2);//Set closing form when 2 closes the form exit program
Instantiate an object of a streaming layout class, the layout class is for the container, and you want to add multiple components on the container, you must set the alignment
FlowLayout f1 = new Java.awt.FlowLayout ();
Jf.setlayout (F1);//sets the form to be laid out as a streaming layout.
4. Define a ImageIcon class that is used to read a picture file on a disk.
ImageIcon image = new Javax.swing.ImageIcon ("D:\\eclipse\\1.png");//Create an object of the JLabel class to display the loaded picture
JLabel labelimage = new Javax.swing.JLabel (image);
Jf.add (labelimage);//Add labelimage to the form
Instantiate an object of a JTextField class
JTextField textname = new Javax.swing.JTextField ();
Instantiate an object of the dimension class that encapsulates the width and height of a component
Dimension Dim = New Java.awt.Dimension (200, 30);//Set the size of the Textname component
Textname.setpreferredsize (Dim);
Jf.add (textname);//Add textname to the form
Create an object of the JLabel class to show how to buy
JLabel lableshopping = new Javax.swing.JLabel ("How to buy");
Jf.add (lableshopping);//Add labelshopping to the form
JPasswordField nextname = new Javax.swing.JPasswordField ();
Dimension NEX = new Java.awt.Dimension (200, 30);
Nextname.setpreferredsize (NEX);
Jf.add (Nextname);

Javax.swing.JLabel Lablepassword = new Javax.swing.JLabel ("Enter password");
Jf.add (Lablepassword);

Javax.swing.JCheckBox CB1 = new Javax.swing.JCheckBox ("Remember Password");
Jf.add (CB1);
Javax.swing.JCheckBox CB2 = new Javax.swing.JCheckBox ("Forgot password");
Jf.add (CB2);

JButton Butdenglu = new Javax.swing.JButton ("login");
Dimension thi = new Java.awt.Dimension (150, 30);
Butdenglu.setpreferredsize (THI);
Jf.add (Butdenglu);
Jf.setvisible (TRUE);//Set form to visible
Jiemianlistener JP = new Jiemianlistener ();//instantiation of Jiemianlistener class
Butdenglu.addactionlistener (JP);//Add an action listener method to the event source Butdenglu, specifying the object of the event handling class JP
Jp.setjiemian (JF);//Lend the Form object in the Jiemian class to the JP object
Jp.setnextname (nextname);//Listening method captures the input of the password
Jp.settextname (textname);////Listening method capturing the input of a text box

}
}

Package wenya.interface527;

Import Java.awt.Frame;
Import java.awt.event.ActionEvent;
Import Java.awt.event.ActionListener;
Import Javax.swing.JFrame;
Import Javax.swing.JPasswordField;
Import Javax.swing.JTextField;

public class Jiemianlistener implements ActionListener {
ActionListener is the system's own interface
Public JFrame Jiemian;//public Object type object Name Declaration Login interface Form property value Object name
Method of setting the Login interface Form object (the method of borrowing JFrame form objects to its Jiemian class)
public void Setjiemian (JFrame j) {
Jiemian = j;
}

Input to the Jiemian class to borrow the password

Public JPasswordField Nextname;

public void Setnextname (JPasswordField nex) {
Nextname = NEX;
}

The input to the Jiemian class borrowing text box
Public JTextField Textname;

public void Settextname (JTextField Dim) {
Textname = Dim;
}
/**
* 1. Implement an abstract method (event handling method) in an interface
* @param e is a Parameter object of type ActionEvent, which stores the text information and event source objects displayed on the event source
*/

public void actionperformed (ActionEvent e) {

String str = new String (Nextname.getpassword ());//A new method for obtaining a password and text input, equivalent to "Nextname.gettext ()"

if (Str.equals ("1229") &&textname.gettext (). Equals ("Wenya")) {
JFrame frame = new JFrame ();
Frame.settitle ("drawing");
Frame.setsize (600, 500);
Frame.setdefaultcloseoperation (2);
Frame.setlocationrelativeto (NULL);
Frame.setvisible (TRUE);
Jiemian.dispose ();
} else {
JFrame frame = new JFrame ();
Frame.settitle ("hint box");
Frame.setsize (600, 500);
Frame.setdefaultcloseoperation (2);
Frame.setlocationrelativeto (NULL);
Frame.setvisible (TRUE);
Jiemian.dispose ();


}
}
}

Authentication login for interface and event graphical interface

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.