java; cannot instantiate the type Runnable 16777373

代码是这样的

import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.lang.Runnable;

public class SleepMethodTest extends JFrame{
    private Thread t;
    private static Color[] color = { Color.BLACK,Color.BLUE,Color.YELLOW,Color.CYAN,Color.GREEN,Color.ORANGE,Color.YELLOW,Color.RED,Color.PINK,Color.LIGHT_GRAY};
    private static final Random rand = new Random();
    private static Color getC(){
        return color[rand.nextInt(color.length)];
    }
    public SleepMethodTest(){
        t = new Thread ( new Runnable() ){
            int x = 30;
            int y = 50;
            public void run(){
                while(true){
                    try{
                        Thread.sleep(100);
                    }catch (InterruptedException e){
                        e.printStackTrace();
                    }
                    Graphics graphics = getGraphics();
                    graphics.setColor(getC());
                    graphics.drawLine(x,y,100,y++);
                    if(y>80){
                        y=50;
                    }
                }
            }
        };
        t.start();
    }
    public static void main(String[]args){
        init(new SleepMethodTest(),100,100);
    }
    public static void init(JFrame frame, int width, int height){
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width,height);
        frame.setVisible(true);
    }
}

其实跟下图内(自己买的书)里的代码基本一致= =

img

然而出现cannot instantiate 的错误 = =

= =

看了一下你的代码,一般情况下书上的源代码是需要稍加改造的,就可以运行了,如有帮助,请采纳,谢谢
改造后的代码如下:

import java.awt.*;
import java.util.Random;
import javax.swing.*;
import java.lang.Runnable;

public class SleepMethodTest extends JFrame {
    private Thread t;
    private static Color[] color = {Color.BLACK, Color.BLUE, Color.YELLOW, Color.CYAN, Color.GREEN, Color.ORANGE, Color.YELLOW, Color.RED, Color.PINK, Color.LIGHT_GRAY};
    private static final Random rand = new Random();

    private static Color getC() {
        return color[rand.nextInt(color.length)];
    }

    // 更改后的代码
    public SleepMethodTest() {
        t = new Thread(new Runnable() {
            @Override
            public void run() {
                int x = 30;
                int y = 50;
                while (true) {
                    try {
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    Graphics graphics = getGraphics();
                    graphics.setColor(getC());
                    graphics.drawLine(x, y, 100, y++);
                    if (y > 80) {
                        y = 50;
                    }
                }
            }
        });
        t.start();
    }

    public static void main(String[] args) {
        init(new SleepMethodTest(), 500, 500);
    }

    public static void init(JFrame frame, int width, int height) {
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(width, height);
        frame.setVisible(true);
    }
}


运行结果:
img

jwt?谁还用啊