问题描述
我需要将一些事件发送给Swing中的一个组件,所以它像处理任何用户生成的标准Swing事件一样处理。
I need to send some events to a component in Swing, so it is handled just like any user generated, standard Swing events.
基本上,像宏录音机,然后执行JEditorPane。但是我需要更多地控制生成的事件。
Basically, something like a macro recorder, and then executor for JEditorPane. But I need more control over the generated events.
SO,假设我有一个编辑器,我想:
SO, assume I have an editor, I want to:
- 捕获发送给它的所有事件,在这一点上,我可以将它们存储在列表中,磁盘上或者什么。
- 能够以后玩这些事件
- 理想的情况是允许用户编辑包含以下内容的命令:
type key A,select line,delete-selection,cursor -up,line-start ...
推荐答案
显而易见的事情是get EventQueue
并发布事件。这只会将事件添加到队列中,该队列将在EDT上转发。
The obvious thing to do is get the EventQueue
and post events to it. That would just add the event to the queue which will get dispatched in its turn on the EDT.
java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);
有人昨天声称Opera和Safari做自己的事情,不给予不受信任的权限代码
Someone claimed yesterday the Opera and Safari do their own thing and don't give the required permission to untrusted code.
直接的方式是调用$ code Component.dispatchEvent 。
A direct way to do it is to call Component.dispatchEvent
.
这篇关于Swing:如何创建事件并将其发送到组件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!