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)
{
}
}
}
}
----------------解决方案--------------------------------------------------------
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)
{
}
}
}
}
}
高手再改一下!!
----------------解决方案--------------------------------------------------------