2

我想创建一个简单的测试应用程序,其中包含一个显示一些 HTML 内容的JEditorPane和一个使所选文本变为粗体的按钮。HTMLEditorKit已经为这个按钮提供了必要的操作,所以我可以用这样的复杂代码来使用它:

// Build action map
Map<String, Action> actionMap = new HashMap<String, Action>();
for (Action action: editor.getActions())
    actionMap.put("" + action.getValue(Action.NAME), action);

// Get the action
Action action = actionMap.get("font-bold");

但我很确定有什么问题。我不敢相信我必须创建这个辅助地图才能按名称获取操作。我发现用于检索动作的唯一官方方法似乎是JEditorPane的getActions()方法,它只返回一个数组。

那么有没有更好的方法来从 an 中获取特定操作,EditorKit而我只是错过了它,或者它只是糟糕的 API 设计?

4

1 回答 1

5

你为什么要填写自己的地图?

editor.getActionMap().allKeys()
editor.getActionMap().get("font-bold")
于 2011-12-09T10:28:36.743 回答