Simple Java Problem - Please Help!


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: Simple Java Problem - Please Help!

  1. #1
    Chris Ransley Guest

    Simple Java Problem - Please Help!


    hi

    I'm writing a program for a university project, and have hit a problem which
    i can't seem to overcome. I have two class files, one which draws a button
    (PhoneButton.class) and one which invokes the button (Phone.class). I want
    to add a sound when the button is pressed, but I keep getting this error
    when I compile it...

    D:\java\Phone.java:76: cannot resolve symbol
    symbol : variable myButton1
    location: class Phone
    if (event.getSource() == myButton1)

    could anyone tell me what changes need to be made? please?!

    cheers in advance

    >>>>>>>


    import java.awt.*;
    import java.applet.Applet;
    import java.awt.event.*;

    public class Phone extends Applet implements ActionListener {

    private Image image;
    private AudioClip sound1;

    public void init() {

    setLayout(null);
    image = getImage(getDocumentBase(), "phone.jpg");
    sound1 = getAudioClip(getDocumentBase(), "sound.wav");

    PhoneButton myButton1;
    myButton1 = new PhoneButton(322,180, 1);
    add(myButton1);
    myButton1.addActionListener(this);

    }

    public void paint (Graphics g) {
    g.drawImage(image, 0, 0, 700, 530, this);
    super.paint(g);

    }

    public void actionPerformed(ActionEvent event)
    {
    if (event.getSource() == myButton1)
    sound1.play();

    }

    }

  2. #2
    Kent Guest

    Re: Simple Java Problem - Please Help!


    Chris,

    See my comments below.

    Regards,
    Kent

    "Chris Ransley" <morgs_2k@yahoo.co.uk> wrote:
    >
    >hi
    >
    >I'm writing a program for a university project, and have hit a problem which
    >i can't seem to overcome. I have two class files, one which draws a button
    >(PhoneButton.class) and one which invokes the button (Phone.class). I want
    >to add a sound when the button is pressed, but I keep getting this error
    >when I compile it...
    >
    >D:\java\Phone.java:76: cannot resolve symbol
    >symbol : variable myButton1
    >location: class Phone
    >if (event.getSource() == myButton1)
    >
    >could anyone tell me what changes need to be made? please?!
    >
    >cheers in advance
    >
    >>>>>>>>

    >
    >import java.awt.*;
    >import java.applet.Applet;
    >import java.awt.event.*;
    >
    >public class Phone extends Applet implements ActionListener {
    >
    >private Image image;
    >private AudioClip sound1;


    PhoneButton myButton1;
    //declare myButton1 here (see below).

    >
    >public void init() {
    >
    >setLayout(null);
    >image = getImage(getDocumentBase(), "phone.jpg");
    >sound1 = getAudioClip(getDocumentBase(), "sound.wav");
    >
    >PhoneButton myButton1;


    //declaring myButton1 here means its scope is inside the init() method.

    >myButton1 = new PhoneButton(322,180, 1);
    >add(myButton1);
    >myButton1.addActionListener(this);
    >
    >}
    >
    >public void paint (Graphics g) {
    >g.drawImage(image, 0, 0, 700, 530, this);
    >super.paint(g);
    >
    >}
    >
    >public void actionPerformed(ActionEvent event)
    >{
    >if (event.getSource() == myButton1)


    //because myButton1 is now scoped to the class this method can access it

    >sound1.play();
    >
    >}
    >
    >}



  3. #3
    Soft Solution System Limited Guest

    Re: Simple Java Problem - Please Help!

    try declare your myButton1 outside init()


    "Chris Ransley" <morgs_2k@yahoo.co.uk> gl
    news:3cd4d307@10.1.10.29...
    >
    > hi
    >
    > I'm writing a program for a university project, and have hit a problem

    which
    > i can't seem to overcome. I have two class files, one which draws a

    button
    > (PhoneButton.class) and one which invokes the button (Phone.class). I

    want
    > to add a sound when the button is pressed, but I keep getting this error
    > when I compile it...
    >
    > D:\java\Phone.java:76: cannot resolve symbol
    > symbol : variable myButton1
    > location: class Phone
    > if (event.getSource() == myButton1)
    >
    > could anyone tell me what changes need to be made? please?!
    >
    > cheers in advance
    >
    > >>>>>>>

    >
    > import java.awt.*;
    > import java.applet.Applet;
    > import java.awt.event.*;
    >
    > public class Phone extends Applet implements ActionListener {
    >
    > private Image image;
    > private AudioClip sound1;
    >
    > public void init() {
    >
    > setLayout(null);
    > image = getImage(getDocumentBase(), "phone.jpg");
    > sound1 = getAudioClip(getDocumentBase(), "sound.wav");
    >
    > PhoneButton myButton1;
    > myButton1 = new PhoneButton(322,180, 1);
    > add(myButton1);
    > myButton1.addActionListener(this);
    >
    > }
    >
    > public void paint (Graphics g) {
    > g.drawImage(image, 0, 0, 700, 530, this);
    > super.paint(g);
    >
    > }
    >
    > public void actionPerformed(ActionEvent event)
    > {
    > if (event.getSource() == myButton1)
    > sound1.play();
    >
    > }
    >
    > }




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center