JTextPane 内容的动作事件 [英] action event for JTextPane contents

查看:37
本文介绍了JTextPane 内容的动作事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个带有 HTMLEditorKit 的 JTextPane.我正在显示一段文本,并希望在用户单击单个单词或在突出显示单词时按下某个热键时捕获事件.例如,当突出显示一个单词并且用户按 D 时,该单词将从文本中删除.另外,如何实现自定义光标导航 - 即仅在单词边界上移动光标?

Suppose I have a JTextPane with HTMLEditorKit. I'm displaying a paragraph of text and want to capture events when user clicks on individual word or presses a certain hotkey while a word is highlighted. For example, when a word is highlighted and user presses D, the word is removed from the text. Also, how to implemented custom cursor navigation - that is, move cursor on word boundaries only?

实现这些功能的最简单方法是什么?我意识到这是很多代码,所以只需说明有用的类侦听器名称、相关方法等就足以让我继续:) 谢谢.

What would be the simplest way to implement these features? I realise this is a lot of code, so just stating useful class listener names, relevant methods, etc would be sufficient to get me going :) Thanks.

推荐答案

例如,当一个单词被高亮显示并且用户按下 D 键时,该单词就会从文本中移除

For example, when a word is highlighted and user presses D, the word is removed from the text

默认 EditorKit 已支持此功能.有关支持的绑定,请参阅键绑定.如果您愿意,该链接还会向您展示如何使用不同的 KeyStrokes 共享现有操作.

This is already supported by the default EditorKit. See Key Bindings for the supported bindings. The link also shows you how to share the existing Actions with different KeyStrokes if you wish.

如果您需要实现其他操作,那么您可以首先扩展 TextAction 以添加您的自定义功能.然后你将你的动作绑定到一个击键.

If you need to implement other Actions then you would start by extending TextAction to add your custom functionality. Then you bind your action to a key stroke.

这篇关于JTextPane 内容的动作事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆