-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.java
107 lines (90 loc) · 2.62 KB
/
App.java
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
import java.awt.event.*;
import java.nio.file.Path;
import java.awt.*;
import java.util.ArrayDeque;
import java.util.List;
import java.util.Queue;
import java.util.Random;
import javax.swing.*;
/**
* App
*/
public class App implements KeyListener{
private static JFrame jFrame ;
private static JPanel jp ;
private static JLabel label ;
private static Queue<Integer> qIndex;
private static Queue<String> qString;
private static JTextField text;
String res = "";
public App(){
qIndex = new ArrayDeque<>();
qString = new ArrayDeque<>();
}
public static void main(String[] args) {
new App();
jFrame = new JFrame();
jp= new JPanel(new GridLayout(5, 5, 5, 5));
label = new JLabel();
text = new JTextField(10);
App a = new App();
text.addKeyListener(a);
label.setText(generateLabel());
jp.setBorder(BorderFactory.createEmptyBorder(30, 30, 10, 10));
jp.add(label);
jp.add(text);
jFrame.add(jp,0);
jFrame.setBackground(Color.LIGHT_GRAY);
jFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jFrame.pack();
jFrame.setVisible(true);
}
@Override
public void keyPressed(KeyEvent e) {
char ch = e.getKeyChar();
if(ch != 32 ){
if(ch==8){
StringBuilder sb = new StringBuilder(res);
try{
sb.setLength(sb.length() - 1);
}catch (Exception s){
return;
}
res = sb.toString();
return;
}
else{
res+= e.getKeyChar();
System.out.println(res);
}
}
else{
if(res.equals(qString.peek())){
System.out.println("Same");
qString.remove();
text.setText();
res = "";
}
}
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
//
}
private static String generateLabel() {
String para = "";
int index;
List<String> list = new EnglishWords(Path.of("words_alpha.txt"));
Random r = new Random();
for(int i=0;i<10;i++){
index = r.nextInt(370106);
qIndex.add(index);
qString.add(list.get(qIndex.remove()));
para+=list.get(index)+" ";
}
return para;
}
}