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