-
wrong source code
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
public class PlayBalloon extends Applet implements ActionListener {
private Button grow, shrink, left, right;
private Balloon myBalloon;
public void init() {
grow = new Button("Grow");
add(grow);
grow.addActionListener(this);
shrink = new Button("Shrink");
add(shrink);
grow.addActionListener("this");
left = new Button("Left");
add(left);
left.addActionListener(this);
right = new Button("Right");
add(right);
right.addActionListener(this);
myBalloon = new Balloon();
}
public void actionPerformed(ActionEvent event) {
if (event.getSource() == grow)
myBalloon.grow();
if (event.getSource() == shrink)
myBalloon.shrink();
if (event.getSource() == left)
myBalloon.left();
if (event.getSource() == right)
myBalloon.right();
repaint();
}
public void paint(Graphics g) {
myBalloon.display(g);
}
}
class Balloon {
private int diameter = 10;
private int xCoord = 20, yCoord = 50;
public void display(Graphics g) {
g.drawOval(xCoord, yCoord, diameter, diameter);
}
public void left() {
xCoord = xCoord - 10;
}
public void right() {
xCoord = xCoord + 10;
}
public void grow() {
diameter = diameter + 5;
}
public void shrink() {
diameter = diameter - 5;
}
}
error line 16, how to edit?
-
What's the error you get there? Seems you have copied this code and don't know what's exactly happen. And I don't think that anyone wants to edit this code for you here in our community.
Post your complete error message and ask your question more clearly.
-
error line 16 shrink.addActionListener("this");
it mention
addActionListener(java.awt.event.ActionListener) in java.awt.Button can not be applied to (java.lang.String)
how to solve it?
-
The error message is telling you exactly what's wrong. You're passing a String to the addActionListener method when it's expecting an ActionListener object. Quotes around "this" make it a String. No quotes makes it refer to the current object. So get rid of the quotes.
-
Simply,
and are completely two things.