I only studied till methods and am not patient enough to wait till my UNI teaches me the rest so i found this interesting… I want to create a GUI with a circle in the middle that moves according to what button you press with java. after alot of reading i managed to get it done this way. So before i got to the error mentioned in the question i had 19 errors and only when i figured all of them out this error suddenly appeared. I hope i don’t tick anybody’s nerve cause am sure this mistake is due to some lack of programming foundation.
any help will be very much appreciated. I pointed out where the error occured by “<—”
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();
}
}
}
Your
Move_the_ballclass must implementjava.awt.event.ActionListenerinterface for this to work.