No puedo utilizar el ActionListener varias veces
Publicado por Johan (2 intervenciones) el 27/10/2019 02:30:11
Estoy adentrándome en el mundo de la programación y pues estoy intentando programar un pequeño programa con interfaz. Hay una parte del codigo que no se si esta bien o mal, pero pues no se ejecuta como yo esperaba, es la parte del ActionListener que si bien me ejecuta la primera instrucción, la siguiente no la hace. Aqui dejo el codigo:
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package prototipo;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Prototipo implements ActionListener {
JFrame ventana1, ventana2, ventana3, ventana4;
JPanel panel1, panel2, panel3, panel4, panel5, panel6, panel7, panel8;
JLabel title1, title2, title3, title4, enunciado1, enunciado2, enunciado3;
JButton si1, no1, siguiente1, siguiente2, siguiente3, cancelar1, cancelar2, cancelar3;
JTextField entrada1, entrada2, entrada3;
//Primera ventana
public void crearPanel1(){
panel1 = new JPanel();
title1 = new JLabel("Función Cuadratica");
title1.setFont(new Font("VoxRound-Light", Font.BOLD,36));
title1.setBounds(2, 10, 354, 46);
enunciado1 = new JLabel("¿Desea calcular una función cuadratica");
enunciado1.setFont(new Font("VoxRound-Light", Font.BOLD,14));
enunciado1.setBounds(89, 110, 298, 18);
panel1.setLayout(null);
panel1.add(title1);
panel1.add(enunciado1);
}
public void crearPanel2 (){
panel2 = new JPanel();
si1 = new JButton("Si");
si1.setFont(new Font("VoxRound-Light", Font.BOLD,12));
no1 = new JButton("No");
no1.setFont(new Font("VoxRound-Light", Font.BOLD,12));
panel2.setLayout(null);
si1.setBounds(113, 30, 50, 28);
no1.setBounds(315, 30, 50, 28);
no1.addActionListener(this);
si1.addActionListener(this);
panel2.add(si1);
panel2.add(no1);
}
public void crearVentana(){
ventana1 = new JFrame("Función Cuadratica");
ventana1.setSize(800, 640);
ventana1.setLayout(new BoxLayout(ventana1.getContentPane(), BoxLayout.Y_AXIS));
ventana1.add(panel1);
ventana1.add(panel2);
ventana1.setLocationRelativeTo(null);
ventana1.setVisible(true);
ventana1.setDefaultCloseOperation(ventana1.EXIT_ON_CLOSE);
}
//Segunda ventana
public void crearPanel3(){
panel3 = new JPanel();
title2 = new JLabel("Introducción de valores");
title2.setFont(new Font("VoxRound-Light", Font.BOLD,36));
title2.setBounds(190, 10, 432, 46);
enunciado2 = new JLabel("Ingrese el valor de ax^2");
enunciado2.setFont(new Font("VoxRound-Light", Font.BOLD,24));
enunciado2.setBounds(260, 100, 281, 31);
entrada1 = new JTextField();
entrada1.setFont(new Font("VoxRound-Light", Font.BOLD,24));
entrada1.setBounds(370, 260, 54, 37);
panel3.setLayout(null);
panel3.add(title2);
panel3.add(enunciado2);
panel3.add(entrada1);
}
public void crearPanel4(){
panel4 = new JPanel();
siguiente1 = new JButton("Siguiente");
siguiente1.setFont(new Font("VoxRound-Light", Font.BOLD,24));
cancelar1 = new JButton("Cancelar");
cancelar1.setFont(new Font("VoxRound-Light", Font.BOLD,24));
panel4.setLayout(null);
siguiente1.setBounds(330, 160, 148, 39);
cancelar1.setBounds(640, 260, 142, 39);
cancelar1.addActionListener(this);
siguiente1.addActionListener(this);
panel4.add(siguiente1);
panel4.add(cancelar1);
}
public void crearVentana2(){
ventana2 = new JFrame("Función Cuadratica");
ventana2.setSize(800, 640);
ventana2.setLayout(new BoxLayout(ventana2.getContentPane(), BoxLayout.Y_AXIS));
ventana2.add(panel3);
ventana2.add(panel4);
ventana2.setLocationRelativeTo(null);
ventana2.setVisible(true);
ventana2.setDefaultCloseOperation(ventana2.EXIT_ON_CLOSE);
}
//Tercera ventana
public void crearPanel5(){
panel5 = new JPanel();
title3 = new JLabel("Introducción de valores");
title3.setFont(new Font("VoxRound-Light", Font.BOLD,36));
title3.setBounds(190, 10, 432, 46);
enunciado2 = new JLabel("Ingrese el valor de bx");
enunciado2.setFont(new Font("VoxRound-Light", Font.BOLD,24));
enunciado2.setBounds(260, 100, 281, 31);
entrada2 = new JTextField();
entrada2.setFont(new Font("VoxRound-Light", Font.BOLD,24));
entrada2.setBounds(370, 260, 54, 37);
panel5.setLayout(null);
panel5.add(title3);
panel5.add(enunciado2);
panel5.add(entrada2);
}
public void crearPanel6(){
panel6 = new JPanel();
siguiente2 = new JButton("Siguiente");
siguiente2.setFont(new Font("VoxRound-Light", Font.BOLD,24));
siguiente2.setBounds(330, 160, 148, 39);
cancelar2 = new JButton("Cancelar");
cancelar2.setFont(new Font("VoxRound-Light", Font.BOLD,24));
cancelar2.setBounds(640, 260, 142, 39);
panel6.setLayout(null);
siguiente2.addActionListener(this);
cancelar2.addActionListener(this);
panel6.add(siguiente2);
panel6.add(cancelar2);
}
public void crearVentana3(){
ventana3 = new JFrame("Función cuadratica");
ventana3.setSize(800,640);
ventana3.setLayout(new BoxLayout(ventana3.getContentPane(), BoxLayout.Y_AXIS));
ventana3.add(panel5);
ventana3.add(panel6);
ventana3.setLocationRelativeTo(null);
ventana3.setVisible(true);
ventana3.setDefaultCloseOperation(ventana3.EXIT_ON_CLOSE);
}
//Cuarta ventana
public void crearPanel7(){
panel7 = new JPanel();
title4 = new JLabel("Introducción de valores");
title4.setFont(new Font("VoxRound-Light", Font.BOLD,36));
title4.setBounds(190, 10, 432, 46);
enunciado3 = new JLabel("Ingrese el valor de c");
enunciado3.setFont(new Font("VoxRound-Light", Font.BOLD,24));
enunciado3.setBounds(260, 100, 281, 31);
entrada3 = new JTextField();
entrada3.setFont(new Font("VoxRound-Light", Font.BOLD,24));
entrada3.setBounds(370, 260, 54, 37);
panel7.setLayout(null);
panel7.add(title4);
panel7.add(enunciado3);
panel7.add(entrada3);
}
public void crearPanel8(){
panel8 = new JPanel();
siguiente3 = new JButton("Siguiente");
siguiente3.setFont(new Font("VoxRound-Light", Font.BOLD,24));
siguiente3.setBounds(330, 160, 148, 39);
cancelar3 = new JButton("Cancelar");
cancelar3.setFont(new Font("VoxRound-Light", Font.BOLD,24));
cancelar3.setBounds(640, 260, 142, 39);
panel8.setLayout(null);
siguiente3.addActionListener(this);
cancelar3.addActionListener(this);
panel8.add(siguiente3);
panel8.add(cancelar3);
}
public void crearVentana4(){
ventana4 = new JFrame("Función cuadratica");
ventana4.setSize(800,640);
ventana4.setLayout(new BoxLayout(ventana4.getContentPane(), BoxLayout.Y_AXIS));
ventana4.add(panel7);
ventana4.add(panel8);
ventana4.setLocationRelativeTo(null);
ventana4.setVisible(true);
ventana4.setDefaultCloseOperation(ventana4.EXIT_ON_CLOSE);
}
//ActionListener 1
@Override
public void actionPerformed(ActionEvent a) {
if (a.getSource()==si1){
ventana1.dispose();
crearPanel3();
crearPanel4();
crearVentana2();
/*Codigo que no se ejecuta, pero tampoco sale error.
if (a.getSource() == siguiente1){
ventana2.dispose();
}
*/
}
else{
ventana1.dispose();
}
}
//Constructor
public Prototipo(){
crearPanel1();
crearPanel2();
crearVentana();
}
}
Valora esta pregunta


0