Ver Mensaje Individual
  #1 (permalink)  
Antiguo 17/10/2015, 11:30
skirlappa
 
Fecha de Ingreso: junio-2005
Mensajes: 107
Antigüedad: 18 años, 10 meses
Puntos: 0
Consulta básica - incluir JPanel en un JFrame con netbeands

Hola,

Tengo una duda, estoy aprendiendo, sed pacientes, y gracias de antemano.

He creado un JFrame con el editor gráfico de netbeans, este tiene un JMenu con sus JMenuItem, y quiero que al pulsar se mestre una JPanel que he creado en una clase distinta:

Clase ventana contiene el JFrame
Código Java:
Ver original
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package dniover;
  7.  
  8. import static java.awt.AWTEventMulticaster.add;
  9. import java.awt.Dimension;
  10.  
  11. /**
  12.  *
  13.  * @author skirlappa
  14.  */
  15. public class ventana extends javax.swing.JFrame {
  16.  
  17.     /**
  18.      * Creates new form ventana
  19.      */
  20.     public ventana() {
  21.         initComponents();
  22.     }
  23.  
  24.     /**
  25.      * This method is called from within the constructor to initialize the form.
  26.      * WARNING: Do NOT modify this code. The content of this method is always
  27.      * regenerated by the Form Editor.
  28.      */
  29.     @SuppressWarnings("unchecked")
  30.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  31.     private void initComponents() {
  32.  
  33.         jMenuBar1 = new javax.swing.JMenuBar();
  34.         jMenu1 = new javax.swing.JMenu();
  35.         jMenuItem1 = new javax.swing.JMenuItem();
  36.         jMenuItem2 = new javax.swing.JMenuItem();
  37.         jMenu2 = new javax.swing.JMenu();
  38.  
  39.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  40.         setTitle("Ejemplo");
  41.  
  42.         jMenu1.setText("Gestiones");
  43.  
  44.         jMenuItem1.setText("Altas");
  45.         jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
  46.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  47.                 jMenuItem1ActionPerformed(evt);
  48.             }
  49.         });
  50.         jMenu1.add(jMenuItem1);
  51.  
  52.         jMenuItem2.setText("Modificar");
  53.         jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
  54.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  55.                 jMenuItem2ActionPerformed(evt);
  56.             }
  57.         });
  58.         jMenu1.add(jMenuItem2);
  59.  
  60.         jMenuBar1.add(jMenu1);
  61.  
  62.         jMenu2.setText("Edit");
  63.         jMenuBar1.add(jMenu2);
  64.  
  65.         setJMenuBar(jMenuBar1);
  66.  
  67.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  68.         getContentPane().setLayout(layout);
  69.         layout.setHorizontalGroup(
  70.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  71.             .addGap(0, 400, Short.MAX_VALUE)
  72.         );
  73.         layout.setVerticalGroup(
  74.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  75.             .addGap(0, 279, Short.MAX_VALUE)
  76.         );
  77.  
  78.         pack();
  79.     }// </editor-fold>                        
  80.  
  81.     private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  82.         // TODO add your handling code here:
  83.        
  84.     }                                          
  85.  
  86.     private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                          
  87.  
  88.      //AQUI VA EL EVENTO PARA INTRODUCIR EL JPANEL
  89.        
  90.     }                                          
  91.  
  92.     /**
  93.      * @param args the command line arguments
  94.      */
  95.     public static void main(String args[]) {
  96.         /* Set the Nimbus look and feel */
  97.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  98.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  99.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  100.          */
  101.         try {
  102.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  103.                 if ("Nimbus".equals(info.getName())) {
  104.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  105.                     break;
  106.                 }
  107.             }
  108.         } catch (ClassNotFoundException ex) {
  109.             java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  110.         } catch (InstantiationException ex) {
  111.             java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  112.         } catch (IllegalAccessException ex) {
  113.             java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  114.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  115.             java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  116.         }
  117.         //</editor-fold>
  118.  
  119.         /* Create and display the form */
  120.         java.awt.EventQueue.invokeLater(new Runnable() {
  121.             public void run() {
  122.                 new ventana().setVisible(true);
  123.             }
  124.         });
  125.     }
  126.  
  127.     // Variables declaration - do not modify                    
  128.     private javax.swing.JMenu jMenu1;
  129.     private javax.swing.JMenu jMenu2;
  130.     private javax.swing.JMenuBar jMenuBar1;
  131.     private javax.swing.JMenuItem jMenuItem1;
  132.     private javax.swing.JMenuItem jMenuItem2;
  133.     // End of variables declaration                  
  134. }

Clase ejemplo donde contiene el JPanel

Código Java:
Ver original
  1. package dniover;
  2.  
  3. /**
  4.  *
  5.  * @author skirlappa
  6.  */
  7. public class ejemplo extends javax.swing.JPanel {
  8.  
  9.     /**
  10.      * Creates new form ejemplo
  11.      */
  12.     public ejemplo() {
  13.         initComponents();
  14.     }
  15.  
  16.     /**
  17.      * This method is called from within the constructor to initialize the form.
  18.      * WARNING: Do NOT modify this code. The content of this method is always
  19.      * regenerated by the Form Editor.
  20.      */
  21.     @SuppressWarnings("unchecked")
  22.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  23.     private void initComponents() {
  24.  
  25.         jLabel1 = new javax.swing.JLabel();
  26.  
  27.         jLabel1.setText("Esto es un prueba.");
  28.  
  29.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
  30.         this.setLayout(layout);
  31.         layout.setHorizontalGroup(
  32.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  33.             .addGroup(layout.createSequentialGroup()
  34.                 .addGap(101, 101, 101)
  35.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
  36.                 .addContainerGap(94, Short.MAX_VALUE))
  37.         );
  38.         layout.setVerticalGroup(
  39.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  40.             .addGroup(layout.createSequentialGroup()
  41.                 .addGap(48, 48, 48)
  42.                 .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
  43.                 .addContainerGap(119, Short.MAX_VALUE))
  44.         );
  45.     }// </editor-fold>                        
  46.  
  47.  
  48.     // Variables declaration - do not modify                    
  49.     private javax.swing.JLabel jLabel1;
  50.     // End of variables declaration                  
  51. }