/*
 * xc.java
 *
 * Created on 2008年7月15日, 上午1:05
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */package menu;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import javax.swing.*;
/**
 *
 * @author Administrator
 */
public class xc extends JFrame{
    
    /** Creates a new instance of xc */
    javax.swing.JPanel jp1=new JPanel();
    JPanel jp2=new JPanel();
   
    JLabel jl1=new JLabel("999999");
    
    JButton jp1jb=new JButton("线程减");
    JButton jp1stop=new JButton("停止线程减");
    
    
    JLabel jl2=new JLabel("0");
    java.lang.Thread t;
    java.lang.Thread timet;
    
    
    public xc() {
        
        setSize(800,600);
        javax.swing.JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,jp1,jp2);
        add(jsp);
        jl1.setOpaque(true);
        jl1.setBackground(Color.RED);
        jp1.add(jl1);
        jp1jb.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                jp1class jp1c=new jp1class();
                t=new Thread(jp1c);//Thread类也可以实现线程,实例化这个类,然后将准备进行线程处理的对象作为参数传入
                t.start();
            }
        
    });
    jp1stop.addActionListener(new ActionListener(){    //此按钮停止t线程,按下后,能停止,但有错误报出如下:请指点如何消除此报错/*
java.lang.InterruptedException: sleep interrupted
        at java.lang.Thread.sleep(Native Method)
        at menu.xc$jp1class.run(xc.java:103)
        at java.lang.Thread.run(Thread.java:595)*/
            public void actionPerformed(ActionEvent e) {
                try
                {
                    
                    t.interrupt();
                   
                }
                catch(Exception ex)
                {
                    
                    ex.printStackTrace();
                }
            }
        
    });
    jp1.add(jp1jb);
    jp1.add(jp1stop);
    }
    
    class jp1class implements java.lang.Runnable//Runnable接口是java线程接口
    {
        public void run() { //Runnable接口只有一个函数run(),此函数必须由实现了此接口的类实现。
            try
            {
                while(java.lang.Integer.parseInt(jl1.getText())>0)
                {
                    int changeint=Integer.parseInt(jl1.getText())-1;
                    jl1.setText(changeint+"");
                    jl1.repaint();
                   
                   timet=new Thread();
                   
                   timet.sleep(1000);
                   
                }
            }
            catch(java.lang.Exception ex)
            {
                ex.printStackTrace();
            }
        }
        
    }
    
    
    
    
    public static void main(String[] args)
    {
        xc slxc=new xc();
        
        slxc.setVisible(true);
    }
}

解决方案 »

  1.   

    此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
    楼主【III_dont_know】截止到2008-07-15 09:17:26的历史汇总数据(不包括此帖):
    发帖的总数量:20                       发帖的总分数:520                      每贴平均分数:26                       
    回帖的总数量:17                       得分贴总数量:2                        回帖的得分率:11%                      
    结贴的总数量:20                       结贴的总分数:520                      
    无满意结贴数:1                        无满意结贴分:50                       
    未结的帖子数:0                        未结的总分数:0                        
    结贴的百分比:100.00%               结分的百分比:100.00%                  
    无满意结贴率:5.00  %               无满意结分率:9.62  %                  
    敬礼!
      

  2.   


      javax.swing.JPanel jp1=new JPanel(); 
        JPanel jp2=new JPanel(); 
      
        JLabel jl1=new JLabel("999999"); 
        
        JButton jp1jb=new JButton("线程减"); 
        JButton jp1stop=new JButton("停止线程减"); 
        
        
        JLabel jl2=new JLabel("0"); 
        java.lang.ThreadGroup  group=new ThreadGroup("搞来耍的");; 
        
        
        public Test() { 
            
            setSize(800,600); 
            javax.swing.JSplitPane jsp=new JSplitPane(JSplitPane.VERTICAL_SPLIT,true,jp1,jp2); 
            add(jsp); 
            jl1.setOpaque(true); 
            jl1.setBackground(Color.RED); 
            jp1.add(jl1); 
            jp1jb.addActionListener(new ActionListener(){ 
                public void actionPerformed(ActionEvent e) { 
                    jp1class jp1c=new jp1class(); 
                    java.lang.Thread timet=new Thread(group,jp1c);//Thread类也可以实现线程,实例化这个类,然后将准备进行线程处理的对象作为参数传入 
                    timet.start(); 
                } 
            
        }); 
        jp1stop.addActionListener(new ActionListener(){    //此按钮停止t线程,按下后,能停止,但有错误报出如下:请指点如何消除此报错 
               public void actionPerformed(ActionEvent e) { 
                    try 
                    { 
                        
                        group.interrupt(); 
                      
                    } 
                    catch(Exception ex) 
                    { 
                        
                        System.out.println("全部停止吧");
                    } 
                } 
            
        }); 
        jp1.add(jp1jb); 
        jp1.add(jp1stop); 
        } 
        
        class jp1class implements java.lang.Runnable//Runnable接口是java线程接口 
        { 
            public void run() { //Runnable接口只有一个函数run(),此函数必须由实现了此接口的类实现。 
                try 
                { 
                    while(java.lang.Integer.parseInt(jl1.getText())>0) 
                    { 
                        int changeint=Integer.parseInt(jl1.getText())-1; 
                        jl1.setText(changeint+""); 
                        jl1.repaint(); 
                        Thread.sleep(1000); 
                      
                    } 
                } 
                catch(java.lang.Exception ex) 
                { 
                    System.out.println("打扰到我了");
                } 
            } 
            
        } 
        
        
        
        
        public static void main(String[] args) 
        { 
            Test slxc=new Test(); 
            
            slxc.setVisible(true); 
        } 
      

  3.   

    大哥,还是报错我是在netbeans5.5中运行的,
    报错入下:
    java.lang.InterruptedException: sleep interrupted
            at java.lang.Thread.sleep(Native Method)
            at menu.xc$jp1class.run(xc.java:107)
            at java.lang.Thread.run(Thread.java:595)
    我单击at menu.xc$jp1class.run(xc.java:107)后,程序指向了Thread.sleep(1000); 是这个sleep出了问题吗?
      

  4.   

    这个不是报错,老大,这个是你使用了interrupted ,来中断程序的啊,呵呵。
      

  5.   

    意思就是说,无论如何,这个报告
    java.lang.InterruptedException: sleep interrupted 
            at java.lang.Thread.sleep(Native Method) 
            at menu.xc$jp1class.run(xc.java:107) 
            at java.lang.Thread.run(Thread.java:595) 
    都会出来的,是吗???
      

  6.   

    我说了的啊,细节上的东西我没有太关注,你可能会在调用interrupte之前先判断是否已经终端,如果已经中断,就不用中断了,就不会出现这个问题了
      

  7.   

    interrupted() 方法总是会报错的.别理它.
      

  8.   

    那不叫报错,是捕获到异常后打印的调用轨迹,你要是烦就把 ex.printStackTrace(); 这句去了。