关于线程运行与暂停的控制,请大家看看我的代码,总是不能控制线程的运行和暂停
我想通过一个boolean的stats来控制while,实现控制线程的目的,但好像没有用,另外还有别的方法可以控制吗?//getEvent.java
//show how to get the swing eventimport javax.swing.*;
import java.awt.*;
import java.awt.event.*;public class getEvent extends JApplet{
private JLabel myLabel = new JLabel("LABEL");
private JButton myButton = new JButton("BUTTON");
private boolean stats = false;
//a inner class for get the buttion event
private ActionListener b1 = new ActionListener(){
public void actionPerformed(ActionEvent e){
String text = ((JButton)e.getSource()).getText();
if(stats == false){
myButton.setText("now the Thread is go");
stats = true;
}else{
stats = false;
myButton.setText("nwo the Thread is stopped");
}
System.out.println(stats);
}
};
//a inner class for Thread
private Thread mt = new Thread(){
private int i = 1;
private String ii;
public void run(){
System.out.println(stats);
while(stats){
ii = String.valueOf(i);
myLabel.setText(ii);
try{
sleep(2000);
}catch(Exception e){}
i++;
}
}
};
public void init(){
//reg the ActionListener to the button
myButton.addActionListener(b1);
Container cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(myLabel);
cp.add(myButton);
mt.start();
}
public static void main(String[] args){
JApplet applet = new getEvent();
JFrame frame = new JFrame(applet.getClass().toString());
frame.getContentPane().add(applet);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(200,300);
applet.init();
applet.start();
frame.setVisible(true);
}

解决方案 »

  1.   

    /*
     * Created on 2005-8-14
     *
     * TODO To change the template for this generated file go to
     * Window - Preferences - Java - Code Style - Code Templates
     */
    package rmi;/**
     * @author slash
     * 
     * TODO To change the template for this generated type comment go to Window -
     * Preferences - Java - Code Style - Code Templates
     */
    //getEvent.java
    //show how to get the swing event
    //getEvent.java
    //show how to get the swing event
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;public class GetEvent extends JApplet {
    private JLabel myLabel = new JLabel("LABEL"); private JButton myButton = new JButton("BUTTON"); private boolean stats = false; //a inner class for get the buttion event
    private ActionListener b1 = new ActionListener() {
    public void actionPerformed(ActionEvent e) {
    String text = ((JButton) e.getSource()).getText();
    if (stats == false) {
    myButton.setText("now the Thread is go");
    stats = true;
    } else {
    stats = false;
    myButton.setText("nwo the Thread is stopped");
    }
    System.out.println(stats);
    }
    }; //a inner class for Thread
    private Thread mt = new Thread() {
    private int i = 1; private String ii; public void run() {
    System.out.println(stats);
    //多线程的概念:必须能跑起来
    while (true) {
    ii = String.valueOf(i);
    if(stats)
    myLabel.setText(ii);
    try {
    sleep(2000);
    } catch (Exception e) { }
    i++;
    }
    }
    }; public void init() {
    //reg the ActionListener to the button
    myButton.addActionListener(b1);
    Container cp = getContentPane();
    cp.setLayout(new FlowLayout());
    cp.add(myLabel);
    cp.add(myButton);
    mt.start();
    } public static void main(String[] args) {
    JApplet applet = new GetEvent();
    JFrame frame = new JFrame(applet.getClass().toString());
    frame.getContentPane().add(applet);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setSize(200, 300);
    applet.init();
    applet.start();
    frame.setVisible(true);
    }
    }