日期:2014-05-20  浏览次数:20915 次

期末到了,老师要求做项目,本人初学java,自写的代码运行不了,求高手们指点迷津啊
//灰常低级的打地鼠游戏
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.Button;
import java.lang.Thread;
//------------------------------------------------------------

class gide extends JFrame implements ActionListener{
boolean flag;
JMenuItem startitem,pauseitem,aboutitem,closeitem;
JMenu filemenu,systemmenu;
JMenuBar mbar;
JButton[] jb=new JButton[9];
JTextField jt1,jt2;

gide()
{
super("打地鼠o(∩_∩)o");
startitem=new JMenuItem("开始");
startitem.addActionListener(this);

pauseitem=new JMenuItem("暂停");
pauseitem.addActionListener(this);

aboutitem=new JMenuItem("关于");
aboutitem.addActionListener(this);

closeitem=new JMenuItem("关闭");
closeitem.addActionListener(this);

filemenu=new JMenu("文件");
systemmenu=new JMenu("系统");

filemenu.add(startitem);
filemenu.add(pauseitem);

systemmenu.add(aboutitem);
systemmenu.add(closeitem);

mbar=new JMenuBar();

mbar.add(filemenu);
mbar.add(systemmenu);

this.setJMenuBar(mbar);

jt1=new JTextField("0");
jt1.setBounds(0,0,60,40);

jt2=new JTextField("0");
jt2.setBounds(60,0,60,40);

jb[0]=new JButton("");
jb[0].setBounds(0,50,70,30);

jb[1]=new JButton("");
jb[1].setBounds(80,50,70,30);

jb[2]=new JButton("");
jb[2].setBounds(160,50,70,30);

jb[3]=new JButton("");
jb[3].setBounds(0,90,70,30);

jb[4]=new JButton("");
jb[4].setBounds(80,90,70,30);

jb[5]=new JButton("");
jb[5].setBounds(160,90,70,30);

jb[6]=new JButton("");
jb[6].setBounds(0,130,70,30);

jb[7]=new JButton("");
jb[7].setBounds(80,130,70,30);

jb[8]=new JButton("");
jb[8].setBounds(160,130,70,30);

Container wcontainer=this.getContentPane();

wcontainer.setLayout(null);

wcontainer.add(jb[1]);
wcontainer.add(jb[2]);
wcontainer.add(jb[3]);
wcontainer.add(jb[4]);
wcontainer.add(jb[5]);
wcontainer.add(jb[6]);
wcontainer.add(jb[7]);
wcontainer.add(jb[8]);
wcontainer.add(jb[0]);
wcontainer.add(jt1);
wcontainer.add(jt2);

this.setSize(240,220);
this.setVisible(true);

}
public static void main(String[] args)
{
gide test1=new gide();
}

public void actionPerformed(ActionEvent e)
 {
  int timer=0,i,scr;
//----------------------------------------------------
if(e.getSource()==startitem)
{

while(timer!=60)
{
i=(int)(Math.random()*8);
timer=timer+1;
jb[i].addActionListener(this);jb[i].setLabel("mouce");
try
  {
  this.wait(500);
  } 
  catch(InterruptedException e2)
  {
   
  }

jb[i].removeActionListener(this);jb[i].setLabel("");



try
  {
  this.wait(500);
  } 
  catch(InterruptedException e2)
  {
   
  }
   
jt2.setText(String.valueOf(timer));
}
JOptionPane.showMessageDialog(this,"you get the score:"+jt1.getText(),"the score got",1);
}
//-------------------------------------------------------------
if(e.getSource()==pauseitem){ try{this.wait(9999999);} catch(InterruptedException e2){} }
if(e.getSource()==aboutitem){JOptionPane.showMessageDialog(this,"yuge product","Message about the product",1);}
if(e.getSource()==closeitem){System.exit(0);}


if(e.getSource()==jb[0])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[1])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[2])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[3])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[4])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[5])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[6])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[7])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[8])

scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}

 }


}


------解决方案--------------------
我是 菜鸟。。。。在你基础上最少的修改 添加了多线程。望采纳

//灰常低级的打地鼠游戏
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import java.lang.*;
import java.awt.Button;
import java.lang.Thread;
//------------------------------------------------------------

class gide extends JFrame implements ActionListener{
boolean flag;
JMenuItem startitem,pauseitem,aboutitem,closeitem;
JMenu filemenu,systemmenu;
JMenuBar mbar;
JButton[] jb=new JButton[9];
JTextField jt1,jt2;

gide()
{
super("打地鼠o(∩_∩)o");
startitem=new JMenuItem("开始");
startitem.addActionListener(this);

pauseitem=new JMenuItem("暂停");
pauseitem.addActionListener(this);

aboutitem=new JMenuItem("关于");
aboutitem.addActionListener(this);

closeitem=new JMenuItem("关闭");
closeitem.addActionListener(this);

filemenu=new JMenu("文件");
systemmenu=new JMenu("系统");

filemenu.add(startitem);
filemenu.add(pauseitem);

systemmenu.add(aboutitem);
systemmenu.add(closeitem);

mbar=new JMenuBar();

mbar.add(filemenu);
mbar.add(systemmenu);

this.setJMenuBar(mbar);

jt1=new JTextField("0");
jt1.setBounds(0,0,60,40);

jt2=new JTextField("0");
jt2.setBounds(60,0,60,40);

jb[0]=new JButton("");

jb[0].setBounds(0,50,70,30);

jb[1]=new JButton("");
jb[1].setBounds(80,50,70,30);

jb[2]=new JButton("");
jb[2].setBounds(160,50,70,30);

jb[3]=new JButton("");
jb[3].setBounds(0,90,70,30);

jb[4]=new JButton("");
jb[4].setBounds(80,90,70,30);

jb[5]=new JButton("");
jb[5].setBounds(160,90,70,30);

jb[6]=new JButton("");
jb[6].setBounds(0,130,70,30);

jb[7]=new JButton("");
jb[7].setBounds(80,130,70,30);

jb[8]=new JButton("");
jb[8].setBounds(160,130,70,30);

Container wcontainer=this.getContentPane();

wcontainer.setLayout(null);

wcontainer.add(jb[1]);
wcontainer.add(jb[2]);
wcontainer.add(jb[3]);
wcontainer.add(jb[4]);
wcontainer.add(jb[5]);
wcontainer.add(jb[6]);
wcontainer.add(jb[7]);
wcontainer.add(jb[8]);
wcontainer.add(jb[0]);
wcontainer.add(jt1);
wcontainer.add(jt2);

this.setSize(240,220);
this.setVisible(true);

}
public static void main(String[] args)
{
gide test1=new gide();
}

public void actionPerformed(ActionEvent e)
 {
int timer=0,i,scr;
//----------------------------------------------------
if(e.getSource()==startitem)
{
new MyThread(this).start();

}
//-------------------------------------------------------------
if(e.getSource()==pauseitem){ try{this.wait(9999999);} catch(InterruptedException e2){} }
if(e.getSource()==aboutitem){JOptionPane.showMessageDialog(this,"yuge product","Message about the product",1);}
if(e.getSource()==closeitem){System.exit(0);}


if(e.getSource()==jb[0])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[1])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[2])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[3])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[4])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[5])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[6])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[7])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}
if(e.getSource()==jb[8])
{
scr=Integer.parseInt(jt1.getText())+1;
jt1.setText(String.valueOf(scr));
}

 }


}
class MyThread extends Thread
{
gide g;
public MyThread(gide g)
{
this.g = g;
}
public void run()
{

int timer =0;
while(timer!=60)
{
int i=(int)(Math.random()*8);
timer=timer+1;
g.jb[i].addActionListener(g);
g.jb[i].setLabel("mouce");
try
{

Thread.sleep(500);


}
catch(InterruptedException e2)
{}

g.jb[i].removeActionListener(g);
g.jb[i].setLabel("");





g.jt2.setText(String.valueOf(timer));
}
JOptionPane.showMessageDialog(null,"you get the score:"+g.jt1.getText(),"the score got",1); 
}
}