Skip to content

Commit 5f4d0ea

Browse files
author
Karine-Codes
committed
feat: Implementa lógica do botão Iniciar/Reiniciar
1 parent 6bdb8c1 commit 5f4d0ea

File tree

1 file changed

+46
-3
lines changed

1 file changed

+46
-3
lines changed

TypingSpeedTestGUI.java

Lines changed: 46 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,13 @@
77
import java.awt.BorderLayout;
88
import java.awt.FlowLayout;
99
import java.awt.Font;
10+
import java.awt.event.ActionListener;
11+
import java.awt.event.ActionEvent;
12+
import java.util.Random;
1013

11-
public class TypingSpeedTestGUI extends JFrame {
14+
15+
16+
public class TypingSpeedTestGUI extends JFrame implements ActionListener {
1217

1318
JLabel labelFraseParaDigitar;
1419
JLabel labelResultadosWPM;
@@ -19,7 +24,17 @@ public class TypingSpeedTestGUI extends JFrame {
1924

2025
JButton botaoIniciar;
2126

22-
String fraseDeTesteInicial = "A mãe é muito braba sendo GP.";
27+
private String[] frases = {
28+
"A rápida raposa marrom saltou sobre o cão preguiçoso.",
29+
"O sol brilha intensamente no céu azul e claro.",
30+
"Programar em Java pode ser desafiador, mas é muito recompensador.",
31+
"A persistência é o caminho do êxito.",
32+
"O único homem que nunca comete erros é aquele que nunca faz nada."
33+
};
34+
35+
private Random random = new Random();
36+
37+
private String fraseAtual = "";
2338

2439
public TypingSpeedTestGUI() {
2540

@@ -28,7 +43,7 @@ public TypingSpeedTestGUI() {
2843
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
2944
setLayout(new BorderLayout(10, 10));
3045

31-
labelFraseParaDigitar = new JLabel("<html><p style='padding: 5px;'>" + fraseDeTesteInicial + "</p></html>");
46+
labelFraseParaDigitar = new JLabel("<html><p style='padding: 5px;'>Clique em 'Iniciar' para começar! </p></html>");
3247
labelFraseParaDigitar.setFont(new Font("Arial", Font.PLAIN, 18));
3348
add(labelFraseParaDigitar, BorderLayout.NORTH);
3449

@@ -47,6 +62,8 @@ public TypingSpeedTestGUI() {
4762

4863
botaoIniciar = new JButton("Iniciar Teste");
4964

65+
botaoIniciar.addActionListener(this);
66+
5067
painelSul.add(botaoIniciar);
5168
painelSul.add(labelResultadosWPM);
5269
painelSul.add(labelResultadosPrecisao);
@@ -56,6 +73,32 @@ public TypingSpeedTestGUI() {
5673

5774
}
5875

76+
@Override
77+
public void actionPerformed(ActionEvent e) {
78+
79+
if (e.getSource() == botaoIniciar) {
80+
81+
iniciarNovoTeste();
82+
}
83+
}
84+
85+
private void iniciarNovoTeste() {
86+
87+
int indiceAleatorio = random.nextInt(frases.length);
88+
fraseAtual = frases[indiceAleatorio];
89+
90+
labelFraseParaDigitar.setText("<html><p style='paddinh: 5px; '>" + fraseAtual + "</p></html>");
91+
92+
botaoIniciar.setText("Reiniciar Teste");
93+
94+
labelResultadosWPM.setText("WPM: --");
95+
labelResultadosPrecisao.setText("Precisão: --%");
96+
labelResultadosErros.setText("Erros: --");
97+
98+
areaDeDigitacao.setText("");
99+
100+
areaDeDigitacao.requestFocusInWindow();
101+
}
59102
public static void main(String[] args) {
60103

61104
SwingUtilities.invokeLater(new Runnable() {

0 commit comments

Comments
 (0)