Solución: Semana 2: Evidencia: Construcción de un applet - Parte I
Publicado por
el
Solución: Elabore un applet llamado AppletInterfaz el cual despliegue una lista con diferentes colores y que al seleccionar uno de los elementos de la lista se dibuje un rectángulo con el color seleccionado.
Para la construcción del applet tenga en cuenta la siguiente estructura:
import java.awt.*; import javax.swing.*; import java.applet.*; import java.awt.event.*; public class AppletInterfaz { JFrame frame = new JFrame(); Choice opciones = new Choice(); FlowLayout flowLayout1 = new FlowLayout(); final Color[] colores = {Color.red, Color.green, Color.blue, Color.black, Color.MAGENTA, Color.CYAN, Color.LIGHT_GRAY, Color.YELLOW}; int indice; public AppletInterfaz() { flowLayout1.setAlignment(FlowLayout.CENTER); opciones.add("Rojo"); opciones.add("Verde"); opciones.add("Azul"); opciones.add("Negro"); opciones.add("Magenta"); opciones.add("Cyan"); opciones.add("Light_Gray"); opciones.add("Amarillo"); opciones.select(0); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true); frame.setTitle("AppletInterfaz"); frame.setLayout(flowLayout1); frame.add(opciones, null); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); opciones.addItemListener(new java.awt.event.ItemListener() { public void itemStateChanged(ItemEvent e) { indice = opciones.getSelectedIndex(); frame.getContentPane().setBackground(colores[indice]); } }); } public static void main(String[] args) { new AppletInterfaz(); } }
Comentarios
Publicar un comentario