how do i convert from private inner class to a runnable interface?

时间:2016-07-11 19:40:15

标签: java

so im given a working code as below, but then i have to convert it to use runnable interface

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++;
            }
        }
    }

}

}

i understand that i must do something tot he public class secondthread yeah? but i dont know how to convert it to runnable since everything is working already.

i tried deleting the "extends thread" and insert " implements runnable" but that just gives me error. adding both "extends thread" and "implements runnable" works but i doubt thats what i had to do

1 个答案:

答案 0 :(得分:0)

It's kind of hard for anybody to tell you what an error message means if you don't tell us what the error message said, but here's a guess:

If you do nothing else but change SecondThread extends Thread to SecondThread implements Runnable, then secondThread.start() isn't going to compile because your SecondThread class does not define a start() method.

So, given a ... implements Runnable object, how do you get a thread to run it?

I suggest you spend some time with the Java Concurrency tutorial. https://docs.oracle.com/javase/tutorial/essential/concurrency/

The answer to my question is right near the beginning.