当前位置: 代码迷 >> Java相关 >> [求助]这个程序哪里出错了,怎么没反应
  详细解决方案

[求助]这个程序哪里出错了,怎么没反应

热度:92   发布时间:2007-05-22 01:06:40.0
[求助]这个程序哪里出错了,怎么没反应

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

import java.awt.event.*;


public class GoodLuck extends JFrame implements ActionListener,Runnable
{
static JTextField jf = new JTextField(20);
JButton jb1=new JButton("抽奖");
JButton jb2=new JButton("停止");
JPanel jp=new JPanel();

public GoodLuck()
{
Container con=getContentPane();

jb1.addActionListener(this);
jb2.addActionListener(this);

jb1.setActionCommand("start");

jp.add(jb1);
jp.add(jb2);

con.add(jf,"North");
con.add(jp,"South");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300,200,400,400);
setVisible(true);

}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("start"))
{
Thread t=new Thread();
t.start();
}

}

public static void main(String arg[])
{
new GoodLuck();
}

public void run()
{
for(int i=0;i<10;i++)
{

int num=(int)(Math.random()*36)+1;
if(num<10)
jf.setText("0"+num);
else
jf.setText(""+num);
try
{
Thread.sleep(1000);
}
catch(InterruptedException ex)
{

}

}


}



}

搜索更多相关的解决方案: import  public  抽奖  

----------------解决方案--------------------------------------------------------
int num=(int)(Math.random()*36)+1;
这里随机赋值,好像不对.

----------------解决方案--------------------------------------------------------

if(e.getActionCommand().equals("start"))
{
Thread t=new Thread();
t.start();
}

你这是什么意思

Thread对象里面的run方法可是空的,什么都不会执行的

哪有像你这样起动一个线程的


----------------解决方案--------------------------------------------------------

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

import java.awt.event.*;
import java.awt.image.BufferedImage;


public class GoodLuck extends JFrame implements ActionListener
{
static JTextField jf = new JTextField(20);
JButton jb1=new JButton("抽奖");
JButton jb2=new JButton("停止");
JPanel jp=new JPanel();
myThread thread=new myThread();

public GoodLuck()
{
Container con=getContentPane();

jb1.addActionListener(this);
jb2.addActionListener(this);

jb1.setActionCommand("start");

jp.add(jb1);
jp.add(jb2);

con.add(jf,"North");
con.add(jp,"South");

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBounds(300,200,400,400);
setVisible(true);
Graphics g = this.getGraphics();
g.drawString("aaaa",100,100);

}

public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("start"))
{

if(this.thread.isAlive())
{
this.thread.resume();
System.out.println("Resume()");
}
else
{
this.thread.start();
System.out.println("Start()");
}

}
else if(e.getSource()==jb2)
/*synchronized (this) {
try {
this.wait();
} catch (InterruptedException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}*/
{
this.thread.suspend();
System.out.println("Suspend()");
}

}

public static void main(String arg[])
{
GoodLuck g=new GoodLuck();


}


public class myThread extends Thread
{


public void run()
{
for(int i=0;;i++)
{


int num=(int)(Math.random()*36)+1;
if(num<10)
jf.setText("0"+num);
else
jf.setText(""+num);
try
{
Thread.sleep(100);
}
catch(InterruptedException ex)
{

}


}

}
}

}

高手再改一下!!


----------------解决方案--------------------------------------------------------
  相关解决方案