java Swing:如何创建事件并将它们分派到组件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/808491/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 13:53:36  来源:igfitidea点击:

Swing: How to create Events and dispatch them to a component?

javaswingevents

提问by Ayman

I need to send some events to a component in Swing, so it is handled just like any user generated, standard Swing events.

我需要向 Swing 中的组件发送一些事件,因此它的处理方式与任何用户生成的标准 Swing 事件一样。

Basically, something like a macro recorder, and then executor for JEditorPane. But I need more control over the generated events.

基本上,类似于宏记录器,然后是 JEditorPane 的执行器。但我需要更多地控制生成的事件。

SO, assume I have an editor, I want to:

所以,假设我有一个编辑器,我想:

  1. capture all events sent to it, and at that point, i can store them in list, on disk, or whatver..
  2. Be able to later play those events
  3. Ideal situation, is to allow user to edit a commands which contains things like:
    "type key A, select line, delete-selection, cursor-up, line-start..."
  1. 捕获发送给它的所有事件,然后,我可以将它们存储在列表、磁盘或其他任何地方。
  2. 以后可以玩这些活动
  3. 理想的情况是允许用户编辑包含以下内容的命令:
    “键入键 A,选择行,删除选择,光标向上,行开始......”

回答by Tom Hawtin - tackline

The obvious thing to do is get the EventQueueand post events to it. That would just add the event to the queue which will get dispatched in its turn on the EDT.

显而易见的事情是获取EventQueue和发布事件给它。这只会将事件添加到队列中,该队列将在 EDT 上被调度。

java.awt.Toolkit.getDefaultToolkit().getSystemEventQueue().postEvent(event);

Someone claimed yesterday the Opera and Safari do their own thing and don't give the required permission to untrusted code.

昨天有人声称 Opera 和 Safari 做他们自己的事情并且没有给予不受信任的代码所需的权限。

A direct way to do it is to call Component.dispatchEvent.

一个直接的方法是调用Component.dispatchEvent.

回答by kdgregory

If you're looking for standard GUI events, then you want java.awt.Robot

如果您正在寻找标准的 GUI 事件,那么您需要java.awt.Robot

If you're looking to define your own events, you're going to have to subclass the GUI classes that need to receive those events (or better, create an external controller for them), define an event class, and dispatch the events. You can use java.beans.EventHandlerto dispatch, create your own handler class (I've found that more useful on occasions), or inject your events (depending on how they inherit) into the system event queue (can't find the class to do that ... I thought it was Toolkit).

如果您想定义自己的事件,您将不得不对需要接收这些事件的 GUI 类进行子类化(或者更好的是,为它们创建一个外部控制器),定义一个事件类,然后分派事件。您可以使用java.beans.EventHandler来调度、创建您自己的处理程序类(我发现这在某些情况下更有用),或将您的事件(取决于它们如何继承)注入系统事件队列(找不到类来做到这一点......我以为是Toolkit)。

However, I would do none of these. What you describe (a macro recorder) should be implemented using a controller that generates/is fed a series of application-specific action messages (look at the Command pattern).

但是,我不会做这些。您所描述的(宏记录器)应该使用控制器来实现,该控制器生成/馈送一系列特定于应用程序的操作消息(查看命令模式)。

回答by Gary Kephart

I believe that you have to construct an instance of the event and then call

我相信你必须构造一个事件的实例然后调用

java.awt.EventQueue.dispatchEvent(event)

Set the source of the event to the desired component.

将事件源设置为所需的组件。