Helpful Information
 
 
Category: Java and JSP
problem in java

i have the following code:


import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.*;

public class Revolve extends Applet {
String[] pageTitle = new String[6];
URL[] pageLink = new URL[6];
int current = 0;
Thread runner;

public void init() {
Color background = new Color(255, 255, 204);
setBackground(background);
Button goButton = new Button("Go");
goButton.addActionListener(this);
add(goButton);
}
}

but when i excute it in JCreator the following error appears:
addActionListener(java.awt.event.ActionListener) in java.awt.Button cannot be applied to (Revolve)
why?

The object that listens for action must implement ActionListerner, your applet does not.

how can i solve it?

As Josh just said if you want to use the addActionListener method you need to implement the ActionListener class.


public class Revolve extends Applet implements ActionListener {




}

And you must define all the methods in the ActionListener interface. I think theres only one (I could be wrong) but its:

public void actionPerformed(ActionEvent evt);

if I'm not mistaken.

Yes you must also define that method. Something like this for example:




public void actionPerformed(ActionEvent event){
String menuItemName = event.getActionCommand();

if(menuItemName.equals("Quit")){
System.exit(0);
}

else if(menuItemName.equals("Circle")){
whichShape = 0;
}
else if(menuItemName.equals("Square")){
whichShape = 1;
}
else if(menuItemName.equals("Rectangle")){
whichShape = 2;
}
else if(menuItemName.equals("Arc")){
whichShape = 3;
}
}

Or in your case since you have a button you could define it like so:



public void actionPerformed(ActionEvent event){
if(event.getSource() instanceof Button){
Button clickedButton = (Button) event.getSource();
if(clickedButton == goButton){
//Action to perform when go button clicked
}
}
}










privacy (GDPR)