| Referencia: podemos conocer lo qué es un applet de Java en un artículo de DesarrolloWeb.com. También hay disponible un manual de Java que estamos publicando y en el que trataremos los Applets en algún momento. |
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Colores extends JFrame
{
JButton cmdRojo, cmdVerde, cmdAzul, cmdAmarillo,cmdRosa,
cmdNegro, cmdBlanco, cmdCyan, cmdGris, cmdVioleta, cmdNaranja,
cmdGrisO, cmdGrisC;
public Colores()
{
setSize(500,200);
setTitle("Colores Varo");
getContentPane().setLayout(new FlowLayout());
cmdRojo=new JButton("Rojo");
cmdVerde=new JButton("Verde");
cmdAzul=new JButton("Azul");
cmdAmarillo=new JButton("Amarillo");
cmdRosa=new JButton("Rosa");
cmdNegro=new JButton("Negro");
cmdBlanco=new JButton("Blanco");
cmdCyan=new JButton("Azul Claro");
cmdGris=new JButton("Gris");
cmdVioleta=new JButton("Violeta");
cmdNaranja=new JButton("Naranja");
cmdGrisO=new JButton("Gris Oscuro");
cmdGrisC=new JButton("Gris Claro");
cmdRojo.addActionListener(new ManejaEventos());
cmdVerde.addActionListener(new ManejaEventos());
cmdAzul.addActionListener(new ManejaEventos());
cmdAmarillo.addActionListener(new ManejaEventos());
cmdRosa.addActionListener(new ManejaEventos());
cmdNegro.addActionListener(new ManejaEventos());
cmdBlanco.addActionListener(new ManejaEventos());
cmdCyan.addActionListener(new ManejaEventos());
cmdGris.addActionListener(new ManejaEventos());
cmdVioleta.addActionListener(new ManejaEventos());
cmdNaranja.addActionListener(new ManejaEventos());
cmdGrisO.addActionListener(new ManejaEventos());
cmdGrisC.addActionListener(new ManejaEventos());
getContentPane().add(cmdRojo);
getContentPane().add(cmdVerde);
getContentPane().add(cmdAzul);
getContentPane().add(cmdAmarillo);
getContentPane().add(cmdRosa);
getContentPane().add(cmdNegro);
getContentPane().add(cmdBlanco);
getContentPane().add(cmdCyan);
getContentPane().add(cmdGris);
getContentPane().add(cmdVioleta);
getContentPane().add(cmdNaranja);
getContentPane().add(cmdGrisO);
getContentPane().add(cmdGrisC);
}
public static void main(String[] a)
{
Colores c1= new Colores();
c1.setDefaultCloseOperation(EXIT_ON_CLOSE);
c1.show();
}
class ManejaEventos implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String color=((JButton) e.getSource()).getText();
if(color.equals("Rojo"))
getContentPane().setBackground(Color.red);
if(color.equals("Verde"))
getContentPane().setBackground(Color.green);
if(color.equals("Azul"))
getContentPane().setBackground(Color.blue);
if(color.equals("Amarillo"))
getContentPane().setBackground(Color.yellow);
if(color.equals("Rosa"))
getContentPane().setBackground(Color.pink);
if(color.equals("Negro"))
getContentPane().setBackground(Color.black);
if(color.equals("Blanco"))
getContentPane().setBackground(Color.white);
if(color.equals("Azul Claro"))
getContentPane().setBackground(Color.cyan);
if(color.equals("Gris"))
getContentPane().setBackground(Color.gray);
if(color.equals("Violeta"))
getContentPane().setBackground(Color.magenta);
if(color.equals("Naranja"))
getContentPane().setBackground(Color.orange);
if(color.equals("Gris Oscuro"))
getContentPane().setBackground(Color.darkGray);
if(color.equals("Gris Claro"))
getContentPane().setBackground(Color.lightGray);
}
}
}