阿飞大爷 2015-09-25 12:56 采纳率: 0%
浏览 1721

下面是一段Java代码,里面有几处看不懂,望大神指教,不胜感激。

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");
        }
    }
}

}

  • 写回答

5条回答 默认 最新

  • 无心含笑 2015-09-25 13:06
    关注

    if (thread != null)//这是啥意思? 如果线程不为空
    thread = new MyThread();//为什么我改为MyThread thread=new MyThread();运行会报错?
    上面已经定义我thread ,现在定义thread ,生命周期是在当前函数里面。出了函数就没有了,所以会运行错误 。

    thread = null;//这又是啥意思?
    线程为空

    /isContinue=true;//为什么不在意开头哪里就定义iscontinue=true?
    这个看不懂你说的意思?

    评论

报告相同问题?

悬赏问题

  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试