CodingForums.com

CodingForums.com (http://www.codingforums.com/index.php)
-   Java and JSP (http://www.codingforums.com/forumdisplay.php?f=54)
-   -   problem in java (http://www.codingforums.com/showthread.php?t=16491)

Aymen++ 03-17-2003 11:14 AM

problem in java
 
i have the following code:
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 05:14 PM

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

Aymen++ 03-17-2003 05:25 PM

how can i solve it?

Spookster 03-17-2003 07: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 08: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 09:32 PM

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

PHP Code:


    
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 09:53 PM

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

PHP Code:

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




All times are GMT +1. The time now is 03:46 PM.

Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.