method not passing across actionlistener interface
I need to make a basic GUI with three buttons that changes the background to either red, blue, or green. I have the interface set up just fine, but I cannot get the actionlistener code to work. The error is specifically on the ColorField.setColor in the three listener classes: "ColorField cannot be resolved".
import javax.swing.*;
class ColorSelector extends JFrame
{
public ColorSelector()
{ setTitle("My Empty Frame");
setSize(300,200); // default size is 0,0
setLocation(10,200); // default is 0,0 (top left corner)
}
}
import java.awt.event.ActionListener;
import javax.swing.*;
public class ColorSelectorTester {
public static void main(String[] args) {
JFrame frame = new ColorSelector();
frame.show();
JButton Rbutton = new JButton("Red");
JButton Gbutton = new JButton("Green");
JButton Bbutton = new JButton("Blue");
JLabel ColorField = new JLabel();
ColorField.setOpaque(true);
JPanel panel = new JPanel();
panel.add(ColorField);
panel.add(Rbutton);
panel.add(Gbutton);
panel.add(Bbutton);
frame.add(panel);
ActionListener Rlistener = new RButtonListener();
Rbutton.addActionListener(Rlistener);
ActionListener Glistener = new GButtonListener();
Rbutton.addActionListener(Glistener);
ActionListener Blistener = new BButtonListener();
Rbutton.addActionListener(Blistener);
}
}
import java.awt.Color;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
public class RButtonListener implements ActionListener{
public void actionPerformed(ActionEvent Rbutton)
{ ColorField.setColor(Color.red);
}
}
Re: method not passing across actionlistener interface
You have declared ColorField inside the main method of the ColorSelectorTester class and then trying to access it from the RButtonListener class. You cannot do this. You can only access a variable within the scope it is declared in. You will need to rethink your design.
Re: method not passing across actionlistener interface
Yeah I can, because it's going through an interface, I just dont know how to declare it the RIGHT way!
Re: method not passing across actionlistener interface
err no you can't. Unless you are using a different version of Java to everyone else on the planet.
Re: method not passing across actionlistener interface
Then how should I be doing it? Because I am completely stumped.
Re: method not passing across actionlistener interface
For starters I would move most of the code from your main method into the GUI class. Then you can make your Listener classes inner classes of the GUI class. This will allow them to have access to instance variables.
Re: method not passing across actionlistener interface
Thread moved to the Swing forum. And for future reference, please wrap your code in the [highlight=java][/highlight] tags
Re: method not passing across actionlistener interface
Wrap my code in tags? Huh?
Re: method not passing across actionlistener interface
Quote:
Originally Posted by
flamewolf393
Wrap my code in tags? Huh?
Yes...when done so code is actually comprehensible to a human being. Typing:
[highlight=java]your code here[/highlight]
Results in
With spacing retained, and highlights to boot
Re: method not passing across actionlistener interface
Quote:
Originally Posted by
flamewolf393
Yeah I can, because it's going through an interface, I just dont know how to declare it the RIGHT way!
You can not. If it's coming from the interface, that interface is not implemented by RButtonListener, is it?