PDA

View Full Version : problem in java


Aymen++
03-17-2003, 12:14 PM
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?

Josh Campbell
03-17-2003, 06:14 PM
The object that listens for action must implement ActionListerner, your applet does not.

Aymen++
03-17-2003, 06:25 PM
how can i solve it?

Spookster
03-17-2003, 08:19 PM
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 {




}

Josh Campbell
03-17-2003, 09:52 PM
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.

Spookster
03-17-2003, 10:32 PM
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;
}
}

Spookster
03-17-2003, 10:53 PM
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
}
}
}