You are on page 1of 5

/*CLASE VENTANA */

package com.tarea.change.window.background;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.ButtonGroup;
import javax.swing.JFrame;
import javax.swing.JRadioButton;
/*para cambiar el color usamos para el JFrame JColorChooser*/

/*otras pruebas */
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;

public class Ventana extends JFrame {


/*atributos*/
private String titulo;
private int ancho;
private int alto;

/*otras pruebas*/
private Color color = Color.LIGHT_GRAY;
private JPanel coloresJPanel;

/*componentes */ /*declaracion*/
private JRadioButton jrbRadio1, jrbRadio2, jrbRadio3, jrbRadio4;
private ButtonGroup bgRadios;

public Ventana(String titulo, int ancho, int alto){ //constructor


this.titulo = titulo;
this.ancho = ancho;
this.alto = alto;

/*metodos*/
public void inicializarVentana(){

this.setTitle(titulo);
this.setSize(ancho, alto);
//this.setBackground(color);

/*centramos la ventana*/
this.setLocationRelativeTo(null);
/*habilitamos el boton de cerrar para que termine le ejecucion del programa*/
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
bgRadios = new ButtonGroup();

/*componentes */ /*inicializacion*/
jrbRadio1 = new JRadioButton("ROJO");
jrbRadio2 = new JRadioButton("VERDE");
jrbRadio3 = new JRadioButton("AZUL");
jrbRadio4 = new JRadioButton("AMARILLO");

/*otras pruebas*/
//crea objetco JPanel para mostrar el color
coloresJPanel = new JPanel();
coloresJPanel.setPreferredSize(new Dimension(600, 600));
coloresJPanel.setBackground(color);

/*nota reordar que los JRadioButtons no se agregan asi -> this.add(this.jrbRadio1);


sino con un ButtonGroup atributo de clase para que solo se puede elegir un solo jrRadio*/
bgRadios.add(jrbRadio1);
bgRadios.add(jrbRadio2);
bgRadios.add(jrbRadio3);
bgRadios.add(jrbRadio4);

this.setLayout(new FlowLayout()); /*MUCHO OJO CON ESTE SETTER porque sin el NO APARECEN LOS jrbRadioButton*/
/*preguntarle al profe que hace esta linea llevaba 25 minutos sin saber que pasaba*/

/*componentes */ /*los agrego a la ventana*/


this.add(jrbRadio1);
this.add(jrbRadio2);
this.add(jrbRadio3);
this.add(jrbRadio4);

/*agrega coloresJPanel al JFrame*/


this.add( coloresJPanel, BorderLayout.CENTER);

/*********EN ESTA SECCION PROGRAMAMOS LOS EVENTOS ********/


/*recordar : ActionListener para los radio buttons / ItemListener para los checkbox */

ActionListener eventoRadioButton = new ActionListener() {


@Override
public void actionPerformed(ActionEvent e) {

/*declaramos un objeto AbastractButton para manipular los JRadioButtons*/


AbstractButton aRadioButton = (AbstractButton) e.getSource(); //con AbstractButton puedo manipular todos los tipos de
botones
System.out.println(" Seleccionado: " + aRadioButton.getText());

String opcion = aRadioButton.getText();

switch(opcion){
case "ROJO":{
cambiarColor("rojo");

}break;
case "VERDE":{
cambiarColor("verde");
}break;
case "AZUL": {
cambiarColor("azul");
}break;
case "AMARILLO":{
cambiarColor("amarillo");
}break;

}
}; //termina el evento

jrbRadio1.addActionListener(eventoRadioButton);
jrbRadio2.addActionListener(eventoRadioButton);
jrbRadio3.addActionListener(eventoRadioButton);
jrbRadio4.addActionListener(eventoRadioButton);

//this.pack();
/*hasta el final haemos visible la ventana*/

this.setVisible(true);

} //fin del metodo inicializarVentana();

public void cambiarColor(String chooseColor){

/*SI ESTOY ENTRANDO AL METODO PERO NO SETTEA EL COLOR */


System.out.println("rastreador entre!!");

if(chooseColor.equalsIgnoreCase("ROJO")){
color = Color.red;
coloresJPanel.setBackground(color);
}else if(chooseColor.equalsIgnoreCase("VERDE")){
color = Color.green;
coloresJPanel.setBackground(color);
}else if(chooseColor.equalsIgnoreCase("AZUL")){
color = Color.blue;
coloresJPanel.setBackground(color);
}else if(chooseColor.equalsIgnoreCase("AMARILLO")){
color = Color.yellow;
coloresJPanel.setBackground(color);
}
}

}//termina la clase Ventana


/*CLASE MAIN */

package com.tarea.change.window.background;

import java.awt.Color;

/**
*
* @author developer_ubuntu
*/
public class Main {
public static void main(String args[]){

Ventana mainW = new Ventana("Change Backgrounds with RadioButton Component", 600, 600);
mainW.inicializarVentana();

OBSERVACIONES :

En este proyecto usamos un evento de tipo ActionListener para


aplicar cambio de color sobre un Panel embebido, el cual se cambia
con el metodo setter setBackground(), dicho metodo recibe como
parametro una variable de tipo Color. Esto por medio de selectores de
tipo JRadioButton() los cuales, me parecen sumamente utiles usados
con sus respectivos eventos ya que se me ocurren un sin fin de
cosas interesantes que podemos hacer con este potente par (eventos
y radioButtons) por ejemplo, un examen interactivo de opcion
multiple.

Notas vistas en clase, me quedo claro que para anexarle eventos a


JRadioButton debemos usar ActionListener.

You might also like