拼图小游戏

1.游戏主界面(图片素材可自己找)

包名报错,可按照自己的命名修改

package com.zyf.ui;
import zyf.test.time;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.FloatControl;
import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.*;
import java.io.File;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JLabel;
public class GameJFrame extends JFrame implements KeyListener,ActionListener, MouseListener
{
    int code;//键盘监听
    static String load="A7";//音乐路径
    static Clip clip;
    int data[][]=new int[4][4];//创建一个二维数组
    //获取空白方块的坐标
    int x = 0;
    int y = 0;
    String path = "image\\animal\\animal3\\"; //定义一个变量,记录当前展示图片的路径
    //定义一个二维数组
    int win [][]= {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
    int step = 0; //定义变量用来统计步数
    int a=201-step;// 剩余步数
    //窗口菜单
    JMenuItem replayItem = new JMenuItem("重新游戏");
    JMenuItem reLoginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");
    JMenuItem girl = new JMenuItem("美女");
    JMenuItem animal = new JMenuItem("动物");
    JMenuItem sport = new JMenuItem("运动");
    //公众号条目
    JMenuItem accountItem = new JMenuItem("公众号");
    //提示条目
    JMenuItem allphotos = new JMenuItem("A键提示");
    JMenuItem gamewin = new JMenuItem("作弊模式(W)");
    //计时器难度条目
    JMenuItem Time = new JMenuItem("开启计时器");
    // 音乐条目
    JMenuItem startMusic = new JMenuItem("播放音乐");
    JMenuItem stopMusic = new JMenuItem("暂停播放");
    JMenuItem changeMusic = new JMenuItem("更换音乐");
   //增加难度条目
    JMenuItem countdown=new JMenuItem("限定步数");
    boolean start_step = false;
    JLabel tie=new JLabel("剩余步数:"+a);//创建一个标签
    public GameJFrame()
    {
        initJFrame(); //初始化界面
        initJMenuBar(); //初始化菜单
        initData(); //打乱二维数组中的数据
        initImage();//根据打乱之后的结果去加载图片
        this.setVisible(true);//让界面显示出来
    }
    //音乐播放
    public  static void playMusic()
    {
        try
        {
            File musicPath = new File(load+".wav");//Java.to包
            if (musicPath.exists())//判断文件是否存在
            {
                /**
                 * AudioInputStream读取声音档案
                 */
                AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
                clip = AudioSystem.getClip();
                clip.open(audioInput);
                //控制音量大小
                FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
                gainControl.setValue(0.60f);//设置音量范围
                clip.start();
                clip.loop(Clip.LOOP_CONTINUOUSLY);//循环播放
            }
            else
            {
                System.out.println("音乐不存在");
            }
        }
        catch (Exception ex)
        {
            System.out.println("异常");
        }
    }
    //打乱数据
    private void initData()
    {
        int tempArr[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
        //打乱数组中的数据的顺序
        Random r = new Random();//生成随机数
        for (int i = 0; i < tempArr.length; i++)
        {
            int x = r.nextInt(tempArr.length);//随机获取数组下标
            int temp = tempArr[i];
            tempArr[i] = tempArr[x];
            tempArr[x] = temp;
        }
        //给二维数组添加数据
        for (int i = 0; i < tempArr.length; i++)
        {
            if (tempArr[i] == 0)//获取空白图片的坐标
            {
                x = i / 4;
                y = i % 4;
            }
            data[i / 4][i % 4] = tempArr[i];
        }
    }
    //添加图片
    private void initImage()
    {
        this.getContentPane().removeAll();//清空原本已经出现的所有图片
        if(a==0&&!victory())
        {
            JLabel overJlabel=new JLabel(new ImageIcon("image\\c.png"));
            overJlabel.setBounds(115,200,400,200);//设置图片的大小以及位置
            this.getContentPane().add(overJlabel);//用getContentPane()方法获得JFrame的内容面板,再对其加入组件
        }
        if (victory())//显示胜利的图标
        {
            JLabel winJLabel = new JLabel(new ImageIcon("image\\win.png"));
            winJLabel.setBounds(203, 283, 197, 73);
            this.getContentPane().add(winJLabel);
        }
        //增加难度
        JLabel tim=new JLabel("总步数:"+201);
        tim.setBounds(420,20,100,20);
        this.getContentPane().add(tim);
        tie.setBounds(420,40,100,20);
        this.getContentPane().add(tie);
        //步数
        JLabel stepCount = new JLabel("步数:" + step);
        stepCount.setBounds(50, 30, 100, 20);
        this.getContentPane().add(stepCount);
        //根据二维数组添加图片
        for (int i = 0; i < 4; i++)//四行,每行四张图片
        {
            for (int j = 0; j < 4; j++)
            {
                int num = data[i][j];//获取当前要加载图片的序号
                /* ImageIcon icon=new ImageIcon();//创建一个图片ImageIcon对象
                   JLabel  jLabel=new JLabel("image\\login\\用户名.png"(icon);//创建一个JLabel对象
                   this.add(jLabel);//将管理容器添加到界面中*/
                JLabel jLabel = new JLabel(new ImageIcon(path + num + ".jpg")); //创建一个JLabel的对象(管理容器)
                jLabel.setBounds(105 * j + 83, 105 * i + 134, 105, 105);//指定图片位置
                jLabel.setBorder(new BevelBorder(BevelBorder.LOWERED));//给图片添加边框凹斜切边界
                this.getContentPane().add(jLabel); //把管理容器添加到界面中
            }
        }
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image\\background.png"));
        background.setBounds(40, 40, 508, 560);
        this.getContentPane().add(background);//把背景图片添加到界面当中
        this.getContentPane().repaint();//重画图形,刷新界面
    }
    private void initJMenuBar()
    {
        JMenuBar jMenuBar = new JMenuBar();//创建菜单栏
        JMenu functionJMenu = new JMenu("功能");
        JMenu music = new JMenu("音乐");
        JMenu key = new JMenu("提示");
        JMenu difficulty= new JMenu("增加难度");
        JMenu aboutJMenu = new JMenu("关于我们");
        JMenu diffculty = new JMenu("计时功能");
        JMenu change = new JMenu("更换图片");
        //将每一个选项下面的条目添加到对应的选项中
        //功能
        functionJMenu.add(change);// 更换图片
        functionJMenu.add(replayItem);//重新游戏
        functionJMenu.add(reLoginItem);//重新登录
        functionJMenu.add(closeItem);//关闭游戏
        //功能下更换图片下的条目
        change.add(girl);
        change.add(animal);
        change.add(sport);
        //计时器下的条目
        diffculty.add(Time);//限定步数
        //提示
        key.add(allphotos);//展示完整照片
        key.add(gamewin);//快速通关
        //关于我们
        aboutJMenu.add(accountItem);//公众号
        //音乐
        music.add(startMusic);//开始播放
        music.add(stopMusic);//暂停
        music.add(changeMusic);//更换音乐
        //增加难度
        difficulty.add(countdown);//限定步数
        //给条目绑定事件
        replayItem.addActionListener(this);//动作监听事件
        reLoginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);
        allphotos.addActionListener(this);
        gamewin.addActionListener(this);
        Time.addActionListener(this);
        girl.addActionListener(this);
        sport.addActionListener(this);
        animal.addActionListener(this);
        startMusic.addActionListener(this);
        stopMusic.addActionListener(this);
        changeMusic.addActionListener(this);
        countdown.addActionListener(this);
        countdown.addMouseListener(this);
        //将菜单里面的选项添加到菜单当中
        jMenuBar.add(functionJMenu);//功能
        jMenuBar.add(diffculty);//计时器
        jMenuBar.add(music);//音乐
        jMenuBar.add(difficulty);//限定步数
        jMenuBar.add(key);// 快捷键提示
        jMenuBar.add(aboutJMenu);//关于我们
        //给整个界面设置菜单
        this.setJMenuBar(jMenuBar);
    }
    private void initJFrame()
    {
        this.setSize(603, 680);//设置界面的宽高
        this.setTitle("拼图游戏"); //设置界面的标题
        Container c = this.getContentPane();//获取内容窗格
        c.setBackground(Color.ORANGE);//将内容格窗口设置颜色
        Image iconImage = (new ImageIcon("image\\icon.png")).getImage();//创建图标
        this.setIconImage(iconImage);//让窗口显示图标
        this.setAlwaysOnTop(true); //设置界面置顶
        this.setLocationRelativeTo(null);//设置界面居中
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);//设置关闭模式
        this.setLayout(null);  //取消默认的居中放置,只有取消了才会按照XY轴的形式添加组件
        this.addKeyListener(this);//给整个界面添加键盘监听事件
    }
    @Override
    public void keyTyped(KeyEvent e)
    {

    }
    @Override//按下之后调用
    public void keyPressed(KeyEvent e)
    {
        int code = e.getKeyCode();
        //System.out.println(code);
        if (victory()||a==0)
        {
            return;
        }
        if (code == 65)
        {
            this.getContentPane().removeAll(); //把界面中所有的图片全部删除
            JLabel all = new JLabel(new ImageIcon(path + "all.jpg"));//按A键提示后加载一张完整的图片
            all.setBounds(83, 134, 420, 420);//设置图片的位置和大小
            this.getContentPane().add(all);
            JLabel background = new JLabel(new ImageIcon("image\\background.png"));//添加背景图片
            background.setBounds(40, 40, 508, 560);
            this.getContentPane().add(background);//把背景图片添加到界面当中
            this.getContentPane().repaint();//重画图形,刷新界面
        }
    }
    @Override //松开按键的时候会调用这个方法
    public void keyReleased(KeyEvent e)
    {
        if (victory()||a==0)
        {
            return;
        }
        code = e.getKeyCode();
        //System.out.println(code);
        //上下左右移动图片
        if (code == 37)
        {
            System.out.println("向左移动");
            if (y == 3)//空表方块在最右边,右边没有图片,不能移动
            {
                return;
            }
            data[x][y] = data[x][y + 1];
            data[x][y + 1] = 0;
            y++;
            step++;//每移动一次,计数器就自增一次。
            if (start_step)
            {
                a=201-step;
                tie.setText("剩余步数:"+a);
            }
            initImage();//调用方法,按照最新的数字加载图片
        }
        if (code == 38)
        {
            System.out.println("向上移动");
            if (x == 3)//空白方块在最下方,下面没有图片再能移动了
            {
                return;
            }
            data[x][y] = data[x + 1][y];
            data[x + 1][y] = 0; //x + 1, y 表示空白方块下方的数字
            x++;
            step++;//每移动一次,计数器就自增一次。
            a=100-step;
            if (start_step)
            {
                a=201-step;
                tie.setText("剩余步数:"+a);
            }
            
            initImage(); //调用方法按照最新的数字加载图片
        }
        if (code == 39)
        {
            System.out.println("向右移动");//空白方块在最左边,左边没有图片可以移动
            if (y == 0)
            {
                return;
            }
            data[x][y] = data[x][y - 1];
            data[x][y - 1] = 0;
            y--;
            step++; //每移动一次,计数器就自增一次。
            if (start_step)
            {
                a=201-step;
                tie.setText("剩余步数:"+a);
            }
            initImage(); //调用方法按照最新的数字加载图片
        }
        if (code == 40)
        {
            System.out.println("向下移动");
            if (x == 0)//空白方块在最上边,下面没有图片可以移动
            {
                return;
            }
            data[x][y] = data[x - 1][y];//把空白方块上方的数字往下移动
            data[x - 1][y] = 0;
            x--;
            step++;     //每移动一次,计数器就自增一次。
            if (start_step)
            {
                a=201-step;
                tie.setText("剩余步数:"+a);
            }
            initImage(); //调用方法按照最新的数字加载图片
        }

        if (code == 87)//一键通关
        {
            data = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};
            initImage();
        }
        if(code==32)
        {
            System.out.println("暂停音乐");
            clip.stop();
        }
        if (code == 65)
        {
            initImage();
        }
    }
    //判断data数组中的数据是否跟win数组中相同
    public boolean victory()
    {
        //i ,j: 依次表示二维数组 data里面的索引
        for (int i = 0; i < data.length; i++)
        {
            for (int j = 0; j < data[i].length; j++)
            {
                if (data[i][j] != win[i][j])
                {
                    return false;//只要有一个数据不一样,则返回false
                }
            }
        }
        return true; //如果数组中的每一个元素均相同,则游戏胜利
    }
    @Override
    public void actionPerformed(ActionEvent e)//动作事件,选择菜单选项
    {
        //获取当前被点击的条目对象
        Object obj = e.getSource();
        if (obj == replayItem)
        {
            System.out.println("重新游戏");
            step = 0; //计步器清零
            a=201-step;
            tie.setText("剩余步数:"+a);
            initData();//再次打乱二维数组中的数据
            initImage();//重新加载图片
        }

        if (obj == reLoginItem)
        {
            System.out.println("重新登录");
            int n = JOptionPane.showConfirmDialog(this, "请确认是否重新登录", "提示", JOptionPane.YES_OPTION);
            if (n == JOptionPane.YES_OPTION)//如果点击是
            {
                this.setVisible(false);//关闭当前的游戏界面
                new LoginJFramedd(); //打开登录界面
            }
        }
        if (obj == closeItem)
        {
            System.out.println("关闭游戏");
            // 添加确认对话框
            int n = JOptionPane.showConfirmDialog(this, "请确认是否退出游戏", "提示", JOptionPane.YES_OPTION);
            if (n == JOptionPane.YES_OPTION)//如果点击是
            {
                System.exit(0); //关闭游戏
            }
        }
        if (obj == accountItem)
        {
            System.out.println("公众号");
            JDialog jDialog = new JDialog();//创建一个弹框对象
            JLabel jLabel = new JLabel(new ImageIcon("image\\about.jpg"));  //把图片添加到弹框当中
            jLabel.setBounds(0, 0, 258, 258);  //设置图片的位置和宽高
            jDialog.getContentPane().add(jLabel);    //创建一个管理图片的容器对象JLabel
            jDialog.setSize(650, 650);  //给弹框设置大小
            jDialog.setAlwaysOnTop(true);  //让弹框置顶
            jDialog.setLocationRelativeTo(null);  //让弹框居中
            jDialog.setModal(true);  //模式对话框,弹窗不关闭无法进行其它操作
            jDialog.setVisible(true); //让弹框显示出来
        }
        if (obj == allphotos)
        {
            System.out.println("提示");
            JDialog jDialog = new JDialog();//创建一个弹框对象
            JLabel jLabel = new JLabel(new ImageIcon(path + "all.jpg"));//提示出完整图片,并将图片加载如弹框中
            jLabel.setBounds(0, 0, 300, 300);//设置弹框的位置和宽高
            jDialog.getContentPane().add(jLabel);//创建一个管理图片的容器对象JL abel
            jDialog.setSize(650, 650);
            jDialog.setAlwaysOnTop(true);  //让弹框置顶
            jDialog.setLocationRelativeTo(null);  //让弹框居中
            jDialog.setModal(true);  //弹框不关闭则无法操作下面的界面
            jDialog.setVisible(true); //让弹框显示出来
        }
        if (obj == gamewin)//一键通关
        {
            int n = JOptionPane.showConfirmDialog(this, "请确认是否启动作弊功能", "提示", JOptionPane.YES_OPTION);
            if (n == JOptionPane.YES_OPTION)//如果点击是
            {
                data = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}, {13, 14, 15, 0}};//将二维数组正确排列
                initImage();
            }
        }
        if (obj == Time)//开启计时功能
        {
            if (victory())
            {
                return;
            }
            System.out.println("开始计时");
            new time();
        }
        //切换图片的功能
        if (obj == girl)//更换girl照片
        {
            Random sc = new Random();
            int nummer = sc.nextInt(13) + 1;
            path = "image\\girl\\girl" + nummer + "\\";
            System.out.println(path);
            System.out.println(" 更换图片");
            a=200;//步数恢复
            step = 0; //计步器清零
            initData();//再次打乱二维数组中的数据
            initImage();//重新加载图片
        }
        if (obj == sport)//随机更换一张运动的照片
        {
            Random sc = new Random();
            int nummer = sc.nextInt(10) + 1;
            path = "image\\sport\\sport" + nummer + "\\";
            System.out.println(path);
            System.out.println(" 更换图片");
            a=200;//步数恢复
            step = 0; //计步器清零
            initData();//再次打乱二维数组中的数据
            initImage();//重新加载图片
        }
        if (obj == animal)
        {
            Random sc = new Random();
            int nummer = sc.nextInt(8) + 1;
            path = "image\\animal\\animal" + nummer + "\\";
            System.out.println(path);
            System.out.println(" 更换图片");
            a=200;//步数恢复
            step = 0; //计步器清零
            initData();//再次打乱二维数组中的数据
            initImage();//重新加载图片
        }
        if (obj == startMusic)
        {
            System.out.println("播放音乐");
            this.playMusic();
            clip.start();
        }
        if (obj == stopMusic)
        {
            try
            {
                System.out.println("暂停音乐");
                clip.stop();
            }
            catch(NullPointerException p)
            {
                System.out.println("异常");
            }
        }
        if (obj == changeMusic)
        {
            clip.stop();
            System.out.println("更换音乐");
            Random sc = new Random();
            int r = sc.nextInt(7) + 1;
            load = "A" + r;
            System.out.println(load);
            playMusic();
            clip.start();
        }
    }
    @Override
    public void mouseClicked(MouseEvent e)
    {
        //System.out.println("点击了4");
    }

    @Override
    public void mousePressed(MouseEvent e)//点击鼠标按钮时发生
    {
        //System.out.println("点击了3");
        if (!start_step)// 点击限定步数,游戏重开
        {
            step = 0; //计步器清零
            a=200;//步数恢复
            initData();//再次打乱二维数组中的数据
            initImage();//重新加载图片
            start_step = true;
            countdown.setText("不限定步数");
        }
        else
        {
            start_step = false;
            countdown.setText("限定步数");
        }

    }
    @Override
    public void mouseReleased(MouseEvent e)
    {
       // System.out.println("点击了2");
    }

    @Override
    public void mouseEntered(MouseEvent e)
    {
       // System.out.println("点击了1");
    }

    @Override
    public void mouseExited(MouseEvent e)
    {
       // System.out.println("点击了");
    }
}

登录界面

package com.zyf.ui;
import com.zyf.domain.User;
import com.zyf.util.CodeUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
public class LoginJFramedd extends JFrame implements MouseListener
{
    static ArrayList<User> allUsers = new ArrayList<>();//创建一个集合,存储用户名和密码
    static
    {
        allUsers.add(new User("zyf021026","123456"));
       
    }
    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 LoginJFramedd()
    {
        initJFrame(); //初始化界面
        initView(); //在这个界面中添加内容
        this.setVisible(true);//让当前界面显示出来
    }
    public void initView()
    {
        JLabel usernameText = new JLabel(new ImageIcon("image\\login\\用户名.png"));//添加用户名文字的图片
        usernameText.setBounds(116, 135, 47, 17);
        this.getContentPane().add(usernameText);
        username.setBounds(195, 134, 200, 30);//设置用户名文本框的大小和宽高
        this.getContentPane().add(username);//用getContentPane()方法获得JFrame的内容面板,再对其加入组件
        JLabel passwordText = new JLabel(new ImageIcon("image\\login\\密码.png")); //添加密码文字的图片
        passwordText.setBounds(130, 195, 32, 16);
        this.getContentPane().add(passwordText);
        password.setBounds(195, 195, 200, 30);//设置密码输入框的位置和宽高
        this.getContentPane().add(password);
        //验证码提示
        JLabel codeText = new JLabel(new ImageIcon("image\\login\\验证码.png"));//添加验证码文字的图片
        codeText.setBounds(133, 256, 50, 30);
        this.getContentPane().add(codeText);
        //验证码的输入框
        code.setBounds(195, 256, 100, 30);//设置验证码输入框的位置和宽高
        this.getContentPane().add(code);
        String codeStr = CodeUtil.getCode();//获取生成的验证码
        //System.out.println("验证码"+codeStr);
        rightCode.setText(codeStr);//设置内容,标签上添加验证码
        rightCode.addMouseListener(this); //绑定鼠标事件
        rightCode.setBounds(300, 256, 50, 30);//位置和宽高
        this.getContentPane().add(rightCode); //将随机生成的验证码添加到界面
        login.setIcon(new ImageIcon("image\\login\\登录按钮.png"));
        login.setBounds(123, 310, 128, 47);//添加登录按钮
        login.setBorderPainted(false); //去除按钮的边框
        login.setContentAreaFilled(false);//设置按钮透明
        login.addMouseListener(this);//给登录按钮绑定鼠标事件
        this.getContentPane().add(login);
        register.setBounds(256, 310, 128, 47); //添加注册按钮
        register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));
        register.setBorderPainted(false);//去除按钮的边框
        register.setContentAreaFilled(false);//设置按钮透明
        register.addMouseListener(this);//给注册按钮绑定鼠标事件
        this.getContentPane().add(register);
        //添加背景图片
        JLabel background = new JLabel(new ImageIcon("image\\login\\background.png"));
        background.setBounds(0, 0, 470, 390);
        this.getContentPane().add(background);
    }
    public void initJFrame()
    {
        this.setSize(488, 430);//设置宽高
        this.setTitle("拼图游戏 登录");//设置标题
        Image iconImage=(new ImageIcon("image\\icon.png")).getImage();//创建图标
        this.setIconImage(iconImage);//设置窗口的显示图标
        this.setDefaultCloseOperation(EXIT_ON_CLOSE);//设置关闭模式(结束程序运行)
        this.setLocationRelativeTo(null);//设置界面居中
        this.setAlwaysOnTop(true);//界面置顶
        this.setLayout(null);//取消内部默认布局
    }
    @Override
    public void mouseClicked(MouseEvent e) //单击鼠标按钮发生
    {
        if (e.getSource() == login)
        {
            System.out.println("点击了登录按钮");
            //获取两个文本输入框中的内容
            String usernameInput = username.getText();
            String passwordInput = password.getText();
            String codeInput = code.getText();//获取用户输入的验证码
            //创建一个User对象
            User userInfo = new User(usernameInput, passwordInput);//用户名,密码
            System.out.println("用户输入的用户名为" + usernameInput);
            System.out.println("用户输入的密码为" + passwordInput);
            if (codeInput.length() == 0)
            {
                System.out.println("验证码为空");
                showJDialog("验证码不能为空");//提示
            }
            if (usernameInput.length() == 0 || passwordInput.length() == 0)
            {
                System.out.println("用户名或者密码为空");  //检验用户名和密码是否为空
                showJDialog("用户名或者密码为空");//调用showJDialog方法并展示弹框
            }
            if (!codeInput.equalsIgnoreCase(rightCode.getText()))
            {
                System.out.println("验证码错误");
                showJDialog("验证码输入错误");
                System.out.println("随机更换一个新的验证码");
                String code = CodeUtil.getCode(); //获取一个新的验证码
                rightCode.setText(code);//将获取的新的验证码重新添加到界面中

            }
            if  (contains(userInfo)&&codeInput.equalsIgnoreCase(rightCode.getText()))
            {
                System.out.println("输入正确");
                this.setVisible(false);//关闭当前登录界面
                new GameJFrame();   //打开游戏的主界面,把当前登录的用户名传递给游戏界面
            }
            if(!contains(userInfo))
            {
                System.out.println("用户名或密码错误");
                showJDialog("用户名或密码错误");
                System.out.println("更换验证码");
                String code = CodeUtil.getCode();  //获取一个新的验证码
                rightCode.setText(code);
            }
        }
        if (e.getSource() == register)
        {
            System.out.println("点击了注册按钮");
            new RegisterJFrame();
        }
        if (e.getSource() == rightCode)
        {
            System.out.println("更换验证码");
            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); //让弹框展示出来
    }
    //按下鼠标时
    @Override
    public void mousePressed(MouseEvent e)
    {
        if (e.getSource() == login)
        {
            login.setIcon(new ImageIcon("image\\login\\登录按下.png"));
        }
        if (e.getSource() == register)
        {
            register.setIcon(new ImageIcon("image\\login\\注册按下.png"));
        }
    }
    //按下不松
    @Override
    public void mouseReleased(MouseEvent e)
    {
        if (e.getSource() == login)
        {
            login.setIcon(new ImageIcon("image\\login\\登录按钮.png"));
        }
        if (e.getSource() == register)
        {
            register.setIcon(new ImageIcon("image\\login\\注册按钮.png"));
        }
    }
    @Override
    public void mouseEntered(MouseEvent e)
    {

    }
    @Override
    public void mouseExited(MouseEvent e)
    {

    }
    //判断用户在集合中是否存在
    public boolean contains(User userInput)//userInput为用户输入的账号,密码
    {
        for (int i = 0; i < allUsers.size(); i++)//size(),返回列表中元素的个数
        {
            User rightUser = allUsers.get(i);//get(),返回列表中指定的元素
            if(userInput.getUsername().equals(rightUser.getUsername()) && userInput.getPassword().equals(rightUser.getPassword()))//比较用户名和密码是否相同
            {
                return true; //有相同的代表存在,返回true
            }
        }
        return false;//循环结束之后还没有找到就表示不存在
    }
}

注册界面因能力有限,写着玩的

package com.zyf.ui;
import javax.swing.*;
import java.awt.*;
//注册按钮
public class RegisterJFrame extends JFrame//继承框架类
{
    //注册
    public RegisterJFrame()
    {
        this.setTitle("拼图 注册");//设置界面的标题
        Image iconImage=(new ImageIcon("image\\icon.png")).getImage();//创建图标
        this.setIconImage(iconImage);//让窗口显示图标
        JLabel jLab=new JLabel("请看此处",JLabel.CENTER);//创建一个标签对象,居中对齐
        this.setLayout(null);//取消布局管理器
        this.setSize(488,500);//设置标签的宽高
        Container c=this.getContentPane();//获取内容窗格
        c.setBackground(Color.BLACK);//将内容格窗口设置为黑色
        jLab.setOpaque(true);//将标签改为不透明
        jLab.setBackground(Color.YELLOW);//标签背景
        jLab.setForeground(Color.CYAN);//字体颜色
        jLab.setLocation(160,180);
        jLab.setSize(130,60);
        jLab.setToolTipText("技术有限,此功能无法实现");//为标签设置提示信息
        Font fnt=new Font("宋体",Font.BOLD+Font.ITALIC,20);//粗体加斜体
        jLab.setFont(fnt);
        this.add(jLab);//将标签添加的窗口中
        this.setAlwaysOnTop(true);//设置界面置顶
        this.setLocationRelativeTo(null); //设置界面居中
        this.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); //设置关闭模式(隐藏窗口,释放窗口占用的资源)
        this.setVisible(true); //让显示显示出来
    }
}

到一些主要的功能了

验证码功能的实现(验证码随机生成)

package com.zyf.util;
import java.util.ArrayList;
import java.util.Random;
//  验证码
public class CodeUtil
{
    public static String getCode()
    {
        ArrayList<Character> list = new ArrayList<>();//字符列表,长度为52
        for (int i = 0; i < 26; i++)  //添加字母 a - z  A - Z
        {
            list.add((char)('a' + i));//a - z
            list.add((char)('A' + i));//A - Z
        }
        String result="";
        Random r = new Random();
        //生成4个随机字母
        for (int i = 0; i < 4; i++)
        {
            int x = r.nextInt(list.size());//获取随机索引(0-51)
            char c = list.get(x);//返回列表中指定位置的元素
            //System.out.println(x);
            result = result + c;
            //System.out.println(result);
        }
        int number = r.nextInt(10); //生成0-9的随机数
        //验证码
        result = result + number;
        //随机打乱数字和字母的位置
        char chars[]=result.toCharArray(); //把字符串变成字符数组
        int index = r.nextInt(chars.length);  //在字符数组中生成一个随机索引
        char temp = chars[4];
        chars[4] = chars[index];
        chars[index] = temp;
        String code = new String(chars);//把字符数组再变回字符串,随机生成四个英文字母加一个数字的验证码
       // System.out.println(code);
        return code;
    }
}

javaBean类传递用户名和密码:

package com.zyf.domain;
//用户
public class User
{
    private String username;//用户名
    private String password;//密码
    public User()//定义一个无参构造方法
    {

    }
    public User(String username, String password)//定义一个带全部参数的构造方法
    {
        this.username = username;
        this.password = password;
    }
    public void setUsername(String username)
    {
        this.username = username;
    }
    public String getUsername()
    {
        return username;
    }
    public void setPassword(String password)
    {
        this.password = password;
    }
    public String getPassword()
    {
        return password;
    }
}

计时器功能

package zyf.test;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
//计时器功能
public class time extends JFrame
{
    public static boolean end_time =false;
    public static JTextField look =new JTextField(8);//创建文本框,显示8列
    /*public static void main(String[] args)
    {
        new time();
    }*/
    public time()
    {
        this.setTitle("计时器");//设置界面标题
        this.setLayout(null);//取消布局管理器
        this.setBounds(400,500,400,200);
        this.setVisible(true);//让界面显示出来
        this. setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);//设置关闭模式,隐藏窗口,释放窗口所占用的其它资源
        Font font=new Font("宋体",Font.PLAIN,40);//显示的字体,普通样式
        //显示
        this.add(look);
        look.setFont(font);//时间
        {
            look.setBounds(20,50,170,50);
        }
        JButton start =new JButton("开始");//设置开始的命令按钮
        start.setBorderPainted(false);//关闭按钮上的边框
        start.setFont(font);//设置开始的字样
        {
            start.setBounds(250,55,120,40);
        }
        this.getContentPane().add(start);//将开始添加到按钮中
        CaoZuo caoZuo=new CaoZuo();
        caoZuo.setJTextFiled(look);
        caoZuo.setJButton(start);
        start.addActionListener(caoZuo);//给按钮添加事件
    }
}
class DonTai implements Runnable//由Runnable接口实现DonTai类
{
    public void run()//实现Run方法
    {
      int h = 0, m = 0, s = 0;//时,分,秒
      for (h = 0; h < 12; h++)
       {
          for (m = 0; m < 60; m++)
          {
           for (s = 0; s < 60; s++)
            {
              try
               {
                 if (time.end_time == true)//点击开始
                  {
                   Thread.sleep(1000);//线程暂停一秒,相当于时间加一秒
                   time.look.setText(h + ":" + m + ":" + s);
                  }
                   else
                   {
                     return;
                   }
               }
               catch (InterruptedException e)
               {
                 System.out.println("异常");
               }
            }
          }
       }
    }
}
class CaoZuo implements ActionListener
{
    JTextField look;//文本框
    JButton queen;//启动按钮
    public void setJButton(JButton start)
    {
        this.queen = start;
    }
    @Override//重写实现动作监听的方法
    public void actionPerformed(ActionEvent e)
    {
        if (time.end_time == true)
        {
            time.end_time = false;
            queen.setText("开始");
        }
        else
        {
            time.end_time = true;
            queen.setText("停止");
        }
        DonTai d = new DonTai();
        Thread t = new Thread(d);//产生Thread类的对象t
        t.start();//开始计时,开始线程t
    }
    public void setJTextFiled(JTextField look)
    {
        this.look = look;
    }
}

最后是小游戏的主入口

import com.zyf.ui.LoginJFramedd;
//程序主入口
public class App
{
    public static void main(String[] args)
    {
        new LoginJFramedd();//表示程序的启动入口
    }
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值