-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFrameMain.java
executable file
·120 lines (100 loc) · 3.42 KB
/
FrameMain.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
108
109
110
111
112
113
114
115
116
117
118
119
120
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class FrameMain extends JFrame implements KeyListener{
/** */
private static final long serialVersionUID = 1L;
private void debug(String information){
System.out.println("DEBUG: "+information);
}
private void error(String information){
System.out.println("ERROR: "+information);
}
public static void main(String[] args){
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
new FrameMain("Frame main");
}
});
}
private JTextField field_edit_main;
public FrameMain(String caption){
super(caption);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setSize(400,400);
this.getContentPane().add(initComponents());
this.getRootPane().addKeyListener(this);
// add listener before all event
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(new KeyEventDispatcher(){
@Override
public boolean dispatchKeyEvent(KeyEvent e) {
if(e.getID()==KeyEvent.KEY_RELEASED){
if(e.getKeyCode()==KeyEvent.VK_ENTER){
System.out.println(">>>>");
}
System.out.println("dispatchKeyEvent:"+e.getKeyChar());
}
// try changing this return value
return true;
}
});
// add listener after all event
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventPostProcessor(new KeyEventPostProcessor(){
@Override
public boolean postProcessKeyEvent(KeyEvent e) {
if(e.getID()==KeyEvent.KEY_TYPED){
System.out.println("postProcessKeyEvent:"+e.getKeyChar());
}
return false;
}
});
this.pack();
this.setVisible(true);
}
private JPanel initComponents(){
JPanel return_value=new JPanel(new BorderLayout());
JLabel label=new JLabel("this is example for KeyListener across another component's");
return_value.add(label,BorderLayout.NORTH);
JPanel panel_edit=new JPanel(new BorderLayout());
panel_edit.setBorder(javax.swing.BorderFactory.createTitledBorder("another text"));
JTextField field_edit=new JTextField();
field_edit.addKeyListener(new KeyListener(){
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
}
});
panel_edit.add(field_edit,BorderLayout.NORTH);
JButton decorate_button=new JButton("decorate button");
decorate_button.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent e){
JOptionPane.showMessageDialog(FrameMain.this, "show, how get KeyEvent");
}
});
panel_edit.add(decorate_button,BorderLayout.CENTER);
return_value.add(panel_edit,BorderLayout.CENTER);
JPanel panel_edit_main=new JPanel(new BorderLayout());
panel_edit_main.setBorder(javax.swing.BorderFactory.createTitledBorder("text main"));
field_edit_main=new JTextField();
panel_edit_main.add(field_edit_main, BorderLayout.SOUTH);
return_value.add(panel_edit_main,BorderLayout.SOUTH);
return return_value;
}
@Override
public void keyPressed(KeyEvent e) {
}
@Override
public void keyReleased(KeyEvent e) {
}
@Override
public void keyTyped(KeyEvent e) {
debug("KeyTyped:"+e.getKeyChar());
}
}