Java 如何将私有内部类转换为可运行接口?

Java 如何将私有内部类转换为可运行接口?,java,Java,所以我得到了一个如下所示的工作代码,但我必须将其转换为使用runnable接口 package lab12; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JButton; import java.awt.BorderLayout; import java.awt.Color; import java.awt.FlowLayout; import java.awt.Graphics; imp

所以我得到了一个如下所示的工作代码,但我必须将其转换为使用runnable接口

package lab12;

 import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class MultiThreaded2 extends JFrame implements ActionListener {
public static final int WIDTH = 600;
public static final int HEIGHT = 400;
public static final int FILL_WIDTH = 600;
public static final int FILL_HEIGHT = 400;
public static final int SQUARE_SIZE = 10;
public static final int PAUSE = 100; // milliseconds

private JPanel box;

public static void main(String[] args) {
    MultiThreaded2 gui = new MultiThreaded2();
    gui.setVisible(true);
}

public MultiThreaded2() {
    setSize(WIDTH, HEIGHT);
    setTitle("Threaded Fill Demo");
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    setLayout(new BorderLayout());

    box = new JPanel();
    add(box, BorderLayout.CENTER);

    JPanel buttonPanel = new JPanel();
    buttonPanel.setLayout(new FlowLayout());

    JButton startButton = new JButton("Start");
    startButton.addActionListener(this);
    buttonPanel.add(startButton);

    JButton stopButton = new JButton("Stop");
    stopButton.addActionListener(this);
    buttonPanel.add(stopButton);
    add(buttonPanel, BorderLayout.SOUTH);
}

public void run() {
    Graphics g = box.getGraphics();

    int count = 0;

    for (int y = 0; y < FILL_HEIGHT; y = y + SQUARE_SIZE) {
        for (int x = 0; x < FILL_WIDTH; x = x + SQUARE_SIZE) {

            if (count % 2 == 0) {
                g.setColor(Color.red);
                g.fillRect(x, y, SQUARE_SIZE, SQUARE_SIZE);
            } else {
                g.setColor(Color.blue);
                g.drawRect(x, y, SQUARE_SIZE, SQUARE_SIZE);
            }
            try {
                Thread.sleep(PAUSE);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            count++;
        }
    }
}


public void actionPerformed(ActionEvent e) {
    if (e.getActionCommand().equals("Start")) {
        SecondThread secondThread = new SecondThread();
        secondThread.start();
    } else if (e.getActionCommand().equals("Stop")) {
        System.exit(0);
    }
}

private class SecondThread extends Thread {
    public void run() {
        Graphics g = box.getGraphics();

        int count = 0;

        for (int y = 0; y < FILL_HEIGHT; y = y + SQUARE_SIZE) {
            for (int x = 0; x < FILL_WIDTH; x = x + SQUARE_SIZE) {

                if (count % 2 == 0) {
                    g.setColor(Color.red);
                    g.fillRect(x, y, SQUARE_SIZE, SQUARE_SIZE);
                } else {
                    g.setColor(Color.blue);
                    g.drawRect(x, y, SQUARE_SIZE, SQUARE_SIZE);
                }
                try {
                    Thread.sleep(PAUSE);
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                count++;
            }
        }
    }

}
lab12包装;
导入javax.swing.JFrame;
导入javax.swing.JPanel;
导入javax.swing.JButton;
导入java.awt.BorderLayout;
导入java.awt.Color;
导入java.awt.FlowLayout;
导入java.awt.Graphics;
导入java.awt.event.ActionListener;
导入java.awt.event.ActionEvent;
公共类多线程2扩展JFrame实现ActionListener{
公共静态最终整数宽度=600;
公共静态最终内部高度=400;
公共静态最终整型填充宽度=600;
公共静态最终内部填充高度=400;
公共静态最终整数平方_尺寸=10;
公共静态最终整数暂停=100;//毫秒
私人JPanel箱;
公共静态void main(字符串[]args){
MultiThreaded2 gui=新的MultiThreaded2();
setVisible(true);
}
公共多线程2(){
设置尺寸(宽度、高度);
setTitle(“螺纹填充演示”);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(新的BorderLayout());
box=新的JPanel();
添加(框、边框布局、中心);
JPanel buttonPanel=新的JPanel();
buttonPanel.setLayout(新的FlowLayout());
JButton startButton=新JButton(“开始”);
addActionListener(这个);
按钮面板添加(开始按钮);
JButton stopButton=新JButton(“停止”);
stopButton.addActionListener(这个);
按钮面板。添加(停止按钮);
添加(按钮面板,边界布局。南部);
}
公开募捐{
Graphics g=box.getGraphics();
整数计数=0;
用于(整数y=0;y<填充高度;y=y+方形尺寸){
用于(整数x=0;x
}

我明白我必须为公共课做点什么是吗?但我不知道如何将它转换为runnable,因为一切都已经开始工作了


我试图删除“扩展线程”并插入“implements runnable”,但这只会给我带来错误。添加“extends thread”和“implements runnable”都有效,但我怀疑这是我必须要做的

如果您不告诉我们错误消息的内容,任何人都很难告诉您错误消息的含义,但这里有一个猜测:

如果只将
SecondThread extensed Thread
更改为
SecondThread implements Runnable
,则不会编译
SecondThread.start()
,因为您的
SecondThread
类未定义
start()
方法

因此,给定一个
。。。实现可运行的
对象,如何让线程运行它

我建议您花些时间学习Java并发教程


我的问题的答案就在开头。

如果你不告诉我们错误消息是什么意思,任何人都很难告诉你,但这里有一个猜测:

如果只将
SecondThread extensed Thread
更改为
SecondThread implements Runnable
,则不会编译
SecondThread.start()
,因为您的
SecondThread
类未定义
start()
方法

因此,给定一个
。。。实现可运行的
对象,如何让线程运行它

我建议您花些时间学习Java并发教程


我的问题的答案就在开头附近。

听起来像>我尝试删除“扩展线程”并插入“implements runnable”<这是您应该做的。尝试并修复错误,并尝试了解可运行程序和线程是如何协同工作的。通常,您要做的是更改
类MyThread扩展线程{…}
新建MyThread().start()
to
class MyTask implements Runnable{…}
new Thread(new MyTask()).start()
。听起来像>我试图删除“extends Thread”并插入“implements Runnable”<这是你应该做的。尝试并修复错误,并尝试了解可运行程序和线程是如何协同工作的。通常,您要做的是更改
类MyThread扩展线程{…}
新建MyThread().start()
类MyTask实现可运行的{…}
新线程(new MyTask()).start()