在学习了javaGUI图形化界面后,做了一个小型的打地鼠游戏,比较简单。对于自己知识的总结。由于没有学习线程,所以这个游戏是由两个定时器控制。后期有待改进。
涉及知识点:JFrame、JPanel、JLabel、JButton、JComboBox、ImageIcon(设置图片)、Timer
PlayMouse.java
package com.briup.day13;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
public class PlayMouse implements ActionListener {
private JFrame frame; // 容器窗口
private JPanel north, center; // 两个panel容器,放置位置北边和中间
private JComboBox<String> box; // 放置难易程度,下拉框
private JLabel timeLabel; // 时间标签
private JLabel timeValueLabel; // 时间值
private JLabel gradeLabel; // 得分标签
private JLabel gradeValueLabel; // 得分值
private JButton startButton; // 开始按钮
private JButton[] btns; // 设置九个按钮
private ImageIcon img; // 设置图片
private Timer timer; // 存放原来的时间
private Timer mouseTime; // 存放老鼠出现的时间
private int level; // 难度的等级
private int old_index; // 显示九个按钮的下标位置
private String gameTime; // 显示游戏时间
private boolean isNew = false;
// 窗口初始化
public PlayMouse() {
frame = new JFrame("打地鼠");
frame.setBounds(300, 300, 600, 600);
frame.setResizable(false); //窗口不可拉伸
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
initComponent();
frame.setVisible(true);
}
// 添加组件
public void initComponent() {
// 初始化其他组件
north = new JPanel();
center = new JPanel();
box = new JComboBox<String>();
box.addItem("初级");
box.addItem("中极");
box.addItem("高级");
gameTime = "20";
timeLabel = new JLabel("time: ");
timeValueLabel = new JLabel(gameTime);
gradeLabel = new JLabel("grade: ");
gradeValueLabel = new JLabel("0");
startButton = new JButton("Start");
btns = new JButton[9];
for (int i = 0; i < btns.length; i++) {
btns[i] = new JButton();
}
// 设置按钮的禁用状态
setEnabledForBtns(false);
img = new ImageIcon("C:\\Users\\85415\\Desktop\\mouse.png");
// 设置布局管理器,并添加其他组件
frame.setLayout(new BorderLayout());
frame.add(north, BorderLayout.NORTH);
frame.add(center);
north.setLayout(new FlowLayout());
north.add(box);
north.add(timeLabel);
north.add(timeValueLabel);
north.add(gradeLabel);
north.add(gradeValueLabel);
north.add(startButton);
// 设置中间九宫格布局
center.setLayout(new GridLayout(3, 3));
for (int i = 0; i < btns.length; i++) {
center.add(btns[i]);
}
// 添加监听器
for (int i = 0; i < btns.length; i++) {
btns[i].addActionListener(this);
}
box.addActionListener(this);
startButton.addActionListener(this);
// 定时器,监听
timer = new Timer(1000, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
String text = timeValueLabel.getText();
int t = Integer.parseInt(text);
timeValueLabel.setText(--t + "");
if (t < 0) {
timeValueLabel.setText(++t + "");
timer.stop();
startButton.setEnabled(true);
mouseTime.stop();
box.setEnabled(true);
setEnabledForBtns(false);
btns[old_index].setIcon(null); //将老鼠出现的按钮清除图片
}
}
});
}
// 重写监听动作的方法
@Override
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
// 判断是否点击start按钮
if (source == startButton) {
startButton.setEnabled(false);
setEnabledForBtns(true);
timer.start();
timeValueLabel.setText(gameTime);
box.setEnabled(false);
gradeValueLabel.setText("0");
// 不同的难度,老鼠出现时间快慢不同
int boxIndex = box.getSelectedIndex();
if (boxIndex == 0) {
level = 1000;
} else if (boxIndex == 1) {
level = 700;
} else if (boxIndex == 2) {
level = 200;
}
mouseTime = new Timer(level, new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
btns[old_index].setIcon(null);
int index = (int) (Math.random()*9);
btns[index].setIcon(img); //老鼠随机出现在按钮上
old_index = index;
}
});
mouseTime.start();
} else if (source == box) {
} else {
if (isNew == false) {
String text = gradeValueLabel.getText();
int cent = Integer.parseInt(text);
JButton btn = (JButton) source;
if (btn.getIcon() != null) {
gradeValueLabel.setText(++cent + "");
btn.setIcon(null);
isNew = true;
}
}
if (isNew == true) {
String text = gradeValueLabel.getText();
int cent = Integer.parseInt(text);
JButton btn = (JButton) source;
if (btn.getIcon() != null) {
gradeValueLabel.setText(++cent + "");
btn.setIcon(null);
isNew = false;
}
}
}
}
// 设置按钮的禁用
public void setEnabledForBtns(boolean flag) {
for (int i = 0; i < btns.length; i++) {
btns[i].setEnabled(flag);
}
}
public static void main(String[] args) {
new PlayMouse();
}
}
运行结果:

mouse.png
