Abrir nueva ventana al hacer clic en un botón
Publicado por Lucas (23 intervenciones) el 09/06/2020 14:55:55
Buenas tardes,
Estoy en un nuevo proyecto para crear una gestiona de estudiantes. En una ventana principal tengo creado los paneles, botones y caja de texto de salida.
Al ver el listado de estudiantes se muestran correctamente en la caja de salida.
Agradecería si me ayudasen en implementar el código para que cuando haga clic en el botón de crear un nuevo estudiante, se abra una nueva ventana y pida una serie de datos habituales (nombre, apellidos, email,etc.) y después se añadan al txt.
Muchas gracias.
En la clase ventana tengo el siguiente código:
Además tengo creado el botón crearEstudiante:
Estoy en un nuevo proyecto para crear una gestiona de estudiantes. En una ventana principal tengo creado los paneles, botones y caja de texto de salida.
Al ver el listado de estudiantes se muestran correctamente en la caja de salida.
Agradecería si me ayudasen en implementar el código para que cuando haga clic en el botón de crear un nuevo estudiante, se abra una nueva ventana y pida una serie de datos habituales (nombre, apellidos, email,etc.) y después se añadan al txt.
Muchas gracias.
En la clase ventana tengo el siguiente código:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
public class Ventana extends JFrame {
private static final long serialVersionUID = 1L;
private final String TITULO = "Gestion de estudiantes";
private String rutaFichero = "./usuarios.txt";
FileInputStream fis = null;
ObjectInputStream ois = null;
public ArrayList<Estudiante> listEstudiantes = new ArrayList<Estudiante>();
// Seccion de declara
private JPanel panelInicio;
private JPanel panelBotones;
private JButton botonVerEstudiantes;
private JButton botonCrearEstudiante;
private JButton botonEliminarEstudiante;
private JButton botonSalir;
private JPanel panelTextoSalida;
private JScrollPane panelScrollSalida;
private static JTextArea cajaTextoSalida;
public Ventana() {
setTitle(TITULO);
inicializarComponentes();
registrarEscuchadores();
pack();
setVisible(true);
String listaEstudiantes = "";
try {
recuperarDeFichero();
Ventana.escribirDatosDeSalida(String.format( + listEstudiantes.size() + " estudiantes disponibles\n"));
} catch (Exception ex) {
System.out.println("El proceso de carga de datos ha tenido problemas y no ha concluido satisfactoriamente: El fichero 'fichero-inventado.txt' no ha podido ser encontrado");
}
escribirDatosDeSalida(String.format(listaEstudiantes));
}
private void inicializarComponentes() {
getContentPane().setLayout(new GridLayout(2, 2, 6, 0));
/* Panel bienvenida */
JLabel panelInicio=new JLabel("Control de estudiantes");
panelInicio.setVerticalAlignment(JLabel.CENTER);
panelInicio.setHorizontalAlignment(JLabel.CENTER);
panelInicio.setFont(new Font("Calibri", Font.BOLD, 20));
panelInicio.setForeground(Color.blue);
/* Panel botones */
panelBotones = new JPanel();
botonVerEstudiantes = new JButton("Ver todos los estudiantes");
panelBotones.add(botonVerEstudiantes);
botonCrearEstudiante = new JButton("Crear un nuevo estudiante");
panelBotones.add(botonCrearEstudiante);
botonEliminarEstudiante = new JButton("Eliminar un estudiante");
panelBotones.add(botonEliminarEstudiante);
botonSalir = new JButton("Salir");
panelBotones.add(botonSalir);
/* Panel salida */
panelTextoSalida = new JPanel();
panelTextoSalida.setLayout(new BorderLayout(2,4));
panelScrollSalida = new JScrollPane();
panelTextoSalida.add(panelScrollSalida);
cajaTextoSalida = new JTextArea(5, 0);
cajaTextoSalida.setEditable(false);
panelScrollSalida.setViewportView(cajaTextoSalida);
// Agregamos los Paneles para que sean visibles
getContentPane().add(panelInicio);
getContentPane().add(panelBotones);
getContentPane().add(panelTextoSalida);
// propiedades generales
setDefaultCloseOperation(EXIT_ON_CLOSE);
setPreferredSize(new Dimension(700, 550)); // Se establece la dimensión del frame
}
private void registrarEscuchadores() {
// Se registra e instancia el escuchador asociado
botonVerEstudiantes.addActionListener(new botonVerEstudiantesActionListener(this));
botonCrearEstudiante.addActionListener(new botonCrearEstudianteActionListener());
botonEliminarEstudiante.addActionListener(new botonEliminarEstudianteActionListener());
botonSalir.addActionListener(new botonSalirActionListener());
}
/*
* Métodos para la lectura y escritura de datos
*/
private void recuperarDeFichero() throws FileNotFoundException {
Scanner sc = new Scanner(new File(rutaFichero));
while (sc.hasNextLine()) {
String lineaCsv = sc.nextLine();
if (lineaCsv != null && lineaCsv.length() > 0) {
listEstudiantes.add(new Estudiante(lineaCsv));
}
}
}
public static void escribirDatosDeSalida(String texto) {
String formato = "hh:mm:ss a";
DateFormat dateFormat = new SimpleDateFormat(formato);
String tiempo = dateFormat.format(new GregorianCalendar().getTime());
cajaTextoSalida.append(tiempo + " :: " + texto + "\n");
}
/**
* Metodo para recuperar un fichero
*/
/**
* Otros metodos de interes
*
*/
public void activarListadoEstudiantes() {
this.panelInicio.setVisible(true);
}
}
Además tengo creado el botón crearEstudiante:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class botonCrearEstudianteActionListener implements ActionListener {
private Ventana ventana;
public botonCrearEstudianteActionListener() {
this.ventana = ventana;
}
@Override
public void actionPerformed(ActionEvent e) {
String listaEstudiantes;
}
}
Valora esta pregunta


0