Simple Java Problem - Please Help!

Using the Java SDK with TextPad

Moderators: AmigoJack, bbadmin, helios, Bob Hansen, MudGuard

Post Reply
Chris Ransley

Simple Java Problem - Please Help!

Post by Chris Ransley »

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();

}

}
Russell

Re: Simple Java Problem - Please Help!

Post by Russell »

try,

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; //this button should be globally visible
//basically this sets aside the required
//memory for a PhoneButton but doesnt
//initialize the object yet.

public void init() {

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

//if you define the button here it is not globally available
myButton1 = new PhoneButton(322,180, 1); //now initialize the object with
//these values
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();

}

}
Post Reply