JAVA拼图小游戏 (美化界面)

本文介绍了使用Java编写的拼图游戏,包括GameJFrame类的实现,关键接口如KeyListener和ActionListener的使用,以及游戏界面的初始化、图片处理和键盘事件响应。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

import javax.swing.*;
import javax.swing.border.BevelBorder;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GameJFrame extends JFrame implements KeyListener, ActionListener {
    //JFrame 界面,窗体
    //子类呢?也表示界面,窗体
    //规定:GameJFrame这个界面表示的就是游戏的主界面
    //以后跟游戏相关的所有逻辑都写在这个类中
    int date[][] = new int[4][4];
    int win[][] = new int[4][4];



    public GameJFrame(){
        //设置界面的宽高
        initJFrame();
        //设置菜单
        initJMenuBar();
        //初始化数据
        initdate();
        //设置图片
        initimage();

        // 让显示显示出来,建议写在最后
        this.setVisible(true);

    }
    int x=0;
    int y=0;
    int step = 0;
    private void initdate() {
        int[] arr ={0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};

        for(int i = 0;i<arr.length;i++){
            Random r = new Random();
            int num = r.nextInt(arr.length);
            int temp = arr[i];
            arr[i]= arr[num];
            arr[num]= temp;
        }
        int index = 0;
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){

                date[i][j]= arr[index];
                index++;
            }
        }
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                if(date[i][j]==0)
                {
                    x= i;
                    y = j;
                }
            }
        }

    }


    private void initimage() {

        this.getContentPane().removeAll();
        JLabel jstep = new JLabel("步数:"+step);
        jstep.setBounds(50,30,100,20);
        this.getContentPane().add(jstep);
        if(victory()){
            JLabel win = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\win.png"));
            win.setBounds(203,283,197,73);
            this.getContentPane().add(win);
        }
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                //创建一个图片ImageIcon的对象
                //ImageIcon icon = new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal2\\"+count+".jpg");
                //创建一个Jlabel的对象(管理容器)
                JLabel jLabel = new JLabel(new ImageIcon("image\\animal\\animal2\\"+date[i][j]+".jpg"));
                //指定图片的位置
                jLabel.setBounds(105*j+83,105*i+134,105,105);
                jLabel.setBorder(new BevelBorder(1));
                //把管理容器添加到界面中
                //this.add(jLabel);
                this.getContentPane().add(jLabel);

            }
        }
        JLabel backgroud = new JLabel(new ImageIcon("image\\background.png"));
        backgroud.setBounds(40,40,508,560);
        this.getContentPane().add(backgroud);
       this.getContentPane().repaint();
    }

    private boolean victory() {
        int count = 1;
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                win[i][j] = count;
                count++;
            }
        }
        for(int i = 0;i<4;i++){
            for(int j = 0;j<4;j++){
                if(date[i][j]!=win[i][j])
                {
                    return false;
                }
            }
        }
        return true;
    }
    JMenuItem repalyItem = new JMenuItem("重新开始");
    JMenuItem reloginItem = new JMenuItem("重新登录");
    JMenuItem closeItem = new JMenuItem("关闭游戏");

    JMenuItem accountItem = new JMenuItem("公共号");
    private void initJMenuBar() {
        //创建整个的菜单对象
        JMenuBar jMenuBar = new JMenuBar();
        //创建菜单上面的两个选项的对象(功能 关于我们)
        JMenu functionJMenu = new JMenu("功能");
        JMenu aboutJMenu = new JMenu("关于我们");
        //创建选项下面的条目
        repalyItem.addActionListener(this);
        reloginItem.addActionListener(this);
        closeItem.addActionListener(this);
        accountItem.addActionListener(this);

        //将每一个选项下面的条目集中到选项的中
        functionJMenu.add(repalyItem);
        functionJMenu.add(reloginItem);
        functionJMenu.add(closeItem);

        aboutJMenu.add(accountItem);
        //将选项添加到菜单上
        jMenuBar.add(functionJMenu);
        jMenuBar.add(aboutJMenu);
        //将菜单添加到界面上
        this.setJMenuBar(jMenuBar);
    }

    private void initJFrame() {
        this.setSize(603,680);
        //设置界面的标题
        this.setTitle("拼图单机版 v.10");
        //设置界面置顶
        this.setAlwaysOnTop(true);
        //设置界面居中
        this.setLocationRelativeTo(null);
        //设置默认的关闭模式
        this.setDefaultCloseOperation(3);//0 表示DO_NOTHING_ON_CLOSE 通俗讲:关不掉
        //1 表示HIDE_ON_CLOSE 通俗讲:界面关闭但虚拟机未关闭
        //2 表示DISPOSE_ON_CLOSE 通俗讲:当开启多个界面的时候,只有把所有界面都关闭虚拟机才会停止。注意:所有界面都要设置成这个样子。
        //3 表示EXIT_ON_CLOSE 通俗讲: 关掉一个界面,虚拟机就停止了。
        //取消居中放置,按照xy轴的形式添加组件
        this.setLayout(null);
        this.addKeyListener(this);
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        int keyCode = e.getKeyCode();
        if(keyCode==65){
            this.getContentPane().removeAll();
            JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\animal\\animal1\\all.jpg"));
            jLabel.setBounds(83,134,420,420);
            this.getContentPane().add(jLabel);
            this.getContentPane().repaint();
            JLabel backgroud = new JLabel(new ImageIcon("image\\background.png"));
            backgroud.setBounds(40,40,508,560);
            this.getContentPane().add(backgroud);
            this.getContentPane().repaint();
        }
    }

    @Override
    public void keyReleased(KeyEvent e) {
        if(victory()){
            return;
        }
        int keyCode = e.getKeyCode();
        if(keyCode==37){
            if(y==3){
                return;
            }

            date[x][y]=date[x][y+1];
            date[x][y+1]=0;
            y++;
            step++;
            initimage();

        }
        if(keyCode==39){
            if(y==0){
                return;
            }

            date[x][y]=date[x][y-1];
            date[x][y-1]=0;
            y--;
            step++;
            initimage();
        }
        if(keyCode==38){
            if(x==3){
                return;
            }

            date[x][y]=date[x+1][y];
            date[x+1][y]=0;
            x++;
            step++;
            initimage();
        }
        if(keyCode==40){
            if(x==0){
                return;
            }

            date[x][y]=date[x-1][y];
            date[x-1][y]=0;
            x--;
            step++;
            initimage();
        }
        if(keyCode==65){
            initimage();
        }
        if(keyCode==87){
            int count = 1;
             for(int i = 0;i<4;i++){
                 for(int j = 0;j<4;j++){
                     date[i][j] = count;
                        count++;
                 }
             }
             initimage();
        }
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source==repalyItem){
            step=0;
            initdate();
            initimage();

        }
        if(source==reloginItem){
            this.setVisible(false);
            new LoginJFrame();


        }
        if(source==closeItem)
        {
            System.exit(0);
        }
        if(source==accountItem){
            JDialog dialog = new JDialog();
            JLabel jLabel = new JLabel(new ImageIcon("D:\\JAVA\\pintu\\image\\about.png"));
            jLabel.setBounds(0,0,258,258);
            dialog.getContentPane().add(jLabel);
            dialog.setSize(344,344);
            dialog.setAlwaysOnTop(true);
            dialog.setLocationRelativeTo(null);
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值