Interaction between button and form using java event monitoring mechanism

First, the event source object

All container components (such as forms) and element components (such as buttons) can become event sources, and where events occur, they are event sources.

Second, the event monitoring method

The event listener method can capture the action of the event source object, and these action information will become the parameter object of the event listener method.

For example, the action event listener method ActionListener can capture actions such as mouse clicks and carriage returns in input boxes.

Third, the event interface

The event interface is the interface that handles actions. After the parameter object takes the information given by the monitoring method, it will call the event processing method according to the action information, and then execute the code in the method to process the action.

For example, the actionPerformed(ActionEvent e) abstract method in ActionListener

Example: Click the login button on the login interface, verify the account and password entered by the user, if it is correct, display a new form and then close the login form; if it is wrong, display an incorrect form.


Write a login interface

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import java.awt.FlowLayout;

public class Login extends LoginListener{
public static void main(String args[]){
Login lg = new Login();
lg.showframe();
}
public void showframe(){
//创建窗体(顶级容器)
JFrame loginframe = new JFrame();
loginframe.setTitle("Login");
loginframe.setSize(300,400);
loginframe.setLocation(300,300);
loginframe.setDefaultCloseOperation(3);
//设置布局方式
FlowLayout f1 = new FlowLayout();//流式布局
loginframe.setLayout(f1);
//Add icon (\ is escape character, \\ is path separation)
ImageIcon image = new ImageIcon("C:\\Users\\lenovo\\Desktop\\background1.jpg");
JLabel label = new JLabel(image);
loginframe.add(label);
//Add username, password box
JLabel labelName = new JLabel("Account:");
loginframe.add(labelName);
JTextField textName = new JTextField(20 );
loginframe.add(textName); JLabel labelPassword = new JLabel("Password:"); loginframe.add(labelPassword); JPasswordField textPassword = new JPasswordField(20); loginframe.add(textPassword); //Add login button JButton button = new JButton("login"); loginframe.add(button);//Set the window to be visible










loginframe.setVisible(true);
                //Instantiate the monitor
LoginListener l1 = new LoginListener(); button.addActionListener
(l1); l1.setLogin(loginframe);//Pass the login form to the login property l1 of the l1 object. setName(textName);



l1.setPassword(textPassword);

        }

}

Write the listener class for the login interface

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextField;

public class LoginListener implements ActionListener{ private JFrame login;/ /Declare the properties of the login form private JTextField name;//Similarly private JTextField password; //Method for setting the property value of the login form (username, password) public void setLogin(JFrame login){ this.login = login; } public void setName(JTextField name){ this.name = name; } public void setPassword(JTextField password){ this.password = password; } public void actionPerformed(ActionEvent e) { JFrame frame = new JFrame();



















frame.setTitle("Drawing");
frame.setSize(700, 500);
frame.setDefaultCloseOperation(3);
//Make the form appear in the center
frame.setLocationRelativeTo(null);
//Get the content of the input box, getText () must be in the monitor to take effect
String s1=name.getText();
String s2=password.getText();
//Verify whether the username and password are correct
if(s1.equals("abc") && s2.equals( "abc")){
frame.setVisible(true);
}
else{
JFrame wrongframe = new JFrame();
wrongframe.setSize(200,100);
wrongframe.setDefaultCloseOperation(3);
wrongframe.setLocationRelativeTo(null);
JButton button = new JButton ("Username or password is wrong");
wrongframe.add(button);
wrongframe.setVisible(true);
} //Close the login form login.dispose();



}
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324771171&siteId=291194637