编译错误,找不到符号和构造函数不能应用于给定的类型

时间:2011-06-17 15:46:00

标签: java compiler-errors

我收到2个错误:

ControllingSpeed.java:89: constructor Thread in class Thread cannot be applied to given types
   Thread th=new Thread(r);
              ^
required: no arguments
found: Runnable
ControllingSpeed.java:90: cannot find symbol
   th.start(r);
     ^
symbol:   method start(Runnable)
location: class Thread
2 errors

我不知道错误的原因。

// Demo On JSlider

import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
import javax.swing.event.*;
import java.lang.Thread.*;

class ControllingSpeed extends JPanel{

...<snip>...

public void threadForSpeed(final ChangeEvent ce) {   // <----- cause of error ?
    try { 
        Runnable r = new Runnable() {
            public void run() {
                changeSpeed(ce);
            }
        };
        Thread th=new Thread(r);
        th.start(r);                // <---- till here
    } catch(Exception exc) {
        System.out.println(exc);
    }  
}

2 个答案:

答案 0 :(得分:5)

问题很可能是你的软件包中的某个地方你创建了一个名为Thread的类,这个类正在超越java.lang.Thread类。那个类(你自己的)可能看起来像这样:

class Thread {}

尝试使用完全限定名称指定Thread类,如下所示:

    java.lang.Thread th = new java.lang.Thread(r);

此外,Thread#start()方法不接受参数,因此从参数中删除Runnable“r”:

    th.start();

答案 1 :(得分:1)

一个错误是Thread类的start方法不带参数。它只是:

   th.start();