Hola buen día, necesito una ayudita con este codigo, apenas me estoy iniciando en esto de las interfaces graficas con java. La cosa es que necesito almecenar varios datos en una pila,estos datos los estoy capturando desde los jtextfield, ya he hecho el progrma en consola y funciona sin problemas, pero aca no me guarda los valores me los asigna como nulo luego de salir del metodo insertar en la clase registro, porque si pongo un mensaje por consola del metodo insertar del contruct me asigna los valores y al mismo tiempo pongo un JOptionPane en la clase registro justo despues de la llamada al metodo y me da los valores nulos pero en consola me muestra los valores, realmente no entiendo xD.
aca les dejo partes del codigo.
Clase pila. donde se deben apilar los datos.package pila;
/**
*
* @author Th3Crze2
*/
public class pila {
private pila siguiente;
/**
* @return the siguiente
*/
public pila getSiguiente() {
return siguiente;
}
/**
* @param siguiente the siguiente to set
*/
public void setSiguiente(pila siguiente) {
this.siguiente = siguiente;
}
/**
* @return the nombre
*/
return nombre;
}
/**
* @param nombre the nombre to set
*/
public void setNombre
(String nombre
) { this.nombre = nombre;
}
/**
* @return the apellido
*/
return apellido;
}
/**
* @param apellido the apellido to set
*/
public void setApellido
(String apellido
) { this.apellido = apellido;
}
}
Clase Contruct el motodo insertar. package pila;
public class contruct {
pila cima;
int tamanio;
public contruct(){
cima=null;
tamanio=0;
}
public boolean pilaVacia(){
return cima==null;
}
//PUSH
pila nuevo=new pila();
nuevo.setNombre(non);
nuevo.setApellido(ape);
nuevo.setSiguiente(cima);
cima=nuevo;
tamanio++;
}
//POP
}
}
Clase registro donde esta la Interfaz de registrar. el codigo lo estoy metiendo en el boton registrar(jButton1).
package pila;
import java.awt.Color;
import javax.swing.JOptionPane;
import javax.swing.border.LineBorder;
/**
*
* @author Th3Crze2
*/
public class registro
extends javax.
swing.
JFrame { public registro() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jLabel1
= new javax.
swing.
JLabel(); jLabel2
= new javax.
swing.
JLabel(); jLabel3
= new javax.
swing.
JLabel(); jButton1
= new javax.
swing.
JButton(); jButton2
= new javax.
swing.
JButton(); jLabel4
= new javax.
swing.
JLabel(); jLabel5
= new javax.
swing.
JLabel();
jLabel1.setText("Nombre:");
textNombre.
addMouseListener(new java.
awt.
event.
MouseAdapter() { public void mouseClicked
(java.
awt.
event.
MouseEvent evt
) { textNombreMouseClicked(evt);
}
public void mouseExited
(java.
awt.
event.
MouseEvent evt
) { textNombreMouseExited(evt);
}
});
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { textNombreActionPerformed(evt);
}
});
textNombre.
addKeyListener(new java.
awt.
event.
KeyAdapter() { public void keyTyped
(java.
awt.
event.
KeyEvent evt
) { textNombreKeyTyped(evt);
}
});
jLabel2.setText("REGISTRO DE ESTUDIANTES");
jLabel3.setText("Apellido");
jButton1.setText("REGISTRAR");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton1ActionPerformed(evt);
}
});
jButton2.setText("VOLVER");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton2ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(81, 81, 81)
.addComponent(jLabel2))
.addGroup(layout.createSequentialGroup()
.addGap(32, 32, 32)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addGroup(layout.createSequentialGroup()
.addComponent(jButton1)
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED, javax.
swing.
GroupLayout.
DEFAULT_SIZE,
Short.
MAX_VALUE) .addComponent(jButton2))
.addGroup(layout.createSequentialGroup()
.addComponent(jLabel1)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 48, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(layout.createSequentialGroup()
.addGap(13, 13, 13)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 167, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(textApellido, javax.swing.GroupLayout.PREFERRED_SIZE, 177, javax.swing.GroupLayout.PREFERRED_SIZE))))))
.addComponent(jLabel3))))
.
addContainerGap(47,
Short.
MAX_VALUE)) .addComponent(jSeparator1, javax.swing.GroupLayout.Alignment.TRAILING)
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(9, 9, 9)
.addComponent(jLabel2)
.addGap(3, 3, 3)
.addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
.addComponent(jLabel5)
.addGap(12, 12, 12)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(textNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(12, 12, 12)
.addComponent(jLabel4)
.addGap(30, 30, 30)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(textApellido, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.
addPreferredGap(javax.
swing.
LayoutStyle.
ComponentPlacement.
RELATED,
183,
Short.
MAX_VALUE) .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jButton1)
.addComponent(jButton2))
.addGap(25, 25, 25))
);
pack();
}// </editor-fold>
private void textNombreActionPerformed
(java.
awt.
event.
ActionEvent evt
) { // TODO add your handling code here:
}
private void textNombreMouseClicked
(java.
awt.
event.
MouseEvent evt
) {
textNombre.
setBorder(new LineBorder(Color.
red));// TODO add your handling code here: }
private void textNombreMouseExited
(java.
awt.
event.
MouseEvent evt
) {
}
private void jButton2ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { principal p=new principal();
p.setVisible(true);// TODO add your handling code here:
this.setVisible(false);
}
private void jButton1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { contruct cc=new contruct();
pila p=new pila();
if("".equals(textNombre.getText())|| "".equals(textApellido.getText())){
}else{
tnon=textNombre.getText();
tape=textApellido.getText();
cc.insertar(tnon, tape);
}
textNombre.setText("");
textApellido.setText("");
}
private void textNombreKeyTyped
(java.
awt.
event.
KeyEvent evt
) { char c=evt.getKeyChar();
getToolkit().beep();
evt.consume();
} // TODO add your handling code here:
}
/**
* @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(registro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(registro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(registro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(registro.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new registro().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton jButton1
; private javax.
swing.
JButton jButton2
; private javax.
swing.
JLabel jLabel1
; private javax.
swing.
JLabel jLabel2
; private javax.
swing.
JLabel jLabel3
; private javax.
swing.
JLabel jLabel4
; private javax.
swing.
JLabel jLabel5
; // End of variables declaration
}
Y la clase principal donde estar la interfaz pricipal con todas las opciones, aun no tiene nada importante , pero de todas maneras hay va package pila;
import com.sun.glass.ui.Application;
import javax.swing.JOptionPane;
/**
*
* @author Th3Crze2
*/
public class principal
extends javax.
swing.
JFrame { contruct co=new contruct();
pila pila=new pila();
/**
* Creates new form principal
*/
public principal() {
initComponents();
}
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jButton1
= new javax.
swing.
JButton(); jButton2
= new javax.
swing.
JButton(); jButton3
= new javax.
swing.
JButton(); jButton4
= new javax.
swing.
JButton(); jButton5
= new javax.
swing.
JButton(); jButton6
= new javax.
swing.
JButton();
jInternalFrame1.setVisible(true);
javax.swing.GroupLayout jInternalFrame1Layout = new javax.swing.GroupLayout(jInternalFrame1.getContentPane());
jInternalFrame1.getContentPane().setLayout(jInternalFrame1Layout);
jInternalFrame1Layout.setHorizontalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addGap(0,
0,
Short.
MAX_VALUE) );
jInternalFrame1Layout.setVerticalGroup(
jInternalFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.
addGap(0,
0,
Short.
MAX_VALUE) );
setCursor
(new java.
awt.
Cursor(java.
awt.
Cursor.
DEFAULT_CURSOR));
jButton1.setText("Ingresar Estudiante");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton1ActionPerformed(evt);
}
});
jButton2.setText("Sacar Estudiante");
jButton3.setText("Mostrar Estudiantes");
jButton4.setText("Cima de la Pila");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton4ActionPerformed(evt);
}
});
jButton5.setText("Finalizar Aplicación");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton5ActionPerformed(evt);
}
});
jButton6.setText("Tamaño de la pila");
public void actionPerformed
(java.
awt.
event.
ActionEvent evt
) { jButton6ActionPerformed(evt);
}
});
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(72, 72, 72)
.addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jButton6, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton5, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton4, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton3, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
.
addContainerGap(76,
Short.
MAX_VALUE)) );
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(layout.createSequentialGroup()
.addGap(35, 35, 35)
.addComponent(jButton1)
.addGap(33, 33, 33)
.addComponent(jButton2)
.addGap(33, 33, 33)
.addComponent(jButton3)
.addGap(36, 36, 36)
.addComponent(jButton4)
.addGap(34, 34, 34)
.addComponent(jButton6)
.addGap(35, 35, 35)
.addComponent(jButton5)
.
addContainerGap(34,
Short.
MAX_VALUE)) );
pack();
}// </editor-fold>
private void jButton1ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { registro r = new registro();
r.setVisible(true);
this.setVisible(false);
// TODO add your handling code here:
}
private void jButton5ActionPerformed
(java.
awt.
event.
ActionEvent evt
) { dispose(); // TODO add your handling code here:
}
private void jButton4ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
}
private void jButton6ActionPerformed
(java.
awt.
event.
ActionEvent evt
) {
}
/**
* @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(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
java.util.logging.Logger.getLogger(principal.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
public void run() {
new principal().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.
swing.
JButton jButton1
; private javax.
swing.
JButton jButton2
; private javax.
swing.
JButton jButton3
; private javax.
swing.
JButton jButton4
; private javax.
swing.
JButton jButton5
; private javax.
swing.
JButton jButton6
; // End of variables declaration
}
sinceramente no logro ver que hago mal, gracias de antemano y disculpas si publique esto donde no debia.