import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.util.StringTokenizer;
// Fue necesario importar solamente la clase StringTokenizer del paquete java.util ya que presento ambiguedad
// entre la clase List de java.util con la clase List del paquete java.awt
// OBJETIVO.
//-----------------------------------------
// PARTES DE LA INTERFAZ-------------------
// 1.- Un componente de tipo List
// Muestra los tipos de componente como opciones.
// 2.- Un componente de tipo TextArea
// 3.- Un componente de tipo Button
l punto 2.
// 4.- Un componente de tipo Label
e muestre en la etiqueta del paso 4
// NOTA. EL EVENTO DE LA LISTA SE REALIZA CON UN DOBLE CLICK IZQUIERDO DEL RATON.
// Se crea la clase que hereda todo lo de la clase java.applet.Applet
public class Componentes extends Applet
{
List Lista;
TextArea TAOpciones;
Label SALIDA;
String TipoComponente = "";
List Lista_2;
Choice CC;
Checkbox CB[ ];
int N;
public void init( )
{
// Fija al contenedor
setLayout(null);
// Se crea la lista de elementos
Lista = new List(1, false);
Lista.add("CheckBox");
Lista.add("List");
Lista.add("Choice");
add(Lista);
Lista.setBounds(10, 10, 100, 60);
// Creacion del area de texto
TAOpciones = new TextArea( );
add(TAOpciones);
TAOpciones.setBounds(10, 80, 100, 100);
// Creacion del botòn
Button BCrear = new Button("Crear");
add(BCrear);
BCrear.setBounds(10, 190, 60, 30);
BCrear.addActionListener(new Captura_Evento_Del_Boton( ));
// Posicionamiento de la etiqueta
SALIDA = new Label("Salida");
add(SALIDA);
SALIDA.setBounds(80, 190, 500, 30);
}
class Captura_Evento_Del_Boton implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
String Tipo = Lista.getSelectedItem();
String Opciones = TAOpciones.getText( );
StringTokenizer ST = new StringTokenizer(Opciones, "\n") ;
Crea_Componentes(Tipo, ST);
}
}
class Captura_Evento_Del_Check implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
SALIDA.setText((String)e.getItem());
}
}
void Crea_Componentes(String Tipo, StringTokenizer ST)
{
Limpia( );
TipoComponente = Tipo;
if (Tipo.equals("CheckBox"))
{
CB = new Checkbox[ST.countTokens() ];
int x = 200;
int y = 50;
N = ST.countTokens();
for (int k = 0; k < N; k++)
{
CB[k] = new Checkbox(ST.nextToken());
add(CB[k]);
CB[k].setBounds(x, y, 100, 30);
CB[k].addItemListener(new Captura_Evento_Del_Check()) ;
y += 35; // 30 del alto del componente + 5 de pilòn
}
}
if (Tipo.equals("List"))
{
Lista_2 = new List(1, false);
N = ST.countTokens();
for (int k = 0; k < N; k++)
Lista_2.add(ST.nextToken());
Lista_2.addActionListener(new EVENTO_LIST( )) ;
add(Lista_2);
Lista_2.setBounds(200, 50, 100, 100);
}
if (Tipo.equals("Choice"))
{
CC = new Choice();
N = ST.countTokens();
for (int k = 0; k < N; k++)
CC.add(ST.nextToken());
add(CC);
CC.setBounds(200, 50, 100, 30);
CC.addItemListener( new EVENTO_CHOICE( )) ;
}
}
class EVENTO_CHOICE implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
String CAD = (String)e.getItem() ;
SALIDA.setText(CAD);
}
}
class EVENTO_LIST implements ActionListener
{
public void actionPerformed(ActionEvent RRR)
{
String CAD = RRR.getActionCommand();
SALIDA.setText(CAD);
}
}
void Limpia( )
{
if (TipoComponente.equals("List"))
Lista_2.setVisible(false);
if (TipoComponente.equals("CheckBox"))
for (int k = 0; k < N; k++)
CB[k].setVisible(false);
if (TipoComponente.equals("Choice"))
CC.setVisible(false);
}
}