Professional Documents
Culture Documents
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;
/*otras pruebas*/
private Color color = Color.LIGHT_GRAY;
private JPanel coloresJPanel;
/*componentes */ /*declaracion*/
private JRadioButton jrbRadio1, jrbRadio2, jrbRadio3, jrbRadio4;
private ButtonGroup bgRadios;
/*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);
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*/
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);
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);
}
}
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 :