import java.awt.*; import java.awt.event.*; import java.applet.*; // OBJETIVO: // Crear un conjunto de componentes visible o invisibles. // El tipo, la cantidad, y el atributo de visibilidad se controlan cada uno por un choice. oice de cantidad) public class ComponentesVarios extends Applet { Label SALIDA = new Label(); Choice ChoiceN, ChoiceVisible, ChoiceComponentes; TextField TF[ ]; Button BOK; public void init( ) { setLayout(null); // Fijamos al contenedor setBackground(new Color((float)0.4, (float)1.0, (float)0.0)); SALIDA.setBounds(300, 400, 150, 30); add(SALIDA); Button BOK = new Button("OK"); add(BOK); BOK.setBounds(300, 10, 40, 30); BOK.addActionListener(new EVENTO_BOTON()); // Se crean las etiquetas que forman parte de la interfaz Label LComponente = new Label("COMPONENTE"); Label LVisible = new Label("VISIBLE"); Label LN = new Label("N"); LComponente.setBackground(Color.black); LVisible.setBackground(Color.black); LN.setBackground(Color.black); LComponente.setForeground(Color.yellow); LVisible.setForeground(Color.yellow); LN.setForeground(Color.yellow); // Se añaden al contenedor add(LComponente); add(LVisible); add(LN); // Se posicionan en el contenedor LComponente.setBounds(10, 10, 120, 30); // -(x, y, ancho, alto)-. LVisible.setBounds(140, 10, 100, 30); LN.setBounds(250, 10, 40, 30); // Creaciòn de la lista de componentes ChoiceComponentes = new Choice(); ChoiceComponentes.add("Checkbox") ; ChoiceComponentes.add("Choice") ; ChoiceComponentes.add("List") ; add(ChoiceComponentes); // Añadir al contenedor ChoiceComponentes.setBounds(10, 50, 120, 30); //ChoiceComponentes.addItemListener(new EVENTO_CHOICE( )) ; // Captura del evento // Creaciòn de los otros 2 choices -visible & N- ChoiceVisible = new Choice(); ChoiceVisible.add("Si") ; ChoiceVisible.add("No") ; add(ChoiceVisible); // Añadir al contenedor ChoiceVisible.setBounds(140, 50, 100, 30); ChoiceN = new Choice(); ChoiceN.add("1") ; ChoiceN.add("2") ; ChoiceN.add("3") ; ChoiceN.add("4") ; ChoiceN.add("5") ; add(ChoiceN); // Añadir al contenedor ChoiceN.setBounds(250, 50, 40, 30); } class EVENTO_CHOICE implements ItemListener { public void itemStateChanged(ItemEvent RAFA) { String CAD = (String)RAFA.getItem(); SALIDA.setText(CAD); } } class EVENTO_BOTON_2 implements ActionListener { public void actionPerformed(ActionEvent e) { String Tipo = ChoiceComponentes.getSelectedItem( ); String Visible = ChoiceVisible.getSelectedItem( ); int y = 100; boolean VISIBLE; if ( Visible.equals("Si") ) VISIBLE = true; else VISIBLE = false; if (Tipo.equals("Checkbox")) { Checkbox CB[ ] = new Checkbox[ TF.length ]; for (int k = 0; k < TF.length; k++) { CB[k] = new Checkbox( TF[k] .getText( )); TF[ k ].removeNotify( ); add( CB[ k ] ); CB[ k ].setBounds(100, y, 100, 30); CB[ k ].setVisible(VISIBLE); y += 35; } } if (Tipo.equals("Choice")) { Choice CB = new Choice( ); for (int k = 0; k < TF.length; k++) { CB.add( TF[k] .getText( )); TF[ k ].removeNotify( ); } add( CB); CB.setBounds(100, 100, 100, 30); CB.setVisible(VISIBLE); } if (Tipo.equals("List")) { List CB = new List( ); for (int k = 0; k < TF.length; k++) { CB.add( TF[k] .getText( )); TF[ k ].removeNotify( ); } add( CB); CB.setBounds(100, 100, 100, 30); CB.setVisible(VISIBLE); } BOK.removeNotify( ); } } class EVENTO_BOTON implements ActionListener { public void actionPerformed(ActionEvent e) { int Opciones = Integer.parseInt(ChoiceN.getSelectedItem() ); TF = new TextField[ Opciones]; BOK = new Button("OK"); add(BOK); BOK.addActionListener(new EVENTO_BOTON_2()); int y = 100; for (int k = 0; k < Opciones; k++) { TF[ k ] = new TextField( ); add(TF[k]); TF[k].setBounds(100, y, 100, 30); y += 35; } BOK.setBounds(100, y, 40, 30); } } }