编辑:感谢所有指出拼写错误的人!然而,我的计时器仍然有问题。由于某些原因,计时器不会导致每秒后重新绘制窗口。编辑后的代码发布在下面!
C:\java>javac Project2.java
Project2.java:8: error: Project2 is not abstract and does not override abstract
method actionPerformed(ActionEvent) in ActionListener
public class Project2 extends Applet implements ActionListener
^
Project2.java:19: error: <anonymous Project2$1> is not abstract and does not ove
rride abstract method actionPerformed(ActionEvent) in ActionListener
ActionListener getNewValues = new ActionListener() {
^
2 errors
该程序的目的是模拟龟兔赛跑,使用随机数生成器来确定龟兔在任何给定转弯时可以向前或向后移动多少次。
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.lang.*;
import javax.swing.*;
import java.awt.Color;
public class Project2 extends Applet
{
int squaret = 1;
int squareh = 1; //initial location of tortoise and hare
int move;
String tmessage;
String hmessage;
Timer timer;
public void init()
{
timer = new Timer(1000, getNewValues);
timer.addActionListener(getNewValues);
}
ActionListener getNewValues = new ActionListener() {
public void actionPerformed(ActionEvent e)
{
repaint();
}
};
public void paint (Graphics g)
{
move = (int)(Math.random() * 10) + 1;
if (move > 8)
{
squaret -= 6;
tmessage = "Tortoise slips!";
if (squaret < 1)
squaret = 1;
}
else if (move > 6)
{
squaret += 1;
tmessage = "Tortoise plods slowly along.";
if (squaret > 49)
squaret = 50;
squareh -=2;
hmessage = "Hare slips slightly.";
if (squareh < 1)
squareh = 1;
}
else if (move > 5)
{
squaret += 1;
tmessage = "Tortoise plods slowly along.";
if (squaret > 49)
squaret = 50;
squareh -=12;
hmessage = "Hare makes a big slip.";
if (squareh < 1)
squareh = 1;
}
else if (move > 2)
{
squaret += 3;
tmessage = "Tortoise plods along quickly.";
if (squaret > 49)
squaret = 50;
squareh += 1;
hmessage = "Hare makes a small hop.";
if (squareh > 49)
squareh = 50;
}
else
{
squaret += 3;
tmessage = "Tortoise plods along quickly.";
if (squaret > 49)
squaret = 50;
squareh += 9;
hmessage = "Hare makes a big hop.";
if (squareh > 49)
squareh = 50;
}
g.drawString("Start (Square 1)", 0, 70);
g.drawString("Finish (Square 50)", 900, 70);
//determine positions for each area
//each box is ten wide and 150 tall
final int WIDTH_OF_OVAL = 4;
final int HEIGHT_OF_OVAL = 4;
final int WIDTH_OF_SQUARE = 20;
final int HEIGHT_OF_SQUARE = 20;
g.setColor(Color.GREEN);
g.fillOval(((WIDTH_OF_SQUARE - WIDTH_OF_OVAL) / 2) + WIDTH_OF_SQUARE * (squaret - 1), ((HEIGHT_OF_SQUARE - HEIGHT_OF_OVAL) / 2), WIDTH_OF_OVAL, HEIGHT_OF_OVAL);
g.setColor(Color.YELLOW);
g.fillOval(((WIDTH_OF_SQUARE - WIDTH_OF_OVAL) / 2) + WIDTH_OF_SQUARE * (squaret - 1), ((HEIGHT_OF_SQUARE - HEIGHT_OF_OVAL) / 2) + HEIGHT_OF_SQUARE, WIDTH_OF_OVAL, HEIGHT_OF_OVAL);
//show messages
g.setColor(Color.BLACK);
g.drawString(tmessage, 10, 100);
g.drawString(hmessage, 10, 120);
g.drawLine(0, HEIGHT_OF_SQUARE, WIDTH_OF_SQUARE * 50, HEIGHT_OF_SQUARE); //draw horizontal middle line
for (int i = 0; i < 50; i++) //draw vertical lines
{
int width = (i + 1) * WIDTH_OF_SQUARE;
g.drawLine(width, 0, width, HEIGHT_OF_SQUARE * 2);
}
if (squaret > 49 && squareh > 49)
{
g.drawString("Tie!", 500, 60);
timer.stop();
}
else if (squaret > 49)
{
g.drawString("Turtle wins!", 500, 60);
timer.stop();
}
else if (squareh > 49)
{
g.drawString("Hare wins!", 500, 60);
timer.stop();
}
else
{
}
update(g);
}
public static void main(String[] args)
{
Project2 panel = new Project2();
JFrame application = new JFrame();
application.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
application.add(panel);
application.setSize(2600, 300);
application.setVisible(true);
}
}
我已经执行了方法操作,所以我不确定为什么会出现错误。任何反馈或帮助都将不胜感激!
你有一个拼写错误在action执行
,你错过了r。更改为:
public void actionPerformed(ActionEvent e)
{
repaint();
}
有人可以向我解释为什么它总是给我这个错误 错误:MyPanel不是抽象的,并且不重写ActionListener公共类MyPanel extends JPanel实现ActionListener中的抽象方法actionPerformed(ActionEvent){ 我想我做的一切都是对的,我不知道我做错了什么,这段代码用于测试使图像水平移动 这是我的密码 Main.java 我的框架。Java语言
如何解决此错误,我不想删除。请任何人在不删除`implements actionlistener的情况下更正此错误。
错误:BodyMassApplet不是抽象的,并且不重写ActionListener公共类中的抽象方法actionPerformed(ActionEvent)BodyMassApplet扩展Applet实现ActionListener 代码:
请帮帮忙。我对编码不是新手,但对Java是新手。我不知道我做错了什么。我正在用一本书学习java,下面的代码是我此刻正在研究的。我去了books网站,下载了这个程序的源代码,它给了我同样的错误信息。谁来帮帮我。我知道有人问我这个问题,但我被困住了,真的需要一些帮助。
它会给我错误信息 ExitButtonHandler不是抽象的,并且不重写ActionListener中的抽象方法actionPerformed(ActionEvent
//AListener不是抽象的,也不会覆盖抽象方法actionPerform(ActionEvener)中的ActionListener????