gpt4 book ai didi

java - 如何从私有(private)内部类转换为可运行接口(interface)?

转载 作者:太空宇宙 更新时间:2023-11-04 12:26:42 25 4
gpt4 key购买 nike

所以我给出了如下的工作代码,但是我必须将其转换为使用可运行的接口(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++;
}
}
}

}

}

我知道我必须对公共(public)类第二线程做点什么,是吗?但我不知道如何将其转换为可运行的,因为一切都已经正常工作了。

我尝试删除“扩展线程”并插入“实现可运行”,但这只会给我错误。添加“扩展线程”和“实现可运行”都有效,但我怀疑这就是我必须做的

最佳答案

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

如果您除了将 SecondThread extends Thread 更改为 SecondThread Implements Runnable 之外什么也不做,那么 secondThread.start() 将无法编译,因为您的 SecondThread 类没有定义 start() 方法。

那么,给定一个 ... 实现 Runnable 对象,如何获得一个线程来运行它?

我建议您花一些时间学习 Java 并发教程。 https://docs.oracle.com/javase/tutorial/essential/concurrency/

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

关于java - 如何从私有(private)内部类转换为可运行接口(interface)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38314902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com