首先是GUI的知识清单:
源代码:
package jd1908_corejava.ch14.work; //打地鼠小游戏 import java.awt.BorderLayout; import java.awt.FlowLayout; import java.awt.GridLayout; import java.awt.Toolkit; 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 HitMouse extends JFrame implements ActionListener { // 容器模块 private JPanel north, center; // private JLabel timeLabel, timeValueLabel, centLabel, centValueLabel; // 下拉菜单 private JComboBox<String> box; private JButton startButton; private JButton[] buttons; // 图片 private ImageIcon icon; // 等级 private int level; // 是否可以加分的状态 private boolean flag; private int old_index; // 游戏计时器 private Timer timer; // 老鼠跳动计数器 private Timer mouseTimer; public HitMouse() { this.setTitle("打地鼠"); // this.setBounds(200, 200, 1000, 1300); this.setSize(500, 600); this.setLocation((int) (Toolkit.getDefaultToolkit().getScreenSize().getWidth() - this.getWidth()) / 2, (int) (Toolkit.getDefaultToolkit().getScreenSize().getHeight() - this.getHeight()) / 2); ; this.init(); this.setVisible(true); this.setDefaultCloseOperation(EXIT_ON_CLOSE); } private void init() { north = new JPanel(); center = new JPanel(); timeLabel = new JLabel("time"); timeValueLabel = new JLabel("10"); centLabel = new JLabel("cent"); centValueLabel = new JLabel("0"); startButton = new JButton("开始"); buttons = new JButton[9]; box = new JComboBox<String>(); box.addItem("初级"); box.addItem("中级"); box.addItem("高级"); icon = new ImageIcon("a.png"); // 默认布局 north.setLayout(new FlowLayout()); north.add(timeLabel); north.add(timeValueLabel); north.add(box); north.add(centLabel); north.add(centValueLabel); north.add(startButton); center.setLayout(new GridLayout(3, 3)); for (int i = 0; i < buttons.length; i++) { buttons[i] = new JButton(); // 锁定格子 buttons[i].setEnabled(false); center.add(buttons[i]); } this.add(north, BorderLayout.NORTH); this.add(center, BorderLayout.CENTER); startButton.addActionListener(this); box.addActionListener(this); //!!! for (int i = 0; i < buttons.length; i++) { buttons[i].addActionListener(this); } timer = new Timer(1000, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { String text = timeValueLabel.getText(); int time = Integer.parseInt(text); timeValueLabel.setText(--time + ""); if (time == 0) { startButton.setEnabled(true); buttons[old_index].setIcon(null); box.setEnabled(true); timer.stop(); mouseTimer.stop(); setAllButtonsEnable(false); } } }); level = 2000; mouseTimer = new Timer(level, new ActionListener() { @Override public void actionPerformed(ActionEvent e) { buttons[old_index].setIcon(null); int index = (int) (Math.random() * 9); // 随机让九宫格出现老鼠 buttons[index].setIcon(icon); old_index = index; flag = true; setAllButtonsEnable(true); } }); } protected void setAllButtonsEnable(boolean b) { for (int i = 0; i < buttons.length; i++) { buttons[i].setEnabled(b); } } public static void main(String[] args) { new HitMouse(); } @Override public void actionPerformed(ActionEvent e) { Object source = e.getSource(); if (source.equals(startButton)) { timer.start(); mouseTimer.start(); timeValueLabel.setText("10"); centValueLabel.setText("0"); // 锁定等级和开始 startButton.setEnabled(false); box.setEnabled(false); } else if (source.equals(box)) { int index = box.getSelectedIndex(); if (index == 1) { level = 500; } else if (index == 2) { level = 200; } else { level = 2000; } mouseTimer.setDelay(level); } else { JButton btn = (JButton) source; if (btn.getIcon() != null && flag) { String str = centValueLabel.getText(); int cent = Integer.parseInt(str); centValueLabel.setText(++cent + ""); flag = false; } } } }