我只研究直到方法,并且没耐心等到我的UNI教我其余的所以我发现这很有趣...我想创建一个带有圆圈的GUI,根据你按下的按钮移动用java。经过很多阅读后,我设法以这种方式完成。因此,在我得到问题中提到的错误之前,我有19个错误,只有当我想出所有错误时,这个错误突然出现了。我希望我不会勾结任何人的神经因为我确定这个错误是由于缺乏编程基础。 任何帮助将非常感谢。我指出了“< ---”
发生错误的地方import java.awt.*;
import java.awt.event.*;
public class Move_the_ball extends Frame {
public static void main(String[] args) {
Move_the_ball me = new Move_the_ball();
me.setVisible(true);
}
public Move_the_ball(){
setSize(700,700);
setLocation(100,100);
setTitle("Moving the ball");
setLayout(new BorderLayout());
Panel buttonPanel = new Panel();
buttonPanel.setBackground(Color.blue);
buttonPanel.setLayout(new FlowLayout());
Button goUp = new Button("Go up");
goUp.addActionListener(this); <-----------------
buttonPanel.add(goUp);
Button goDown = new Button("Go down");
goDown.addActionListener(this); <--------------
buttonPanel.add(goDown);
Button turnRight = new Button("Turn right");
turnRight.addActionListener(this); <-------------
buttonPanel.add(turnRight);
Button turnLeft = new Button("Turn left");
turnLeft.addActionListener(this); <---------------
buttonPanel.add(turnLeft);
}
public void paint(Graphics g)
{
g.fillOval(x,y,h,d);
}
private int x=200;
private int y=100;
private int h=50;
private int d=50;
public void actionPerformed (ActionEvent e){
String actionCommand = e.getActionCommand();
if(actionCommand.equals("Go up"))
{
y=y-5;
repaint();
} else if (actionCommand.equals("Go down"))
{
y=y+5;
repaint();
} else if (actionCommand.equals("Turn right"))
{
x=x+5;
repaint();
} else if (actionCommand.equals("Turn left"));
{
x=x-5;
repaint();
}
}
}
答案 0 :(得分:4)
您的Move_the_ball
课程必须实现java.awt.event.ActionListener
界面才能实现此目的。
import java.awt.*;
import java.awt.event.*;
public class Move_the_ball extends Frame implements ActionListener {
public void actionPerformed(ActionEvent e) {
// handle the e event
}