线程怎么不能停止??
为什么stop按纽停止线程不能停止??
//TimeThread.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class TimeThread extends JFrame
{
public static JTextField textField;
private JButton button1,button2;
private boolean start=true;
Thread t;
int flag=1;
public TimeThread()
{
super("Time Thread test");
Container c=getContentPane();
c.setLayout(new FlowLayout());
textField=new JTextField(10);
textField.setEditable(false);
c.add(textField);
button1=new JButton("start");
button2=new JButton("stop");
c.add(button1);
c.add(button2);
TimeThread1 thread=new TimeThread1();
t=new Thread(thread);
button1.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(start && flag==1)
{
t.start();
start=false;
}
else
{
t.resume();
start=false;
}
}
});
button2.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
if(Thread.currentThread().getName().equals("Thread-1"))
//if(t.getName().equals("Thread-0")) //here why wrong
{
t.suspend();
start=true;
flag++;
}
}
});
setSize(180,150);
show();
}
public static void main(String[] args)
{
TimeThread t=new TimeThread();
t.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
//TimeThread1.java
import java.util.*;
public class TimeThread1 implements Runnable
{
public void run()
{
try
{
while(true)
{
Thread.sleep(1000);
Date date=new Date();
String s=date.getHours()+":"+date.getMinutes()+":"+date.getSeconds();
TimeThread.textField.setText(s);
}
}
catch(Exception e){}
}
}