1.实现代码
import CodeUtil.CodeUtil;
import domain.User;import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;public class LoginGame extends JFrame implements MouseListener {static ArrayList<User> alluser=new ArrayList<>();static {alluser.add(new User("张三","aaa"));alluser.add(new User("李四","bbb"));}JButton login = new JButton();JButton register = new JButton();JTextField username = new JTextField();JPasswordField password = new JPasswordField();JTextField code = new JTextField();//正确的验证码JLabel rightCode = new JLabel();public LoginGame(){initJFrame();initView();this.setVisible(true);}public void initJFrame(){this.setSize(633,423);this.setTitle("斗地主V1.0登录");//设置左上角小标题this.setAlwaysOnTop(true);//设置一直置顶this.setLocationRelativeTo(null);//设置居中this.setDefaultCloseOperation(3);//设置点右上角关闭整个程序this.setVisible(true);//设置不隐藏界面}public void initView(){//1.添加用户文字Font usernameFont=new Font(null,1,16);JLabel inputtext=new JLabel("用户名");inputtext.setForeground(Color.white);inputtext.setFont(usernameFont);inputtext.setBounds(140, 55, 55, 22);this.getContentPane().add(inputtext);//2.添加用户名输入框username.setBounds(223, 46, 200, 30);this.getContentPane().add(username);//3.添加密码文字Font userPassword=new Font(null,1,16);JLabel intpupasd=new JLabel("密码");intpupasd.setForeground(Color.white);intpupasd.setFont(userPassword);intpupasd.setBounds(197, 95, 40, 22);this.getContentPane().add(intpupasd);//4.密码输入框password.setBounds(263, 87, 160, 30);this.getContentPane().add(password);//5.验证码提示Font yzmFont=new Font(null,1,16);JLabel inputYzm=new JLabel("验证码");inputYzm.setForeground(Color.white);inputYzm.setFont(yzmFont);inputYzm.setBounds(215, 142, 55, 22);this.getContentPane().add(inputYzm);//验证码输入框code.setBounds(291, 133, 100, 30);this.getContentPane().add(code);//获取正确的验证码String codeStr = CodeUtil.getCode();Font rightCodeFont = new Font(null,1,15);rightCode.setForeground(Color.red);//设置字体rightCode.setFont(rightCodeFont);//设置内容rightCode.setText(codeStr);//绑定鼠标事件rightCode.addMouseListener(this);//位置和宽高rightCode.setBounds(400, 133, 100, 30);//添加到界面this.getContentPane().add(rightCode);//6.添加登录按钮 "D:\项目IDEA\PINTU\FammerJoker\img\登录按钮.png"login.setBounds(123, 310, 128, 47);login.setIcon(new ImageIcon("FammerJoker\\img\\登录按钮.png"));//去除按钮的边框login.setBorderPainted(false);//去除按钮的背景login.setContentAreaFilled(false);//给登录按钮绑定鼠标事件login.addMouseListener(this);this.getContentPane().add(login);//7.添加注册按钮register.setBounds(256, 310, 128, 47);register.setIcon(new ImageIcon("FammerJoker\\img\\注册按钮.png"));register.setBorderPainted(false);register.setContentAreaFilled(false);register.addMouseListener(this);this.getContentPane().add(register);//8.添加背景图片 "D:\项目IDEA\PINTU\FammerJoker\img\background.png"JLabel background = new JLabel(new ImageIcon("FammerJoker\\img\\background.png"));background.setBounds(0, 0, 633, 423);this.getContentPane().add(background);}@Overridepublic void mouseClicked(MouseEvent e) {Object obj = e.getSource();if (obj == login) {//获取两个文本输入框中的内容String usernameInput = username.getText();String passwordInput = password.getText();//获取用户输入的验证码String codeInput = code.getText();//判断验证码是否为空if (codeInput.length() == 0) {showJDialog("验证码不能为空");return;}//判断用户名和密码是否为空if (usernameInput.length() == 0 || passwordInput.length() == 0) {showJDialog("用户名或者密码为空");return;}//判断验证码是否正确if (!codeInput.equalsIgnoreCase(rightCode.getText())) {showJDialog("验证码输入错误");return;}//判断集合中是否包含当前用户对象//其实就是验证用户名和密码是否相同//contains底层是依赖equals方法判断的,所以需要重写equals方法User userInfo = new User(usernameInput, passwordInput);if (alluser.contains(userInfo)) {//关闭当前登录界面this.setVisible(false);//打开游戏的主界面new GameJFrame();} else {showJDialog("用户名或密码错误");}} else if (obj == register) {System.out.println("点击了注册按钮");} else if (obj == rightCode) {//获取一个新的验证码String code = CodeUtil.getCode();rightCode.setText(code);}}//展示弹框public void showJDialog(String content) {//创建一个弹框对象JDialog jDialog = new JDialog();//给弹框设置大小jDialog.setSize(200, 150);//让弹框置顶jDialog.setAlwaysOnTop(true);//让弹框居中jDialog.setLocationRelativeTo(null);//弹框不关闭永远无法操作下面的界面jDialog.setModal(true);//创建Jlabel对象管理文字并添加到弹框当中JLabel warning = new JLabel(content);warning.setBounds(0, 0, 200, 150);jDialog.getContentPane().add(warning);//让弹框展示出来jDialog.setVisible(true);}//按下不松@Overridepublic void mousePressed(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("FammerJoker\\img\\登录按下.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("FammerJoker\\img\\注册按下.png"));}}//松开按钮@Overridepublic void mouseReleased(MouseEvent e) {if (e.getSource() == login) {login.setIcon(new ImageIcon("FammerJoker\\img\\登录按钮.png"));} else if (e.getSource() == register) {register.setIcon(new ImageIcon("FammerJoker\\img\\注册按钮.png"));}}@Overridepublic void mouseEntered(MouseEvent e) {}@Overridepublic void mouseExited(MouseEvent e) {}}
2.效果图展示