Results 1 to 7 of 7
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Help!?

  1. #1
    Noobi is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Unhappy Help!?

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    import java.util.*;
    
    public class TicTacToe extends JFrame implements ActionListener
    {
        private JButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
        private Container pane;
        
        public TicTacToe()
        {
            setSize(500, 500);
            setTitle("Play TicTacToe!");
            setLocation(150, 150);
            setDefaultCloseOperation(EXIT_ON_CLOSE);
            pane = this.getContentPane();
            pane.setLayout(new GridLayout(3, 3));
            setUpButtons();      
        }
    
    ...
    
    public void actionPerformed (ActionEvent e)
        {
            int x;
            int count = 0;
            for (x=1; x<10; x++)
            {
                if (count % 2 == 0)
                    (this).setText("x");  
                else
                   (this).setText("o");  
                count++;
            }
        }
    I have been getting the same error for more than an hour. cannot find symbol - method setText(java.lang.String)... Please help. Thanks.
    Last edited by pbrockway2; 07-01-2012 at 05:21 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help!?

    Hi Noobi, welcome to the forums!

    I've edited your code to put in the "code" tags which will make it more readable. They are discussed in this faq: if you have trouble using the tags ask and someone will explain.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,892
    Rep Power
    25

    Default Re: Help!?

    Where is the method: setText() defined? The compiler can not find its definition.
    Have you read the API doc for the class you are using?
    http://docs.oracle.com/javase/6/docs/api/index.html

    What are you wanting the method to do?
    If you don't understand my response, don't ignore it, ask a question.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,585
    Rep Power
    12

    Default Re: Help!?

    cannot find symbol - method setText(java.lang.String)
    I am guessing that the full error message refers to this line:

    Java Code:
    (this).setText("x");
    "cannot find symbol" means the compiler hs looked for a definition of the method setText() taking a String argument and cannot find one. I'm not sure what you were trying to do with the "(this)" part, but this refers to the instance of TicTacToe whose actionPerformed() is being called. Evidently TicTacToe does not contain a setText() method anywhere. Moreover the compiler knows that a TicTacToe instance is a JFrame instance as well: the only trouble is that JFrame doesn't have a setText() method!

    So... You will need to think about what setText() method you meant to call. Note that the ActionEvent class (and its superclasses) givesyou some useful methods that you can call with the e variable.

  5. #5
    Noobi is offline Member
    Join Date
    Jul 2012
    Posts
    2
    Rep Power
    0

    Default Re: Help!?

    Quote Originally Posted by pbrockway2 View Post
    I am guessing that the full error message refers to this line:

    Java Code:
    (this).setText("x");
    "cannot find symbol" means the compiler hs looked for a definition of the method setText() taking a String argument and cannot find one. I'm not sure what you were trying to do with the "(this)" part, but this refers to the instance of TicTacToe whose actionPerformed() is being called. Evidently TicTacToe does not contain a setText() method anywhere. Moreover the compiler knows that a TicTacToe instance is a JFrame instance as well: the only trouble is that JFrame doesn't have a setText() method!

    So... You will need to think about what setText() method you meant to call. Note that the ActionEvent class (and its superclasses) givesyou some useful methods that you can call with the e variable.
    All I want to do is change the text of a JButton when it is clicked. If "this" won't refer to a JButton, how should I account for multiple JButtons? I thought that I'd be able to inherit the setText() method of an AbstractButton object from the import statements I used. Do I have to write my own method? Thanks for your help!
    -Vi

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default Re: Help!?

    You get a reference to the JButton through the ActionEvent object, e, passed into the actionPerformed method. Calling getSource() on it will return a reference to the object that initiated the event, here the JButton. You will have to cast it to JButton (another reason not to use one ActionListener on multiple different diverse components).
    Noobi likes this.

  7. #7
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,458
    Rep Power
    20

    Default Re: Help!?

    Please go through the Forum Rules -- especially the third paragraph.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Posting Permissions

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