Why does not the window change color?

0

The following program tries to change the color of the window by pressing a button, whose name is the color to which the window will be changed. The code does not throw an error but it does not change the color of the panel. The idea is that you do not have to nest the ColorClass class to any other.

package graficos;
import javax.swing.*;

import java.awt.Color;
import java.awt.event.*;
public class PruebaEventos {

public static void main(String[] args) {
    // TODO Auto-generated method stub
        MarcoBotones ventana =new MarcoBotones();
        ventana.setVisible(true);

        ventana.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}

}
class MarcoBotones extends JFrame{
public MarcoBotones(){
setTitle("Botones y eventos");
setBounds(550,150,500,500);
LaminaBotones milamina=new LaminaBotones();
add(milamina);




}


}
 class LaminaBotones extends JPanel {

 JButton botonAzul= new JButton("azul");    
 JButton botonAmarillo=new JButton("amarillo");
 JButton botonRojo= new JButton("rojo");
  public LaminaBotones(){
    add(botonAzul);
    add(botonAmarillo);
    add(botonRojo);
    ColorFondo Amarillo=new ColorFondo(Color.YELLOW);
    ColorFondo Azul=new ColorFondo(Color.blue);
    ColorFondo Rojo=new ColorFondo(Color.red);
    botonAzul.addActionListener(Azul);
    botonAmarillo.addActionListener(Amarillo);
    botonRojo.addActionListener(Rojo);

}


}   

class ColorFondo extends JPanel  implements ActionListener{

    public ColorFondo(Color c ){

        colorDeFondo=c;

    }

    public void actionPerformed(ActionEvent e){


      setBackground(colorDeFondo);
      revalidate();
      System.out.println("apreto el boton");

    }
    private Color colorDeFondo;


}
    
asked by Julio enrique godoy muñoz 19.06.2017 в 10:10
source

1 answer

1
class ColorFondo extends JPanel  implements ActionListener{
   ...
   public void actionPerformed(ActionEvent e){
      setBackground(colorDeFondo);

What your code says:

  • ColorFondo is a JPanel . But it's never added as a component, so it's not visible in your window.
  • When the method is executed, change the color of the instance itself . That is, change the color of a component that is not being drawn .

The code looks like a mixture of two techniques:

  • Make JPanel implement a listener. But in that case it would be in the class LaminaBotones , and the method actionPerformed would be added to that class.

    In that case, depending on which button the action was pressed is different, in your actionPerformed you should check in the ActionEvent the origin of the event to know what to do (what color to put in the background).

  • Make the listener have a reference to the object that you want to manipulate, either because it is passed to you or because it is an internal class. Basically, your code is the first thing to do.

    class ColorFondo implements ActionListener{
       private Color colorDeFondo;
       private JPanel panel
       public ColorFondo(Color c, JPanel panel){
         this.colorDeFondo=c;
         this.panel=panel;
       }
    
       public void actionPerformed(ActionEvent e){
          panel.setBackground(colorDeFondo);
          panel.revalidate(); // No estoy seguro de que esto sea necesario,
                              // pero hace tiempo que no toco GUI.
          System.out.println("apreto el boton");
       }
     }
    

    and

    ColorFondo Amarillo=new ColorFondo(Color.YELLOW, this); // Esto iría mejor en un método init que en el Constructor, leer sobre *instance leaking*. 
                                                            // Pero para este ejemplo ya va bien.
    
answered by 19.06.2017 / 10:37
source