Login interface by creating anonymous objects in java JFrame
package com.sxt;import java.awt.Container;import java.awt.GridLayout;import java.awt.event.ActionEvent;import java.awt.event.ActionListener;import javax.swing.JButton;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPasswordField;import javax.swing.JTextField;public class LoginFrame extends JFrame{JTextField txtname=new JTextField();JPasswordField txtpass=new JPasswordField();JButton bl=new JButton("Login");JButton bg=new JButton("Close");//Construct the parameterless constructor and place the main method in the constructor, and then adjust public LoginFrame(){setBounds(25,25,250,250);Container c = getContentPane();c.setLayout(new GridLayout(4,2,10,10));c.add(new JLabel("username"));c.add(txtname);c.add(new JLabel("password"));c.add(txtpass);c.add(bl);c.add(bg);setDefaultCloseOperation(EXIT_ON_CLOSE);setVisible(true);//Note: Here is the anonymous internal class bg.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubSystem.exit(0);}});//Note: Here is the anonymous inner class bl.addActionListener(new ActionListener(){public void actionPerformed(ActionEvent e) {// TODO Auto-generated method stubString name = txtname.getText();String pass = txtpass.getText();if(name.equals("tom")&&pass.equals("123")){System.out.println("Login successfully");} else{System.out.println("Login failed");}}});}public static void main(String[] args) {new LoginFrame();}}Results show:
Summarize
The above is the complete code example of this article about Java's interface to make a login system through JFrame. I hope it will be helpful to everyone. Interested friends can continue to refer to other related topics on this site. If there are any shortcomings, please leave a message to point it out. Thank you friends for your support for this site!