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");
}
}
}