java练习--拼图游戏之登录

 

点击重新登录菜单,进入登录页面

1. 首先要隐藏游戏的主界面

2. 展示登录界面

3. 当按下登录按钮时,登录按钮图片颜色变深。

4. 松开登录按钮时,获取用户名、密码和验证码的输入信息,进行检验,正确就跳转到游戏主界面

注意:验证码输入框后面的文字每点击一次就换一次内容

import javax.swing.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Random;

public class LoginFrame extends JFrame implements MouseListener {
    private JLabel randomStringLabel = new JLabel();
    private JLabel loginLabel, registerLabel;
    private JTextField usernameTextField, passwordTextField, verificationTextField;
    private String verificationString;

    public LoginFrame(int width, int height, String title){

        // 设置窗体样式
        this.initFrame(width, height, title);

        // 加载页面内容
        initImage();

        // 让页面显示
        this.setVisible(true);
    }
    // 窗体显示设置
    private void initFrame(int width, int height, String title){
        // 设置尺寸
        this.setSize(width, height);
        // 设置窗体的标题
        this.setTitle(title);
        // 设置屏幕在正中心
        this.setLocationRelativeTo(null);
        // 设置页面关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        // 取消默认的居中放置,只有取消了才会按照xy轴指定的位置放置
        this.setLayout(null);
    }

    private void initImage(){
        JLabel username = new JLabel(new ImageIcon("puzzlegame/image/login/用户名.png"));
        username.setBounds(110, 150, 47, 17);
        this.getContentPane().add(username);

        JLabel password = new JLabel(new ImageIcon("puzzlegame/image/login/密码.png"));
        password.setBounds(110, 200, 32, 16);
        this.getContentPane().add(password);

        JLabel verification = new JLabel(new ImageIcon("puzzlegame/image/login/验证码.png"));
        verification.setBounds(110, 250, 56, 21);
        this.getContentPane().add(verification);

        // 输入框
        usernameTextField = new JTextField();
        usernameTextField.setBounds(170, 150, 200, 30);
        this.getContentPane().add(usernameTextField);

        passwordTextField = new JPasswordField();
        passwordTextField.setBounds(170, 200, 200, 30);
        this.getContentPane().add(passwordTextField);

        verificationTextField = new JTextField();
        verificationTextField.setBounds(170, 250, 100, 30);
        this.getContentPane().add(verificationTextField);

        // 按钮
        loginLabel = new JLabel(new ImageIcon("puzzlegame/image/login/登录按钮.png"));
        loginLabel.setBounds(110, 300, 128, 47);
        loginLabel.addMouseListener(this);
        this.getContentPane().add(loginLabel);
        registerLabel = new JLabel(new ImageIcon("puzzlegame/image/login/注册按钮.png"));
        registerLabel.setBounds(250, 300, 128, 47);
        registerLabel.addMouseListener(this);
        this.getContentPane().add(registerLabel);

        // 验证码
        generateVerification();


        addBackgroundImage();
    }

    // 加载验证码
    private void generateVerification(){
        // 验证码
        this.verificationString = getRandomString();
        randomStringLabel.setText(this.verificationString);
        randomStringLabel.setBounds(300, 250, 100, 30);
        // 为验证码添加点击监听事件
        randomStringLabel.addMouseListener(this);
        this.getContentPane().add(randomStringLabel);
    }
    
    // 生成随机字符串
    private String getRandomString(){
        String randomString = "";
        String[] strings = new String[]{"a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", 
                "q", "r", "s", "t", "u", "v", "w", "x", "y", "z", "0", "1", "2", "3", "4", "5", "6", "7", "8", "9"};
        for (int i = 0; i < 4; i++) {
            Random random = new Random();
            randomString += strings[random.nextInt(strings.length)];
        }
        return randomString;
    }

    // 添加背景图片
    private void addBackgroundImage(){
        ImageIcon imageIcon = new ImageIcon("puzzlegame/image/login/background.png");
        JLabel jLabel = new JLabel(imageIcon);
        jLabel.setBounds(15, 20, 470, 390);
        this.getContentPane().add(jLabel);
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object source = e.getSource();
        if (source == loginLabel){
            loginLabel.setIcon(new ImageIcon("puzzlegame/image/login/登录按下.png"));
        }else if(source == registerLabel){
            registerLabel.setIcon(new ImageIcon("puzzlegame/image/login/注册按下.png"));
        }
    }

    @Override
    public void mouseReleased(MouseEvent e) {
        Object source = e.getSource();
        if (source == randomStringLabel) {
            String randomString = getRandomString();
            // 替换验证码内容
            randomStringLabel.setText(randomString);
        }else if(source == loginLabel){
            loginLabel.setIcon(new ImageIcon("puzzlegame/image/login/登录按钮.png"));
            // 获取用户名、密码、验证码
            String username = usernameTextField.getText();
            String password = passwordTextField.getText();
            String verification = verificationTextField.getText();
            if (verification.equals(this.verificationString)){
                // 隐藏本页面
                this.removeNotify();
                new GameFrame(580, 680, "拼图单机版 v1.0");
            }
        }else if(source == registerLabel){
            registerLabel.setIcon(new ImageIcon("puzzlegame/image/login/注册按钮.png"));
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

 

  • 2
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值