实现用户注册登陆界面

package 用户登陆注册;

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class MyFrame extends JFrame implements ActionListener {
public final static HashMap<String,String> map = new HashMap();
private JPanel jp;
private JButton jb1,jb2;
public MyFrame(){

    jp = new JPanel();
    jb1 = new JButton("login");
    jb2 = new JButton("register");
    jp.add(jb1);
    jp.add(jb2);
    this.add(jp);
    this.setBounds(500,100,157,100);
    this.setDefaultCloseOperation(3);
    this.setVisible(true);
    jp.setLayout(null);

// jb1.setBounds(500,100,100,60);
// jb2.setBounds(200,100,100,60);
jb1.addActionListener(this);
jb2.addActionListener(this);

}
public static void main(String[] args){
    MyFrame mf = new MyFrame();

}

@Override
public void actionPerformed(ActionEvent e) {
    String str = e.getActionCommand();
    if(str.equals("login")){
        LoginFrame lf = new LoginFrame();
    }else{
        RegisterFrame rf = new RegisterFrame();
    }

}

}
package 用户登陆注册;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class LoginFrame extends JFrame implements ActionListener {
private JLabel jl1,jl2;
private JTextField jtf;
private JPasswordField jpf;
private JButton jb;
private JPanel jp1;
public LoginFrame(){
this.setBounds(200,200,157,200);
// this.setDefaultCloseOperation(3);
jl1 = new JLabel(“username”);
jtf = new JTextField(10);
jl2 = new JLabel(“password”);

    jpf = new JPasswordField(10);
    jb = new JButton("login");
    jp1 = new JPanel();
    this.add(jp1);
    jp1.add(jl1);
    jp1.add(jtf);

    jp1.add(jl2);
    jp1.add(jpf);
    jp1.add(jb);

    this.setVisible(true);
    jb.addActionListener(this);
    jp1.setBackground(Color.blue);

}


@Override
public void actionPerformed(ActionEvent e) {
    String username = jtf.getText();
    String password = jpf.getText();
    HashMap hm = MyFrame.map;
    if(hm.containsKey(username)){
        if(password.equals(hm.get(username))){
            JOptionPane.showMessageDialog(null,"login successfully!");
            this.setVisible(false);
        }
        else{
            JOptionPane.showMessageDialog(null,"password erro!");
        }

    }
    else{
        JOptionPane.showMessageDialog(null,"username erro!");
    }


}

}
package 用户登陆注册;

import javax.swing.;
import java.awt.
;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.HashMap;

public class RegisterFrame extends JFrame implements ActionListener {
private JLabel jl1, jl2, jl3;
private JTextField jtf;
private JPasswordField jpf, jpf2;
private JButton jb;
private JPanel jp1;

public RegisterFrame() {
    this.setBounds(500, 200, 157, 200);

// this.setDefaultCloseOperation(3);
jl1 = new JLabel(“username”);
jtf = new JTextField(10);
jl2 = new JLabel(“password”);
jl3 = new JLabel(“confirm”);
jpf = new JPasswordField(10);

    jpf2 = new JPasswordField(10);
    jb = new JButton("register");
    jp1 = new JPanel();
    this.add(jp1);
    jp1.add(jl1);
    jp1.add(jtf);

    jp1.add(jl2);
    jp1.add(jpf);
    jp1.add(jl3);
    jp1.add(jpf2);
    jp1.add(jb);
    jb.addActionListener(this);

    this.setVisible(true);
    jp1.setBackground(Color.green);
}

@Override
public void actionPerformed(ActionEvent e) {
    String username = jtf.getText();
    String password  = jpf.getText();
    String password2 = jpf2.getText();
    if(password.equals(password2)){
        HashMap hm = MyFrame.map;
        if(hm.containsKey(username)){
            JOptionPane.showMessageDialog(null,"has registered");
        }else{
            hm.put(username,password);
            JOptionPane.showMessageDialog(null,"register successfully!");
            this.setVisible(false);
        }

    }else{
        JOptionPane.showMessageDialog(null,"different password");

    }

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值