⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 displaykeymaps.java

📁 一个用java开发界面的程序集(jfc核心编程)
💻 JAVA
字号:
package JFCBook.Chapter9.jdk13;

import javax.swing.*;
import javax.swing.text.*;
import java.awt.*;
import java.awt.event.*;

public class DisplayKeymaps {

	public static void printActionMap(ActionMap actionMap) {
		System.out.println("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);
			}
		}
	}

	// Static method to print a keymap, with recursion
	public static void printKeymap(Keymap m, int indent) {
		KeyStroke[] k = m.getBoundKeyStrokes();

		for (int i = 0; i < k.length; i++) {
			for (int j = 0 ; j < indent ; j++) {
				System.out.print(" ");
			}
			System.out.print("Keystroke <" + 
					KeyEvent.getKeyModifiersText(k[i].getModifiers()) 
					+ " " + k[i].getKeyCode() + "> ");
			Action a = m.getAction(k[i]);
			System.out.println((String)a.getValue(Action.NAME));
		}

		// If this keymap has a parent, print that as well
		m = m.getResolveParent();
		if (m != null) {
			printKeymap(m, indent + 2);
		}
	}

	public static void main(String[] args) {

		JTextComponent t = null;
		
		if (args.length == 1) {
			try {
				Class cl = Class.forName(args[0]);
				Object o = cl.newInstance();
				if (o instanceof JTextComponent) {
					t = (JTextComponent)o;
				} else {
					System.out.println("Class " + args[0] + " is not a JTextComponent");
					t = null;
				}
			} catch (Throwable e) {
				System.out.println("Failed to create an instance of " + args[0]);
				t = null;
			}
		}

		if (t != null) {
		   // Print the action map and input maps
			printActionMap(t.getActionMap());
			printInputMap(t.getInputMap(JComponent.WHEN_FOCUSED),
						"Input map used when focused");
			printInputMap(t.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT),
						"Input map used when ancestor of focused component");
			printInputMap(t.getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW),
						"Input map used when in focused window");

			// Get the keymap and print it, including its parent
			System.out.println("\n\nComponent keymap");
			Keymap map = t.getKeymap();
			printKeymap(map, 0);
		}

		System.exit(0);
	}


}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -