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/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package dniover;
import java.awt.Dimension;
/**
*
* @author skirlappa
*/
public class ventana
extends javax.
swing.
JFrame {
/**
* Creates new form ventana
*/
public ventana() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jMenu1
= new javax.
swing.
JMenu(); jMenu2
= new javax.
swing.
JMenu();
setTitle("Ejemplo");
jMenu1.setText("Gestiones");
jMenuItem1.setText("Altas");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jMenuItem1ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem1);
jMenuItem2.setText("Modificar");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jMenuItem2ActionPerformed(evt);
}
});
jMenu1.add(jMenuItem2);
jMenuBar1.add(jMenu1);
jMenu2.setText("Edit");
jMenuBar1.add(jMenu2);
setJMenuBar(jMenuBar1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addGap(0,
400,
Short.
MAX_VALUE) );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addGap(0,
279,
Short.
MAX_VALUE) );
pack();
}// </editor-fold>
private void jMenuItem1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
}
private void jMenuItem2ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
//AQUI VA EL EVENTO PARA INTRODUCIR EL JPANEL
}
/**
* @param args the command line arguments
*/
public static void main
(String args
[]) { /* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
if ("Nimbus".equals(info.getName())) {
javax.
swing.
UIManager.
setLookAndFeel(info.
getClassName()); break;
}
}
java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(ventana.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new ventana().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JMenu jMenu1
; private javax.
swing.
JMenu jMenu2
; // End of variables declaration
}
Clase ejemplo donde contiene el JPanel
Código Java:
Ver originalpackage dniover;
/**
*
* @author skirlappa
*/
public class ejemplo
extends javax.
swing.
JPanel {
/**
* Creates new form ejemplo
*/
public ejemplo() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1
= new javax.
swing.
JLabel();
jLabel1.setText("Esto es un prueba.");
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(101, 101, 101)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 103, javax.swing.GroupLayout.PREFERRED_SIZE)
.
addContainerGap(94,
Short.
MAX_VALUE)) );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(48, 48, 48)
.addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 46, javax.swing.GroupLayout.PREFERRED_SIZE)
.
addContainerGap(119,
Short.
MAX_VALUE)) );
}// </editor-fold>
// Variables declaration - do not modify
private javax.
swing.
JLabel jLabel1
; // End of variables declaration
}