线程怎么不能停止??

tongdayong1981 tongdayong1981 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
为什么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){}
}

}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
tongdayong1981
Java SE 62,621 成员 307,251 内容