-
Variable problem!!!
Hi everyone, just want to know why the actionPerformed method canīt acces the yellowButton, blueButton and redButton variables... the compiler shows an error: "cannot resolve symbol variable yellowButton"... hereīs the code, thanks for your help!
-----------------------------------------------------------------------
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class ButtonPanel extends JPanel implements ActionListener
{
public Color backgroundColor;
public ButtonPanel()
{
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
add(yellowButton);
add(blueButton);
add(redButton);
yellowButton.addActionListener(this);
blueButton.addActionListener(this);
redButton.addActionListener(this);
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == yellowButton) { backgroundColor = Color.yellow; }
if (event.getSource() == blueButton) { backgroundColor = Color.blue; }
if (event.getSource() == redButton) { backgroundColor = Color.red; }
setBackground(backgroundColor);
}
}
-
You declared them inside the constructor... don't do that.
-
to elaborate on Drains answer, the problem is that the button variables you declared are out of scope. The scope of a variable defines where it can be accessed (well more precisely where it exists). When you write the code the scope is defined by the curly brackets
Code:
//start of scope
{
//some code
}
//end of scope
Anything defined inside that area of scope can't be directly accessed outside that area of scope. As Drain said you defined the button variables inside the constructor and the constructor has it's own scope. Anything defined in the constructor ceases to exist as soon as the end of the constructor is reached. To make the buttons available inside the actionPerformed method declare the buttons at the class level and make them class variables.
-
Yep... youīre right. I donīt know what was wrong with me yesterday that couldnīt figure it out... thanks a lot guys!! Cheers...
-
additionally, it would be of greater help to you if you declared a separate listener for each of your buttons.. that way you dont have to write a big, ugly IF statement to find out which button was pressed. here is an example:
Code:
/*
* JFrame.java
*
* Created on 30 June 2004, 18:33
*/
/**
*
* @author admin
*/
public class JFrame extends javax.swing.JFrame {
/** Creates new form JFrame */
public JFrame() {
initComponents();
}
/** This method is called from within the constructor to
* initialize the form.
* WARNING: Do NOT modify this code. The content of this method is
* always regenerated by the Form Editor.
*/
private void initComponents() {
yellowButton = new javax.swing.JButton();
redButton = new javax.swing.JButton();
blueButton = new javax.swing.JButton();
addWindowListener(new java.awt.event.WindowAdapter() {
public void windowClosing(java.awt.event.WindowEvent evt) {
exitForm(evt);
}
});
yellowButton.setText("yellow");
yellowButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
yellowButtonActionPerformed(evt);
}
});
getContentPane().add(yellowButton, java.awt.BorderLayout.CENTER);
redButton.setText("red");
redButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
redButtonActionPerformed(evt);
}
});
getContentPane().add(redButton, java.awt.BorderLayout.NORTH);
blueButton.setText("blue");
blueButton.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
blueButtonActionPerformed(evt);
}
});
getContentPane().add(blueButton, java.awt.BorderLayout.WEST);
pack();
}
private void blueButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here for the blue button
}
private void redButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here for the red button
}
private void yellowButtonActionPerformed(java.awt.event.ActionEvent evt) {
// Add your handling code here for the yellow button:
}
/** Exit the Application */
private void exitForm(java.awt.event.WindowEvent evt) {
System.exit(0);
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
new JFrame().show();
}
// Variables declaration - do not modify
private javax.swing.JButton blueButton;
private javax.swing.JButton redButton;
private javax.swing.JButton yellowButton;
// End of variables declaration
}
NetBeans made this for me, which explains the arcane comments about do not moify, and FORM EDITOR.. but if you see the way this works, it is fantastic.. Each button has its own tiny handler, that merely calls a method in the main class...
this categorises everything and makes it very neat, plus its more obvious and easy to see what you do and dont have in scope, as it is just like any other method...
i would recommend making your guis in this fashion
-
Thanks cjard!
Yeah i know, i was doing it that way because i was testing something... thanks for your time writting the example... hereīs another one for you 
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ButtonTest
{
public static void main(String[] args)
{
ButtonFrame frame = new ButtonFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ButtonFrame extends JFrame
{
public ButtonFrame()
{
setTitle("ButtonTest");
setSize(DEFAULT_WIDTH, DEFAULT_HEIGHT);
// add panel to frame
ButtonPanel panel = new ButtonPanel();
Container contentPane = getContentPane();
contentPane.add(panel);
}
public static final int DEFAULT_WIDTH = 300;
public static final int DEFAULT_HEIGHT = 200;
}
class ButtonPanel extends JPanel
{
public ButtonPanel()
{
// create buttons
JButton yellowButton = new JButton("Yellow");
JButton blueButton = new JButton("Blue");
JButton redButton = new JButton("Red");
// add buttons to panel
add(yellowButton);
add(blueButton);
add(redButton);
ColorAction yellowAction = new ColorAction(Color.YELLOW);
ColorAction blueAction = new ColorAction(Color.BLUE);
ColorAction redAction = new ColorAction(Color.RED);
yellowButton.addActionListener(yellowAction);
blueButton.addActionListener(blueAction);
redButton.addActionListener(redAction);
}
private class ColorAction implements ActionListener
{
public ColorAction(Color c)
{
backgroundColor = c;
}
public void actionPerformed(ActionEvent event)
{
setBackground(backgroundColor);
}
private Color backgroundColor;
}
}
-
i agree, this ios possible, but the code generated by netbeans is clearer in the sense that most people think of classes as separate entities.. whereas a button usually affects a frame in some way, so it should have access to the properties of the frame..
by making all the handling code happen in a method of the frame, it is obvious what is and is not accessible, without having to remember the rules of how inner and sibling classes may interact, through the access modifiers..
-
Jejejejeje yeah.... the inner class here is needed to acces the ButtonPanel objects...
-
well, if you'd bothered making a buttonpanel a proper class in its own java file (like you ought to), then my proscribed method would work because what you wrote as ButtonPanel, and I wrote a JFrame (the default name provided by netbeans; there are some things i cant be bothered setting) would have been equivalent
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks