📄 globalmappingexample.java
字号:
package JFCBook.Chapter5.jdk13;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GlobalMappingExample {
public static void main(String[] args) {
JFrame f = new JFrame("JDK 1.3 Global Mapping Example");
JPanel upperPane = new JPanel();
JPanel lowerPane = new JPanel();
upperPane.setBackground(Color.green);
lowerPane.setBackground(Color.yellow);
f.getContentPane().add(upperPane, BorderLayout.CENTER);
f.getContentPane().add(lowerPane, BorderLayout.SOUTH);
JButton buttonA = new JButton("A");
JButton buttonB = new JButton("B");
JButton buttonC = new JButton("C");
JButton buttonD = new JButton("D");
final JTextField tf = new JTextField(32);
upperPane.add(buttonA);
upperPane.add(tf);
lowerPane.add(buttonB);
lowerPane.add(buttonC);
lowerPane.add(buttonD);
f.pack();
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent evt) {
System.exit(0);
}
});
f.setVisible(true);
// Create actions and install them
// (1) F1 anywhere in the window
AbstractAction a1 = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
tf.setText("F1 somewhere in main window");
}
};
f.getRootPane().getActionMap().put("helpAction", a1);
// Install the mapping for this action
f.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(
KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false),
"helpAction");
//
// (2) Keys A or D when buttons B, C, D have focus
AbstractAction a2 = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
tf.setText("A released or D pressed on lower pane button");
}
};
ActionMap actionMap = lowerPane.getActionMap();
InputMap inputMap = lowerPane.getInputMap(
JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);
actionMap.put("lowerPaneAction", a2);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_A, 0, true),
"lowerPaneAction");
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_D, 0, false),
"lowerPaneAction");
// (3) F1 pressed on button in lower pane
AbstractAction a3 = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
tf.setText("F1 somewhere in lower pane");
}
};
actionMap.put("helpAction", a3);
inputMap.put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false),
"helpAction");
// (4) 'F1' only when text area focused
AbstractAction a4 = new AbstractAction() {
public void actionPerformed(ActionEvent evt) {
tf.setText("F1 pressed in the text area");
}
};
tf.getActionMap().put("helpAction", a4);
tf.getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_F1, 0, false),
"helpAction");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -