| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 503 人关注过本帖
标题:我写的一个打地鼠的游戏,貌似有点问题
只看楼主 加入收藏
enix12enix
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2007-6-19
收藏
 问题点数:0 回复次数:0 
我写的一个打地鼠的游戏,貌似有点问题

以下是我写的一个关于打地鼠的游戏程序,为什么运行起来有点问题,谁能帮我看一下啊
package com;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class MousePlay implements ActionListener
{
private JFrame frame;
private Container contentPane;

private JButton btn[][]=new JButton[3][3];
private JLabel timeLbl,countLbl;

private Timer btnTimer,timer;
private JButton startBtn,exitBtn;

private int x,y;

public MousePlay()
{
frame=new JFrame("游戏");
frame.setBounds(200,200,500,500);
frame.setResizable(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
contentPane=frame.getContentPane();
btnTimer=new Timer(500,this);
timer=new Timer(1000,this);
initGUI();
}

public void initGUI()
{
contentPane.setLayout(new BorderLayout());
timeLbl=new JLabel("0");
countLbl=new JLabel("0");

JPanel p1=new JPanel(new GridLayout(1,2));
p1.add(timeLbl);
p1.add(countLbl);

JPanel p2=new JPanel(new GridLayout(3,3));
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
btn[i][j]=new JButton(" ");
p2.add(btn[i][j]);
btn[i][j].setEnabled(false);
btn[i][j].addActionListener(this);
}
}
JPanel p3=new JPanel(new FlowLayout());
startBtn=new JButton("开始");
exitBtn=new JButton("退出");
p3.add(startBtn);
p3.add(exitBtn);

contentPane.add(p1,BorderLayout.NORTH);
contentPane.add(p2,BorderLayout.CENTER);
contentPane.add(p3,BorderLayout.SOUTH);

startBtn.addActionListener(this);
exitBtn.addActionListener(this);
}

public void actionPerformed(ActionEvent e)
{
if(e.getSource()==timer)
{
int v=timeLbl.getText().trim();
v--;
timeLbl.setText(v+"");
if(v==0)
{
timer.stop();
btnTimer.stop();
JOptionPane.showMessageDialog(null,new String("您的分数为"+countLbl.getText()),"恭喜!",JOptionPane.INFORMATION_MESSAGE,new ImageIcon("logo.gif"));

timeLbl.setText("0");
countLbl.setText("0");

for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
{
btn[i][j].setEnabled(false);
btn[i][j].setText("");
}
startBtn.setEnabled(true);

}
}

if(e.getSource()==btnTimer)
{
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
btn[i][j].setText("");
x=(int)(Math.random()*3);
y=(int)(Math.random()*3);
btn[x][y].setText("Mouse!");
}
if(e.getSource()==startBtn)
{
timeLbl.setText(20+"");
timer.start();
btnTimer.start();
for(int i=0;i<3;i++)
for(int j=0;j<3;j++)
btn[i][j].setEnabled(true);
startBtn.setEnabled(false);
}
if(e.getSource()==exitBtn)
{
System.exit(0);
}
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
if(e.getSource()==btn[i][j])
{
if(i==x&&j==y)
{
int cent=Integer.parseInt(countLbl.getText().trim());
cent++;
countLbl.setText(cent+"");
}
}
}
}

}

public void go()
{
frame.setVisible(true);
}

public static void main(String args[])
{
(new MousePlay()).go();
}

搜索更多相关主题的帖子: 地鼠 游戏 
2007-06-21 22:02
快速回复:我写的一个打地鼠的游戏,貌似有点问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.011381 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved