某马java拼图游戏登录界面设计

 视频到游戏界面就没了,所以我自己在网上搜到了别人的代码,大概改了一下就拿来用了首先我用到的素材如下:

1.有了这个可以不再添加用户名、密码和验证码图片,直接此图片设置为背景图:

2.按钮深色图片:

因为上面有按钮亮色图片所以只需要把亮色部分调暗就行

3.代码部分:

里面都是大多都是copy,原文没有匹配账号那段代码,首先跟着课程敲了一个列表存储账号信息,但是列表匹配花了太多时间,使用了列表中contains函数始终显示false,后来发现contains函数匹配的是地址值,而不是字符串内容的值,通过借鉴另一篇博客,才知道要重写object类中的equals方法:

package com.xp.ui;

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

public class LoginJFrame extends JFrame implements MouseListener {
     ArrayList<User>  list = new ArrayList<>();
    {
        list.add(new User("zhangsan","123"));
        list.add(new User("lisi","1234"));
    }
    private JLabel randomStringLabel = new JLabel();
    private JLabel loginLabel, registerLabel;
    private JTextField usernameTextField, passwordTextField, verificationTextField;
    private String verificationString;

    public LoginJFrame() {
        //游戏界面设置
        InitLoginJFrame();
        //添加游戏图片
        InitImage();

        this.setVisible(true);
    }

    private void InitImage() {
        this.getContentPane().removeAll();
        // 输入框
        usernameTextField = new JTextField();
        usernameTextField.setBounds(155, 134, 200, 30);
        this.getContentPane().add(usernameTextField);

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

        verificationTextField = new JTextField();
        verificationTextField.setBounds(155, 245, 100, 30);
        this.getContentPane().add(verificationTextField);

        // 按钮
        loginLabel = new JLabel(new ImageIcon());
        loginLabel.setBounds(92, 295, 128, 47);
        loginLabel.addMouseListener(this);
        this.getContentPane().add(loginLabel);


        registerLabel = new JLabel(new ImageIcon());
        registerLabel.setBounds(235, 295, 128, 47);
        registerLabel.addMouseListener(this);
        this.getContentPane().add(registerLabel);
        generateVerification();
        addBackgroundImage();
        this.getContentPane().repaint();
    }
    // 加载验证码
    private void generateVerification(){
        // 验证码
        this.verificationString = getRandomString();
        randomStringLabel.setText(this.verificationString);
        randomStringLabel.setBounds(270, 245, 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("src/main/java/com/xp/ui/分割图片_(1)/login.png");
        JLabel jLabel = new JLabel(imageIcon);
        jLabel.setBounds(0,0,470,390);
        this.getContentPane().add(jLabel);
    }

    private void InitLoginJFrame() {
// 设置尺寸
        this.setSize(488, 430);
        // 设置窗体的标题
        this.setTitle("拼图游戏 v-1.0");
        // 设置屏幕在正中心
        this.setLocationRelativeTo(null);
        // 设置页面关闭模式
        this.setDefaultCloseOperation(3);
        // 取消默认的居中放置,只有取消了才会按照xy轴指定的位置放置
        this.setLayout(null);
        //一直置顶
        this.setAlwaysOnTop(true);
    }


    @Override
    public void mouseClicked(MouseEvent e) {
    }

    @Override
    public void mousePressed(MouseEvent e) {
        Object source = e.getSource();
        if (source == loginLabel){
            loginLabel.setIcon(new ImageIcon("src/main/java/com/xp/ui/分割图片_(1)/presslog.png"));
        }else if(source == registerLabel){
            registerLabel.setIcon(new ImageIcon("src/main/java/com/xp/ui/分割图片_(1)/register.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());
            // 获取用户名、密码、验证码
            String username = usernameTextField.getText();
            String password = passwordTextField.getText();
            String verification = verificationTextField.getText();
            if (verification.equals(this.verificationString)){
                User user = new User(username, password);
                if (list.contains(user)){
                    this.removeNotify();
                    new GameJFrame();
                }else {
                    System.out.println("用户名或者密码输入错误");
                    InitImage();
                }
            }else {
                System.out.println("验证码错误,请重新输入");
             InitImage();
            }
        }else if(source == registerLabel){
            registerLabel.setIcon(new ImageIcon());
        }
    }
    @Override
    public void mouseEntered(MouseEvent e) {
    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}

User类代码:

package com.xp.ui;
public class User {
    private String name;
    private String password;

    public User() {
    }

    public User(String name, String password) {
        this.name = name;
        this.password = password;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
//重写qual方法,使contains使用值判断一致,而不是使用地址方法
    @Override
    public boolean equals(Object o) {
        if (o instanceof User) {
            User user = (User) o;
            return this.name.equals(user.getName())
                    && this.password.equals(user.getPassword());

        }
        return super.equals(o);
    }

    @Override
    public String toString() {
        return "User [name=" + name + ", password="
                + password + "]";
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值