📄 buttoninputmapexample3.java
字号:
package JFCBook.Chapter5.jdk13;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class ButtonInputMapExample3 extends JFrame {
public ButtonInputMapExample3() {
super("Button Input Map Example 3");
JButton button = new JButton("PRESS");
getContentPane().add(button, BorderLayout.CENTER);
// Add InputMap entries for the BACKSPACE key
InputMap whenFocused = button.getInputMap(JComponent.WHEN_FOCUSED);
whenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0, false),
"pressed");
whenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_BACK_SPACE, 0, true),
"released");
// Mask the UI mappings for the SPACE key
Object dummy = new Object();
whenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, false), dummy);
whenFocused.put(KeyStroke.getKeyStroke(KeyEvent.VK_SPACE, 0, true), dummy);
// Print the maps
printActionMap(button.getActionMap());
printInputMap(button.getInputMap(JComponent.WHEN_FOCUSED),
"Input map used when focused");
printInputMap(button.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT),
"Input map used when ancestor of focused component");
printInputMap(button.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW),
"Input map used when in focused window");
}
public static void printActionMap(ActionMap actionMap) {
System.out.println("Button action map:");
Object[] keys = actionMap.allKeys();
if (keys != null) {
for (int i = 0; i < keys.length; i++) {
Object key = keys[i];
Action targetAction = actionMap.get(key);
System.out.println("\tName: <" + key + ">, action: "
+ targetAction.getClass().getName());
}
}
}
public static void printInputMap(InputMap inputMap, String heading) {
System.out.println("\n" + heading + ":");
KeyStroke[] keys = inputMap.allKeys();
if (keys != null) {
for (int i = 0; i < keys.length; i++) {
KeyStroke key = keys[i];
Object actionName = inputMap.get(key);
System.out.println("\tKey: <" + key + ">, action name: "
+ actionName);
}
}
}
public static void main(String[] args) {
JFrame f = new ButtonInputMapExample3();
f.pack();
f.setVisible(true);
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -