Sponsored by:

Java help question. Cannot find symbol Error?

Java help question. Cannot find symbol Error?

Postby larry » Fri Nov 25, 2011 4:12 am

I'm getting this error when trying to compile: File: C:\Users\Jake\Java\defaultFrameSubClass.… [line: 46]
Error: cannot find symbol
symbol : variable clickedButton
location: class defaultFrameSubClass

I am completely new to java, am taking a class on it and I can't figure out what the problem is.

Here is the code: import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

class defaultFrameSubClass extends JFrame implements ActionListener {

private static final int FRAME_WIDTH = 300;
private static final int FRAME_HEIGHT = 200;
private static final int FRAME_X_ORIGIN = 150;
private static final int FRAME_Y_ORIGIN = 250;

private static final int BUTTON_WIDTH = 80;
private static final int BUTTON_HEIGHT = 30;

private JButton cancelButton;
private JButton okButton;

public defaultFrameSubClass() {
Container contentPane = getContentPane();

setTitle ("My First Subclass");
setSize (FRAME_WIDTH,FRAME_HEIGHT);
setLocation (FRAME_X_ORIGIN, FRAME_Y_ORIGIN);

//set the layout manager
contentPane.setLayout(new FlowLayout());

//create and place two new buttons on the frame's content pane
okButton = new JButton("OK");
okButton.setSize(BUTTON_WIDTH,BUTTON_HEI…
contentPane.add(okButton);

cancelButton = new JButton ("CANCEL");
cancelButton.setSize(BUTTON_WIDTH,BUTTON…
contentPane.add(cancelButton);

//register this frame as an action listener of the two buttons
cancelButton.addActionListener(this);
okButton.addActionListener(this);

setDefaultCloseOperation(EXIT_ON_CLOSE);
}//end constructor

public void actionPerformed(ActionEvent event){
JButton clickedButon = (JButton) event.getSource();
String buttonText = clickedButton.getText();
setTitle("You clicked " + buttonText);
}//end method actionPerformed
}//end class




This "assignment" I am just copying stuff out of the book, but I am not getting the same results! There is this error that I'm getting now, and I've also gotten an error about this line as well :

class defaultFrameSubClass extends JFrame implements ActionListener {

/ In that error it said something about not being able to override something :O. Guess I should have saved the error.
larry
 
Posts: 861
Joined: Thu Mar 31, 2011 7:10 am
Top

Java help question. Cannot find symbol Error?

Postby anson34 » Fri Nov 25, 2011 4:14 am

Also: the following lines are redundant:

import java.awt.*;
import java.awt.event.*;

The asterik represents all files in the java.awt file; therefor, you do not need the import java.awt.event.*; line.
anson34
 
Posts: 799
Joined: Thu Mar 31, 2011 6:53 pm
Top

Java help question. Cannot find symbol Error?

Postby matyas » Fri Nov 25, 2011 4:19 am

You left one of the ts out off clickedButton at the declaration.

JButton clickedButon = (JButton) event.getSource();
String buttonText = clickedButton.getText();
matyas
 
Posts: 810
Joined: Thu Mar 31, 2011 6:15 am
Top


Return to Class Action

 


  • Related topics
    Replies
    Views
    Last post

Who is online

Users browsing this forum: crosbey, gustav, jorel, osbourne, regenweald, spengler and 0 guests