Quiero pasar un objeto Jugador
de un jframe a otro, y que luego pueda operar con él al pulsar un botón.
Tengo puesto que, al pulsar un botón e instanciar la otra ventana, se pase el objeto con el constructor, como podréis ver en jButton3MouseClicked:
public Inicio() {
initComponents();
try {
FileInputStream fis = new FileInputStream("jugadoresinterf.ser");
ObjectInputStream ois = new ObjectInputStream(fis);
jugadores = (HashMap) ois.readObject();
ois.close();
fis.close();
} catch (IOException ioe) {
ioe.printStackTrace();
return;
} catch (ClassNotFoundException c) {
System.out.println("Elemento no encontrado.");
c.printStackTrace();
return;
}
System.out.println("Correcto");
}
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
Registro_jug reg = new Registro_jug();
reg.setVisible(true);
this.dispose();
}
private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
String user = jTextField1.getText();
String pass = jPasswordField1.getText();
if (jugadores.containsKey(user)) {
Jugador j1 = jugadores.get(user);
if (j1.getPassword().equals(pass)) {
Juego juego = new Juego(j1);
juego.setVisible(true);
this.dispose();
}
} else {
JOptionPane.showMessageDialog(this, "Nombre de usuario o contraseña incorrectos.", "Error de login", JOptionPane.INFORMATION_MESSAGE);
}
}
Después de esto, la ventana Juego
lo recibe:
public Juego(Jugador j1) {
initComponents();
jTextField2.setText(j1.getNombre());
jTextField3.setText(Double.toString(j1.getSaldo()));
Apuesta apuesta = new Apuesta();
}
Sin embargo, si quiero llamarlo desde un jButton1MouseClicked no me lo detecta:
private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.out.println(j1.getSaldo());
El main de juego es este, por si sirve de algo:
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 {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Juego.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
Jugador j1 = new Jug_Reg("ABCDE-FGHIJ-KMNLO", "99999999N", "TESTBOT", 2000.00, 6, 6, 1950, "12345678");
new Juego(j1).setVisible(true);
}
});
}
Le puse un jugador testbot para que no pete si ejecuto la ventana sin pasar por Inicio.
¿Cómo le paso al botón actionperformed
el jugador recibido del constructor?
Gracias.