Results 1 to 3 of 3
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Why am I getting errors on this code?

    I am trying to add an actionlistener that changes the text of a button when clicked. What am I doing wrong :(?


    Java Code:
    import javax.swing.*;
    import java.awt.*;
    public class butTrial implements ActionListener {
    JButton b;
    butTrial(){
    JFrame frame = new JFrame("E");
    frame.setSize(500,500);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
    JPanel p = new JPanel();
    frame.add(p);
    b = new JButton("Unclicked");
    p.add(b);
    b.addActionListener(this);
    }
    public void actionPerformed(ActionEvent e){
    b.setText("Clicked.");
    }
    public static void main(String[] args) {
    new butTrial();
    }
    }





    butTrial.java:3: error: cannot find symbol
    public class butTrial implements ActionListener {
    ^
    symbol: class ActionListener
    butTrial.java:16: error: cannot find symbol
    public void actionPerformed(ActionEvent e){
    ^
    symbol: class ActionEvent
    location: class butTrial
    butTrial.java:14: error: method addActionListener in class AbstractButton cannot be applied to given types;
    b.addActionListener(this);
    ^
    required: ActionListener
    found: butTrial
    reason: actual argument butTrial cannot be converted to ActionListener by method invocation conversion
    3 errors

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: Why am I getting errors on this code?

    Not fully sure on that Error message to be honest. try this:
    change:
    Java Code:
    public void actionPerformed(ActionEvent e){
    b.setText("Clicked.");
    }
    to:
    Java Code:
    public void actionPerformed(ActionEvent e){
    if(e.getSource() == b) {
    b.setText("Clicked.");
    }
    }
    Not sure if this will work, have never seen that error before so just throwing sensible ideas here.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,771
    Rep Power
    5

    Default Re: Why am I getting errors on this code?

    Try adding this import statement
    Java Code:
    import java.awt.event.*;
    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Similar Threads

  1. Why am I getting errors on this code?
    By MW130 in forum New To Java
    Replies: 11
    Last Post: 03-14-2013, 01:58 PM
  2. Errors in code
    By MikeJ39 in forum New To Java
    Replies: 0
    Last Post: 10-23-2011, 10:57 PM
  3. Help with errors in code
    By XodoX in forum New To Java
    Replies: 10
    Last Post: 02-08-2011, 05:14 AM
  4. nasty errors in your code
    By j2me64 in forum Forum Lobby
    Replies: 0
    Last Post: 01-27-2011, 04:18 PM
  5. Errors in the code (?)
    By AndersBjörnör in forum New To Java
    Replies: 1
    Last Post: 10-09-2009, 03:06 PM

Posting Permissions

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