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);
}
}