-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKeyDemo.java
More file actions
41 lines (31 loc) · 798 Bytes
/
KeyDemo.java
File metadata and controls
41 lines (31 loc) · 798 Bytes
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
import java.awt.*;
import java.awt.event.*;
public class KeyDemo extends Frame implements KeyListener {
Label l;
KeyDemo() {
// Create label
l = new Label("Press any key...");
l.setBounds(50, 100, 200, 30);
// Add label to frame
add(l);
// Add KeyListener
addKeyListener(this);
// Frame settings
setSize(300, 300);
setLayout(null);
setVisible(true);
}
// When key is pressed
public void keyPressed(KeyEvent e) {
l.setText("Key Pressed: " + e.getKeyChar());
}
public void keyReleased(KeyEvent e) {
// optional
}
public void keyTyped(KeyEvent e) {
// optional
}
public static void main(String[] args) {
new KeyDemo();
}
}