NetBeans--打地鼠

本文介绍了一个基于Java Swing的简单打地鼠游戏实现。游戏使用了定时器来控制地鼠的出现,并通过按钮监听来增加分数。文章展示了如何创建游戏界面、设置按钮样式以及实现游戏逻辑。

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

xia

 

 

下面为 GameJFrame.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg20221204knockgame;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.Timer;

/**
 *
 * @author mikha
 */
public class GameJFrame extends javax.swing.JFrame {

    private ArrayList<JButton>buttons;//所有的按钮
    private Random rd;//地鼠出现的位置
    private int currentIndex;//当前地鼠的位置
    private int score=0;//打地鼠的分数
    private Timer timer;//计时器
    
    /**
     * Creates new form GameJFrame
     */
    public GameJFrame() {
        initComponents();
        this.setSize(660,660);
        buttons=new ArrayList<>();
        rd=new Random();
        for(int i=0;i<9;i++)//添加九个按钮
        {
            JButton button=new JButton(new ImageIcon(".\\src\\img\\21.jpg"));
            button.setSize(220,220);
            button.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    if(ae.getSource()==buttons.get(currentIndex))
                    {
                        score=score+10;
                        GameJFrame.this.setTitle("当前得分:"+score);
                    }
                }
            });
            buttons.add(button);
            this.add(button);
        }
        currentIndex=rd.nextInt(9);
        buttons.get(currentIndex).setIcon(new ImageIcon(".\\src\\img\\1.jpg"));
        timer=new Timer(800, new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                buttons.get(currentIndex).setIcon(new ImageIcon(".\\src\\img\\21.jpg"));
                currentIndex=rd.nextInt(9);
                buttons.get(currentIndex).setIcon(new ImageIcon(".\\src\\img\\1.jpg"));
            }
        });
        timer.start();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new java.awt.GridLayout(3, 3));

        pack();
    }// </editor-fold>                        

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(GameJFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new GameJFrame().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    // End of variables declaration                   
}

 下面是Main.java

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package pkg20221204knockgame;

/**
 *
 * @author mikha
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        // TODO code application logic here
        GameJFrame gameJFrame=new GameJFrame();
        gameJFrame.setVisible(true);
    }
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值