Detener Método con Botón
Publicado por Diego (14 intervenciones) el 23/07/2021 08:05:42
Hola muy buenas, necesito su ayuda por favor. Resulta que estoy haciendo una especie de cronómetro para mi uso personal..... la cuestión es que no logro hacer funcionar el programa con botones (iniciar, stop, reset). En el método del botón, logro que el programa inicie usando:
Pero luego no se cómo detenerlo ni resetearlo. Entiendo que el programa debe correr usando:
Pero no........, entonces no se si llegaría a funcionar t.stop(); o t.reset();.... Lo que necesito entender es como hago para que inicie con t.start();, creo que después de eso ya funcionaría el resto, pero no hallo la solución. Muchas gracias por su ayuda como siempre. Este es mi código:
1
2
3
4
private void iniciarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
acciones.actionPerformed(evt);
}
Pero luego no se cómo detenerlo ni resetearlo. Entiendo que el programa debe correr usando:
1
2
3
4
private void iniciarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
t.start();
}
Pero no........, entonces no se si llegaría a funcionar t.stop(); o t.reset();.... Lo que necesito entender es como hago para que inicie con t.start();, creo que después de eso ya funcionaría el resto, pero no hallo la solución. Muchas gracias por su ayuda como siempre. Este es mi 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
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
package prueba;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.TimerTask;
import javax.swing.Timer;
/**
*
* @author Diego Erazo <diegoerazo83 at gmail.com>
*/
public class NewJFrame extends javax.swing.JFrame {
/**
* Creates new form NewJFrame
*/
public NewJFrame() {
initComponents();
t = new Timer(1, acciones);
}
private Timer t;
private ActionListener acciones = new ActionListener() {
@Override
public void actionPerformed(ActionEvent ae) {
java.util.Timer timer = new java.util.Timer();
int k = 1;
int c = Integer.parseInt(parrafos.getText());
int j = 0; //60 / (c + 3); //Minutos
double num = (double) 60 / (c + 3);
double redondeo = Math.round(num * 100.0) / 100.0;
double seg = redondeo - j;
double yy = seg * 100;
int y = 10; //((int) (yy)) * 60 / 100; //Segundos
while (k <= (c
+ 3)) {
int a = (j * 60) + y - 1;
timer.scheduleAtFixedRate(new TimerTask() {
int i = a; //repetirse la cantidad total de segundos
int ii = 89, iii = 89;
int x = 0;
int sec = y, min = j;
int sec1 = 30, min1 = 1;
int sec2 = 30, min2 = 1;
public void run() {
if (x == 0) {
pruebalabel.setText("Palabras de Introducción");
sec1--;
if (sec1 == 00) {
sec1 = 60;
min1--;
}
ii--;
tiemporestantelabel.setText(min1 + ":" + sec1);
}
if (x >= 1 && x <= c) {
pruebalabel.setText("Párrafo: " + x);
sec--;
if (sec == 00) {
sec = 60;
min--;
}
i--;
tiemporestantelabel.setText(min + ":" + sec);
}
if (x == c + 1) {
pruebalabel.setText("Preguntas de repaso");
sec--;
if (sec == 00) {
sec = 60;
min--;
}
i--;
tiemporestantelabel.setText(min + ":" + sec);
}
if (x == c + 2) {
pruebalabel.setText("Palabras de Conclusión");
sec2--;
if (sec2 == 00) {
sec2 = 60;
min2--;
}
iii--;
tiemporestantelabel.setText(min2 + ":" + sec2);
}
if (ii < 0) {
ii = 90;
x = x + 1;
}
if (iii < 0) {
iii = 90;
x = x + 1;
}
if (i < 0) {
i = a;
x = x + 1;
min = j;
sec = y;
}
if (x == c + 3) {
timer.cancel();
pruebalabel.setText("Tiempo terminado");
}
}
}, 0, 1000);
k = k + 1;
}
java.util.Timer timer1 = new java.util.Timer();
int k1 = 1;
while (k1
<= 1) {
int a1 = 10;
timer1.scheduleAtFixedRate(new TimerTask() {
int i1 = a1;
int x1 = 1;
int sec1, min1, hor1;
public void run() {
sec1++;
if (sec1 == 60) {
min1++;
sec1 = 0;
}
if (min1 == 60) {
hor1++;
min1 = 0;
sec1 = 0;
}
tiempolabel.setText(hor1 + ":" + min1 + ":" + sec1);
if (i1 < 0) {
i1 = a1;
x1 = x1 + 1;
}
}
}, 0, 1000);
k1 = k1 + 1;
}
}
};
private void iniciarActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
acciones.actionPerformed(evt);
}
private void parrafosActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
private void stopActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
}
Valora esta pregunta


0