登陆窗口小项目

1、输入正确账号和密码,点击登陆,提示“用户登陆成功”

2、输入错误密码,提示“用户登陆失败”

 1 import java.awt.HeadlessException;
 2 import java.awt.event.ActionEvent;
 3 import java.awt.event.ActionListener;
 4 
 5 
 6 import javax.swing.JButton;
 7 import javax.swing.JFormattedTextField;
 8 import javax.swing.JFrame;
 9 import javax.swing.JLabel;
10 import javax.swing.JOptionPane;
11 import javax.swing.JPasswordField;
12 import javax.swing.JTextField;
13 
14 
15 public class MapTest extends JFrame implements ActionListener {
16     JLabel lb1,lb2;
17     JTextField jt;
18     JPasswordField jp;
19     JButton jb1,jb2;
20     String dl="123456";               //设定初始登录名和密码,此处将其写死了
21     String pass="qwert";
22     public MapTest(String title) throws HeadlessException{
23         super(title);
24         this.setSize(200,200);
25         this.setLocation(500, 200);
26         setLayout(new java.awt.FlowLayout());//设为流式布局
27         //添加组件
28         lb1=new JLabel("用户名:");
29         this.add(lb1);
30         jt=new JTextField(10);
31 //jt.addActionListener(this);
32         this.add(jt);
33         lb2=new JLabel("密码:");
34         this.add(lb2);
35         jp=new JPasswordField(10);
36 //jp.addActionListener(this);
37         this.add(jp);
38         jb1=new JButton("登录");
39         this.add(jb1);
40         jb1.addActionListener(this);
41         jb2=new JButton("取消");
42         jb2.addActionListener(this);
43         this.add(jb2);
44         this.setResizable(false);
45         this.setDefaultCloseOperation(EXIT_ON_CLOSE);
46         this.setVisible(true);
47 
48     }
49     @Override
50     public void actionPerformed(ActionEvent e){
51         // TODO Auto-generated method stub
52         if(e.getSource()==jb1) {
53             String str1=jt.getText();
54             String str2=String.valueOf(jp.getPassword());
55             if(str1.matches(dl) && str2.matches(pass)){
56                 JOptionPane.showMessageDialog(this, "用户登录成功");}
57             else{
58                 JOptionPane.showMessageDialog(this, "用户登录失败","消息提示",JOptionPane.WARNING_MESSAGE);
59                 jp.setText("");
60             }
61         }
62         if(e.getSource()==jb2){
63             dispose();
64         }
65     }
66     public static void main(String args[]){
67         new MapTest("登陆");
68     }
69 
70 }
原文地址:https://www.cnblogs.com/datacenter/p/11447681.html