Java事件代表Java的Swing GUI API中的GUI操作

An event in Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object. This behavior is part of Java's Event Handling mechanism and is included in the Swing GUI library. 

Java中的事件是在图形用户界面中发生某些更改时创建的对象。 如果用户单击按钮,单击组合框或在文本字段中键入字符等,那么将触发事件,并创建相关的事件对象。 此行为是Java的事件处理机制的一部分,并且包含在Swing GUI库中。

For example, let's say we have a JButton. If a user clicks on the JButton, a button click event is triggered, the event will be created, and it will be sent to the relevant event listener (in this case, the ActionListener). The relevant listener will have implemented code that determines the action to take when the event occurs. 

例如,假设我们有一个JButton 。 如果用户单击JButton,则会触发按钮单击事件,将创建该事件,并将其发送到相关的事件侦听器 (在本例中为ActionListener )。 相关的侦听器将实现代码,该代码确定事件发生时要采取的措施。

Note that an event source must be paired with an event listener, or its triggering will result in no action.

请注意,事件源必须与事件侦听器配对,否则其触发将不执行任何操作。

活动如何运作 ( How Events Work )

Event handling in Java is comprised of two key elements:

Java中的事件处理包含两个关键元素:

  • The event source, which is an object that is created when an event occurs. Java provides several types of these event sources, discussed in the section Types of Events below.

    事件源 ,是事件发生时创建的对象。 Java提供了几种类型的事件源,下面的事件类型一节中对此进行了讨论。

  • The event listener, the object that "listens" for events and processes them when they occur.

    事件监听器 ,该对象“监听”事件并在事件发生时对其进行处理。

There are several types of events and listeners in Java: each type of event is tied to a corresponding listener. For this discussion, let's consider a common type of event, an action event represented by the Java class ActionEvent, which is triggered when a user clicks a button or the item of a list. 

Java中有几种类型的事件和侦听器:每种类型的事件都与一个对应的侦听器相关联。 在此讨论中,让我们考虑一种常见的事件类型,即由Java类ActionEvent表示的动作事件 ,当用户单击按钮或列表项时会触发该事件。

At the user's action, an ActionEvent object corresponding to the relevant action is created. This object contains both the event source information and the specific action taken by the user. This event object is then passed to the corresponding ActionListener object's method:

在用户的动作处,创建了与相关动作相对应的ActionEvent对象。 该对象包含事件源信息和用户采取的特定操作。 然后将此事件对象传递给相应的ActionListener对象的方法:


 ​void actionPerform

This method is executed and returns the appropriate GUI response, which might be to open or close a dialog, download a file, provide a digital signature, or any other of the myriad actions available to users in an interface.

执行此方法并返回适当的GUI响应,该响应可能是打开或关闭对话框,下载文件,提供数字签名或界面中用户可用的任何其他多种操作。

活动类型 ( Types of Events )

Here are some of the most common types of events in Java:

以下是Java中最常见的事件类型:

  • ActionEvent: Represents a graphical element is clicked, such as a button or item in a list. Related listener: ActionListener.

    ActionEvent :表示单击的图形元素,例如列表中的按钮或项目。 相关侦听器: ActionListener。

  • ContainerEvent: Represents an event that occurs to the GUI's container itself, for example, if a user adds or removes an object from the interface. Related listener: ContainerListener.

    ContainerEvent :表示GUI容器本身发生的事件,例如,如果用户从界面添加或删除对象,则该事件。 相关的侦听器: ContainerListener。

  • KeyEvent: Represents an event in which the user presses, types or releases a key. Related listener: KeyListener.

    KeyEvent :代表用户按下,键入或释放键的事件。 相关侦听器: KeyListener。

  • WindowEvent: Represents an event relating to a window, for example, when a window is closed, activated or deactivated. Related listener: WindowListener.

    WindowEvent :表示与窗口有关的事件,例如,在关闭,激活或禁用窗口时。 相关侦听器: WindowListener。

  • MouseEvent: Represents any event related to a mouse, such as when a mouse is clicked or pressed. Related listener: MouseListener.

    MouseEvent :表示与鼠标有关的任何事件,例如单击或按下鼠标时。 相关的侦听器: MouseListener。

Note that multiple listeners and event sources can interact with one another. For example, multiple events can be registered by a single listener, if they are of the same type. This means that, for a similar set of components that perform the same type of action, one event listener can handle all the events. Similarly, a single event can be bound to multiple listeners, if that suits the program's design (although that is less common).

请注意,多个侦听器和事件源可以相互交互。 例如,如果多个事件属于同一类型,则可以由一个侦听器注册多个事件。 这意味着,对于执行相同类型操作的一组相似组件,一个事件侦听器可以处理所有事件。 同样,如果一个事件适合程序的设计,则可以将其绑定到多个侦听器(尽管这种情况不太常见)。

翻译自: https://www.thoughtco.com/event-2034091

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值