1
我做了一個簡單的形狀程序,在JPanel
內動畫一個球。我的公共類形狀標記一個錯誤說形狀不是抽象的(見下圖)動畫球JPanel子類不是抽象錯誤
形狀沒有抽象的和
ActionListener
形狀不重寫抽象方法actionPerformed(ActionEvent)
。 java:
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionListener;
import java.awt.geom.Ellipse2D;
import javafx.event.ActionEvent;
import javax.swing.*;
public class Shape extends JPanel implements ActionListener {
Timer t = new Timer(5, this);
double x = 0;
double y = 0;
double velX = 2;
double velY = 2;
public void painComponent(Graphics g) {
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
Ellipse2D circle = new Ellipse2D.Double(x, y, 40, 40);
g2.fill(circle);
t.start();
}
public void actionPerformed(ActionEvent e) {
if (x < 0 || x > 560) {
velX = -velX;
}
if(y < 0 || y > 360) {
velY = -velY;
}
x += velX;
y += velY;
repaint();
}
}
Main.java:
import javax.swing.JFrame;
public class Main {
public static void main(String[] args) {
Shape s = new Shape();
JFrame f = new JFrame();
f.add(s);
f.setVisible(true);
f.setSize(600,400);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.setTitle("Moving Ball");
}
}
注意的paintComponent的'不正確的拼寫()'。 – trashgod