public class BotonesOpcion extends JFrame implements ActionListener {
private JPanel contentPane ;
private ButtonGroup grupoDeBotones ;
private JRadioButton rbRojo ;
private JRadioButton rbVerde ;
private JRadioButton rbAzul ;
private JLabel etTitulo ;
JPanel panelDeBotones;
/**
* Launch the application.
*/
public static void main(String[] args) {
BotonesOpcion frame = new BotonesOpcion();
frame.setTitle( "Ejemplo con Botones de Opcion" );
frame.setVisible( true);
}
/**
* Create the frame.
*/
public BotonesOpcion() {
setDefaultCloseOperation(JFrame. EXIT_ON_CLOSE);
setBounds(100, 100, 450, 300);
contentPane = new JPanel();
setContentPane( contentPane);
contentPane.setLayout(null);
panelDeBotones = new JPanel();
panelDeBotones.setBounds(20, 36, 390, 61);
panelDeBotones.setBorder(new TitledBorder(null, "Panel de Botones", TitledBorder.LEADING , TitledBorder.TOP, null, null ));
contentPane.add(panelDeBotones );
panelDeBotones.setLayout(new FlowLayout(FlowLayout.CENTER, 5, 5));
etTitulo = new JLabel("Ejemplo con RadioButtons" );
etTitulo.setBounds(10, 11, 150, 14);
contentPane.add(etTitulo );
grupoDeBotones = new ButtonGroup();
rbRojo = new JRadioButton();
rbVerde = new JRadioButton();
rbAzul= new JRadioButton();
rbRojo.setText( "Rojo");
rbRojo.addActionListener( this);
grupoDeBotones.add(rbRojo );
rbAzul.setText( "Azul");
grupoDeBotones.add(rbAzul );
rbAzul.addActionListener( this);
rbVerde.setText( "Verde");
rbVerde.addActionListener( this);
grupoDeBotones.add(rbVerde );
panelDeBotones.add(rbAzul );
panelDeBotones.add(rbRojo );
panelDeBotones.add(rbVerde );
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if (e.getSource()==rbAzul )
{
contentPane.setBackground(Color.BLUE);
}
if (e.getSource()==rbVerde )
{
contentPane.setBackground(Color.GREEN);
}
if (e.getSource()==rbRojo ){
contentPane.setBackground(Color.red);
}
}
}
No hay comentarios.:
Publicar un comentario