求高手帮我看下这个java程序错在哪呀,总是不对

来源:百度知道 编辑:UC知道 时间:2024/05/25 06:51:28
这是一道模拟车子运行的程序
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;

public class Exercise14_15 extends JFrame {
public Exercise14_15() {
add(new RunningCar());
}
public static void main(String[] args) {
Exercise14_15 frame = new Exercise14_15();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 200);
frame.setVisible(true);
}
static class RunningCar extends JPanel implements ActionListener {
private static int displacement = 0;
private static int x[] = {20, 80, 40, 60};
private static int y[] = {130, 130, 110, 110};
public RunningCar() {
Timer timer = new Timer(20, this);
timer.start();
}
protected void painComponent(Graphics g) {
super.paintComponent(g);
g.fillOval(20 + displacement, 150, 20, 20);
g.fillOval(60 + displacement, 150, 20, 20)

首先你的paintComponent方法名写错了 另外还有些小问题 我帮你改了下代码 你看看吧

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

public class Exercise14_15 extends JFrame {
public Exercise14_15() {
add(new RunningCar());
}

public static void main(String[] args) {
Exercise14_15 frame = new Exercise14_15();
frame.setLocationRelativeTo(null);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 200);
frame.setVisible(true);
}

static class RunningCar extends JPanel implements ActionListener {
private static int displacement = 0;
private static int x[] = { 20, 80, 40, 60 };
private static int y[] = { 130, 130, 110, 110 };

public RunningCar() {
Timer timer = new Timer(20, this);
timer.start();
}

protected void paintComponent(Graphics g) {
g.setColor(Color.WHITE);
g.fillRect(0, 0, 600,