Java, "Variable name" cannot be resolved to a variable error

Viewed 20

I got an error while running the code. What i want to make is to change the background of the panel depending on the chosen button.

I use Eclipse in running this Code:

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

     public class AwtButtons extends Frame implements ActionListener{
    
      Button b1,b2;
      AwtButtons(){ 
        
        Frame f = new Frame("AWT Buttons");
        Panel p = new Panel();
        b1 = new Button("Blue");
        b1.setBounds(150,150,150,30);
        
        b2 = new Button("Red");
        b2.setBounds(150,200,150,30);
        
        
        b1.addActionListener(this);
        b2.addActionListener(this);
        
        
        f.add(b1);
        f.add(b2);
            
        setSize(450,400);
        setLayout(null);
        setVisible(true);
    
      }
            public void actionPerformed (ActionEvent e) {
                if (e.getSource()==b1) {
                p.setBackground(Color.BLUE);
                }
                if (e.getSource()==b2) {
                p.setBackground(Color.RED);
                }
            
        
     }
        public static void main(String[] args) {
        new AwtButtons();

     }

     }

Im getting an error on the

 p.setBackground(Color.BLUE);

What causes this error?

0 Answers
Related