• Jueves 25 de Abril de 2024, 11:00

Autor Tema:  lista enlazada en Java  (Leído 2746 veces)

luisjoseblancom

  • Nuevo Miembro
  • *
  • Mensajes: 1
    • Ver Perfil
lista enlazada en Java
« en: Jueves 6 de Febrero de 2014, 04:22 »
0
hola buenas, necesito  ayuda con un programa. Soy nuevo programando en Java y me mandaron a hacer un programa de listas y me falta  una parte que es mostrar, mi duda es como hago para usar un boton para mostrar los datos guardados en un textarea. Como hago para enlazarlos de alguna manera.

Código: Java(TM) 2 Platform Standard Edition 5.0
  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5. package lista_clase;
  6.  
  7. /**
  8.  *
  9.  * @author cultca
  10.  */
  11. public class Pantalla extends javax.swing.JFrame {
  12.  
  13.      Alumno ptr,ptr1,inicio,ultimo,aux,c;
  14.     public Pantalla() {
  15.         initComponents();
  16.     }
  17.  
  18.     /**
  19.      * This method is called from within the constructor to initialize the form.
  20.      * WARNING: Do NOT modify this code. The content of this method is always
  21.      * regenerated by the Form Editor.
  22.      */
  23.     @SuppressWarnings("unchecked")
  24.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  25.     private void initComponents() {
  26.  
  27.         Nombre = new javax.swing.JLabel();
  28.         APELLIDO = new javax.swing.JLabel();
  29.         Cedula = new javax.swing.JLabel();
  30.         edad = new javax.swing.JLabel();
  31.         nombre = new javax.swing.JTextField();
  32.         apellido = new javax.swing.JTextField();
  33.         CEDULA = new javax.swing.JTextField();
  34.         EDAD = new javax.swing.JTextField();
  35.         jLabel5 = new javax.swing.JLabel();
  36.         BOTONINGRESAR = new javax.swing.JButton();
  37.         BOTONMOSTRAR = new javax.swing.JButton();
  38.         jScrollPane1 = new javax.swing.JScrollPane();
  39.         jList1 = new javax.swing.JList();
  40.  
  41.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  42.         setAlwaysOnTop(true);
  43.         setBackground(new java.awt.Color(0, 0, 0));
  44.         setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
  45.         setForeground(java.awt.Color.green);
  46.         setName("Pantalla"); // NOI18N
  47.  
  48.         Nombre.setBackground(new java.awt.Color(153, 153, 153));
  49.         Nombre.setFont(new java.awt.Font("Tunga", 0, 14)); // NOI18N
  50.         Nombre.setForeground(new java.awt.Color(51, 51, 255));
  51.         Nombre.setText("NOMBRE:");
  52.  
  53.         APELLIDO.setBackground(new java.awt.Color(153, 153, 153));
  54.         APELLIDO.setFont(new java.awt.Font("Tunga", 0, 14)); // NOI18N
  55.         APELLIDO.setForeground(new java.awt.Color(0, 0, 255));
  56.         APELLIDO.setText("APELLIDO:");
  57.  
  58.         Cedula.setBackground(new java.awt.Color(153, 153, 153));
  59.         Cedula.setFont(new java.awt.Font("Tunga", 0, 14)); // NOI18N
  60.         Cedula.setForeground(new java.awt.Color(0, 0, 255));
  61.         Cedula.setText("CEDULA:");
  62.  
  63.         edad.setBackground(new java.awt.Color(153, 153, 153));
  64.         edad.setFont(new java.awt.Font("Tunga", 0, 14)); // NOI18N
  65.         edad.setForeground(new java.awt.Color(0, 0, 255));
  66.         edad.setText("EDAD:");
  67.  
  68.         nombre.setFont(new java.awt.Font("Traditional Arabic", 3, 14)); // NOI18N
  69.  
  70.         apellido.setFont(new java.awt.Font("Traditional Arabic", 3, 14)); // NOI18N
  71.  
  72.         CEDULA.setFont(new java.awt.Font("Traditional Arabic", 3, 14)); // NOI18N
  73.         CEDULA.addActionListener(new java.awt.event.ActionListener() {
  74.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  75.                 CEDULAActionPerformed(evt);
  76.             }
  77.         });
  78.  
  79.         EDAD.setFont(new java.awt.Font("Traditional Arabic", 3, 14)); // NOI18N
  80.         EDAD.addActionListener(new java.awt.event.ActionListener() {
  81.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  82.                 EDADActionPerformed(evt);
  83.             }
  84.         });
  85.  
  86.         jLabel5.setFont(new java.awt.Font("Times New Roman", 0, 20)); // NOI18N
  87.         jLabel5.setText("Lista De Clases ");
  88.  
  89.         BOTONINGRESAR.setBackground(new java.awt.Color(102, 102, 102));
  90.         BOTONINGRESAR.setForeground(new java.awt.Color(0, 0, 255));
  91.         BOTONINGRESAR.setText("INGRESAR");
  92.         BOTONINGRESAR.addActionListener(new java.awt.event.ActionListener() {
  93.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  94.                 BOTONINGRESARActionPerformed(evt);
  95.             }
  96.         });
  97.  
  98.         BOTONMOSTRAR.setBackground(new java.awt.Color(102, 102, 102));
  99.         BOTONMOSTRAR.setForeground(new java.awt.Color(51, 51, 255));
  100.         BOTONMOSTRAR.setText("MOSTRAR");
  101.         BOTONMOSTRAR.addActionListener(new java.awt.event.ActionListener() {
  102.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.                 BOTONMOSTRARActionPerformed(evt);
  104.             }
  105.         });
  106.  
  107.         jScrollPane1.setViewportView(jList1);
  108.  
  109.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  110.         getContentPane().setLayout(layout);
  111.         layout.setHorizontalGroup(
  112.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  113.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  114.                 .addGap(0, 0, Short.MAX_VALUE)
  115.                 .addComponent(BOTONMOSTRAR)
  116.                 .addGap(120, 120, 120))
  117.             .addGroup(layout.createSequentialGroup()
  118.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  119.                     .addGroup(layout.createSequentialGroup()
  120.                         .addContainerGap()
  121.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  122.                             .addComponent(BOTONINGRESAR)
  123.                             .addGroup(layout.createSequentialGroup()
  124.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  125.                                     .addComponent(Nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 56, javax.swing.GroupLayout.PREFERRED_SIZE)
  126.                                     .addComponent(APELLIDO)
  127.                                     .addComponent(Cedula)
  128.                                     .addComponent(edad))
  129.                                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130.                                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  131.                                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  132.                                         .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE))
  133.                                     .addGroup(layout.createSequentialGroup()
  134.                                         .addGap(14, 14, 14)
  135.                                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.                                             .addComponent(CEDULA, javax.swing.GroupLayout.PREFERRED_SIZE, 140, javax.swing.GroupLayout.PREFERRED_SIZE)
  137.                                             .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, 210, javax.swing.GroupLayout.PREFERRED_SIZE)
  138.                                             .addComponent(EDAD, javax.swing.GroupLayout.PREFERRED_SIZE, 49, javax.swing.GroupLayout.PREFERRED_SIZE))))))
  139.                         .addGap(18, 18, 18)
  140.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))
  141.                     .addGroup(layout.createSequentialGroup()
  142.                         .addGap(118, 118, 118)
  143.                         .addComponent(jLabel5)))
  144.                 .addContainerGap(70, Short.MAX_VALUE))
  145.         );
  146.         layout.setVerticalGroup(
  147.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  148.             .addGroup(layout.createSequentialGroup()
  149.                 .addGap(9, 9, 9)
  150.                 .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
  151.                 .addGap(18, 18, 18)
  152.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  153.                     .addGroup(layout.createSequentialGroup()
  154.                         .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 262, Short.MAX_VALUE)
  155.                         .addGap(18, 18, 18)
  156.                         .addComponent(BOTONMOSTRAR))
  157.                     .addGroup(layout.createSequentialGroup()
  158.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  159.                             .addComponent(nombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  160.                             .addComponent(Nombre, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE))
  161.                         .addGap(6, 6, 6)
  162.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  163.                             .addComponent(apellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  164.                             .addComponent(APELLIDO, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
  165.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  166.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  167.                             .addComponent(CEDULA, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  168.                             .addComponent(Cedula))
  169.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  170.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  171.                             .addComponent(EDAD, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  172.                             .addComponent(edad))
  173.                         .addGap(18, 18, 18)
  174.                         .addComponent(BOTONINGRESAR)
  175.                         .addGap(19, 19, 19))))
  176.         );
  177.  
  178.         pack();
  179.     }// </editor-fold>                        
  180.  
  181.     private void EDADActionPerformed(java.awt.event.ActionEvent evt) {                                    
  182.         // TODO add your handling code here:
  183.     }                                    
  184.  
  185.     private void CEDULAActionPerformed(java.awt.event.ActionEvent evt) {                                      
  186.         // TODO add your handling code here:
  187.     }                                      
  188.  
  189.     private void BOTONINGRESARActionPerformed(java.awt.event.ActionEvent evt) {                                              
  190.         // TODO add your handling code here:
  191.         if (ptr==null){
  192.         ptr=new Alumno();
  193.         // esto es para convertir los enteros en string
  194.        ptr.set_cedula(Integer.parseInt(CEDULA.getText()));
  195.        ptr.set_edad(Integer.parseInt(EDAD.getText()));
  196.        // esto es para borrar los datos en la pantalla
  197.        nombre.setText(" ");
  198.        apellido.setText(" ");
  199.        CEDULA.setText(" ");
  200.        EDAD.setText(" ");
  201.         ptr1=ptr;
  202.         inicio=ptr;
  203.         ultimo=ptr;
  204.         ultimo.sig=null;
  205.         }
  206.         else {
  207.             ptr=new Alumno();
  208.             ptr.set_nombre(nombre.getText());
  209.             ptr.set_apellido(apellido.getText());
  210.             //esto es para convertir los enteros en string
  211.             ptr.set_cedula(Integer.parseInt(CEDULA.getText()));
  212.             ptr.set_edad(Integer.parseInt(EDAD.getText()));
  213.             // esto es para borrar los datos de la pantalla
  214.             nombre.setText(" ");
  215.             apellido.setText(" ");
  216.             CEDULA.setText(" ");
  217.             EDAD.setText(" ");
  218.             ptr1.sig=ptr;
  219.             ultimo=ptr;
  220.             ultimo.sig=null;
  221.         }
  222.     }                                            
  223.  
  224.     private void BOTONMOSTRARActionPerformed(java.awt.event.ActionEvent evt) {                                            
  225.         // TODO add your handling code here:
  226.         aux=ptr;
  227.         int c=0;
  228.         while (aux!=null){
  229.         System.out.println("Nombre : "+aux.nombre);
  230.         System.out.println("Apellido : "+aux.apellido);
  231.         System.out.println("Cedula : "+aux.cedula);
  232.         System.out.println("Edad : "+aux.edad);
  233.         c++;
  234.         }
  235.         if (c==0){
  236.         System.out.println("LISTA VACIA");
  237.         }
  238.      
  239.     }                                            
  240.  
  241.     /**
  242.      * @param args the command line arguments
  243.      */
  244.     public static void main(String args[]) {
  245.         /* Set the Nimbus look and feel */
  246.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  247.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  248.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  249.          */
  250.         try {
  251.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  252.                 if ("Nimbus".equals(info.getName())) {
  253.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  254.                     break;
  255.                 }
  256.             }
  257.         } catch (ClassNotFoundException ex) {
  258.             java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  259.         } catch (InstantiationException ex) {
  260.             java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  261.         } catch (IllegalAccessException ex) {
  262.             java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  263.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  264.             java.util.logging.Logger.getLogger(Pantalla.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  265.         }
  266.         //</editor-fold>
  267.  
  268.         /* Create and display the form */
  269.         java.awt.EventQueue.invokeLater(new Runnable() {
  270.             public void run() {
  271.                 new Pantalla().setVisible(true);
  272.             }
  273.         });
  274.     }
  275.     // Variables declaration - do not modify                    
  276.     private javax.swing.JLabel APELLIDO;
  277.     private javax.swing.JButton BOTONINGRESAR;
  278.     private javax.swing.JButton BOTONMOSTRAR;
  279.     private javax.swing.JTextField CEDULA;
  280.     private javax.swing.JLabel Cedula;
  281.     private javax.swing.JTextField EDAD;
  282.     private javax.swing.JLabel Nombre;
  283.     private javax.swing.JTextField apellido;
  284.     private javax.swing.JLabel edad;
  285.     private javax.swing.JLabel jLabel5;
  286.     private javax.swing.JList jList1;
  287.     private javax.swing.JScrollPane jScrollPane1;
  288.     private javax.swing.JTextField nombre;
  289.     // End of variables declaration                  
  290. }
  291.