0% found this document useful (0 votes)
13 views

Componentes GUI JRadioButton

This document defines a class called BotonOpcion that illustrates the use of JRadioButton components in Java. The class creates a frame with three radio buttons for selecting colors (blue, red, green). It also includes a label that will change colors based on the selected radio button. An event handler class is defined to respond to item selection events from the radio buttons and update the label color accordingly. The main method creates an instance of the BotonOpcion class to display the radio button interface.

Uploaded by

Héctor Nossa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Componentes GUI JRadioButton

This document defines a class called BotonOpcion that illustrates the use of JRadioButton components in Java. The class creates a frame with three radio buttons for selecting colors (blue, red, green). It also includes a label that will change colors based on the selected radio button. An event handler class is defined to respond to item selection events from the radio buttons and update the label color accordingly. The main method creates an instance of the BotonOpcion class to display the radio button interface.

Uploaded by

Héctor Nossa
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EJERCICIO ILUSTRATIVO DEL USO DEL COMPONENTE JRadioButton

Definicin de la clase BotonOpcion

package interfaz9;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class BotonOpcion extends JFrame {


private JLabel etq1;
private JRadioButton azul, rojo, verde;
private ButtonGroup grupoBotonesOpcion;
public BotonOpcion()
{
super( "Prueba Botn Opcin - Radio Button");
Container contenedor = getContentPane();
GridLayout esquema = new GridLayout(2,3,10,10);
contenedor.setLayout( esquema );
etq1 = new JLabel ( "Texto");
etq1.setHorizontalAlignment(SwingConstants.CENTER);
etq1.setOpaque(true);
etq1.setBackground(Color.BLUE);
etq1.setForeground(Color.WHITE);
etq1.setPreferredSize(new Dimension(300, 30));
contenedor.add( etq1 );
azul = new JRadioButton ("Azul",true);
contenedor.add( azul );
rojo = new JRadioButton ("Rojo",false);
contenedor.add( rojo );
verde = new JRadioButton ("Verde",false);
contenedor.add( verde );
grupoBotonesOpcion = new ButtonGroup();

grupoBotonesOpcion.add(azul);
grupoBotonesOpcion.add(rojo);
grupoBotonesOpcion.add(verde);
ManejadorBotonOpcion manejador = new ManejadorBotonOpcion();
azul.addItemListener( manejador );
rojo.addItemListener( manejador );
verde.addItemListener( manejador );
setSize(400,300);
setVisible( true );
}
// Clase interna privada para manejar eventos tipo ItemEvent
private class ManejadorBotonOpcion
implements ItemListener { // Inicio ManejadorBotonOpcion
public void itemStateChanged ( ItemEvent evento) {
if (evento.getSource() == azul)
etq1.setBackground(Color.BLUE);
if (evento.getSource() == rojo)
etq1.setBackground(Color.RED);
if (evento.getSource() == verde)
etq1.setBackground(Color.GREEN);
}
} // Fin ManejadorBotonOpcion

}
Definicin de la clase BotonOpcion
package interfaz9;
import javax.swing.JFrame;
public class Interfaz9 {
public static void main(String[] args) {
BotonOpcion a = new BotonOpcion();
a.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}

You might also like