package 线程;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class TestThread extends JFrame {
/**
*
*/
//private static final long serialVersionUID = 1L;
JPanel jPanel1 = new JPanel();
JButton startButton = new JButton("start");
JButton stopButton = new JButton("stop");
MyThread thread = null;
private boolean isContinue;
public TestThread() {
try {
jbInit();
} catch (Exception e) {
e.printStackTrace();
}
}
private void jbInit() throws Exception {
startButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
startButton_actionPerformed(e);
}
});
stopButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
stopButton_actionPerformed(e);
}
});
Container con=getContentPane();
con.add(jPanel1, BorderLayout.CENTER);
jPanel1.add(startButton);
startButton.setBounds(36, 105, 82, 30);
jPanel1.add(stopButton);
stopButton.setBounds(160, 108, 100, 31);
}
void startButton_actionPerformed(ActionEvent e) {
if (thread != null)//这是啥意思?
isContinue=false;
thread = new MyThread();//为什么我改为MyThread thread=new MyThread();运行会报错?
thread.start();
}
void stopButton_actionPerformed(ActionEvent e) {
if (thread != null)
isContinue=false;
thread = null;//这又是啥意思?
}
public static void main(String[] args) {
TestThread test = new TestThread();
test.setBounds(300,300,300, 80);
test.setVisible(true);
}
private class MyThread extends Thread {
public MyThread() {
//isContinue=true;//为什么不在意开头哪里就定义iscontinue=true?
}
public void run() {
System.out.println("\n\n");
while (true && isContinue) {
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("Java 编程词典\thttp://www.mrbccd.com");
}
}
}
}