Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7

Thread: problem in java

  1. #1
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts

    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?

  • #2
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The object that listens for action must implement ActionListerner, your applet does not.

  • #3
    Regular Coder
    Join Date
    Nov 2002
    Posts
    180
    Thanks
    0
    Thanked 0 Times in 0 Posts
    how can i solve it?

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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 {




    }
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #5
    New Coder
    Join Date
    Jun 2002
    Posts
    66
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #6
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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
    CodingForums Supreme Overlord
    All Hail Spookster

  • #7
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    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
                
    }
             }

    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •