-
Notifications
You must be signed in to change notification settings - Fork 0
/
Swing16.java
48 lines (36 loc) · 1.19 KB
/
Swing16.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
import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
public class Swing16 {
public static void main(String[] args) {
MyFrame frame = new MyFrame();
frame.setTitle("Key event");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setBounds(100,100,400,400);
frame.setVisible(true);
}
static class MyFrame extends JFrame implements KeyListener {
JTextArea ta;
MyFrame(){
Container c = getContentPane();
c.setLayout(null);
ta = new JTextArea();
ta.setBounds(10,10,365,350);
ta.addKeyListener(this);
c.add(ta);
}
@Override
public void keyTyped(KeyEvent e) {
ta.setText(ta.getText()+"\n"+"typing : "+e.getKeyChar());
}
@Override
public void keyPressed(KeyEvent e) {
ta.setText(ta.getText()+"\n"+"pressed : "+e.getKeyChar());
}
@Override
public void keyReleased(KeyEvent e) {
ta.setText(ta.getText()+"\n"+"released : "+e.getKeyChar());
}
}
}