Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. package botonlistapanelapplet;
  2.  
  3. import java.awt.*;
  4. import javax.swing.JApplet;
  5. import javax.swing.JButton;
  6. import javax.swing.JTextField;
  7.  
  8. /**
  9.  *
  10.  * @author Rafa
  11.  */
  12. public class BotonListaPanelApplet extends JApplet{
  13.     //declaracion de variables
  14.     Panel cards;
  15.     String botonPanel = "Pulsar Botones";
  16.     String textPanel = "Ingresar Datos";
  17.     JButton boton1, boton2;
  18.     JTextField campoTexto1, campoTexto2;
  19.    
  20.     public void init(){
  21.         setLayout(new BorderLayout());
  22.         setFont(new Font("Helvetica", Font.PLAIN, 14));
  23.        
  24.         //Crea los botones y TextField
  25.         JButton boton1 = new JButton("Boton1");
  26.         JButton boton2 = new JButton("Boton2");
  27.         JTextField campoTexto1 = new JTextField("Ingrese su Nombre", 20);
  28.         JTextField campoTexto2 = new JTextField("Ingrese su Apellido", 20);
  29.        
  30.         //Asignacion de colores a boton y a TextField
  31.         boton1.setBackground(Color.red);
  32.         boton2.setBackground(Color.red);
  33.         campoTexto1.setBackground(Color.red);
  34.         campoTexto2.setBackground(Color.red);
  35.        
  36.         //ponemos la lista en un panel
  37.         Panel panel = new Panel();
  38.         Choice escoger = new Choice();
  39.         escoger.addItem(botonPanel);
  40.         escoger.addItem(textPanel);
  41.        
  42.         //Agrega color a Choice
  43.         escoger.setBackground(Color.yellow);
  44.        
  45.         //agrega la lista al panel
  46.         panel.add(escoger);
  47.         add("North", panel);
  48.        
  49.         cards = new Panel();
  50.         cards.setLayout(new CardLayout());
  51.        
  52.         //Panel donde se pegan los botones
  53.         Panel panel1 = new Panel();
  54.         panel1.add(boton1);
  55.         panel1.add(boton2);
  56.        
  57.         //Panel donde se pega el TextField
  58.         Panel panel2 = new Panel();
  59.         panel2.add(campoTexto1);
  60.         panel2.add(campoTexto2);
  61.        
  62.        
  63.         cards.add(botonPanel, panel1);
  64.         cards.add(textPanel, panel2);
  65.        
  66.         add("Center", cards);
  67.     }
  68.    
  69.     public boolean action(Event evt,Object arg) {
  70.         if(evt.target instanceof Choice) {
  71.             ((CardLayout)cards.getLayout()).show(cards, (String)arg);
  72.             return true;
  73.         }
  74.         return false;
  75.     }
  76.    
  77. }