Does Java have native support for events, similar to that of C#?

前端 未结 9 1986
眼角桃花
眼角桃花 2020-12-19 03:19

I\'m a bit confused from what I\'ve heard Java doesn\'t do events.

But I know that it does GUI events.

Am I missing something? Does java have an event handli

相关标签:
9条回答
  • 2020-12-19 04:00

    From what I remember of Java (haven't worked with it in > 6 months), I don't think there are events ala .NET. Your best bet will probably be to make use of the publisher/subscriber pattern. It's pretty easy to implement and pretty reliable.

    0 讨论(0)
  • 2020-12-19 04:04

    Swing or AWT, which are the Java UI toolkits, both handle events - check out the official Swing event tutorial for examples. You will need to have written your UI in Swing of course to be able to register listeners for these events.

    0 讨论(0)
  • 2020-12-19 04:10

    You might also want to take a look at the open source EventBus library, which, contrarily to the standard publisher/subscriber pattern uses a mediator class (the event bus) in order to reduce the tight coupling required in publisher/subscriber pattern (where subscribers must know about all publishers to register to them, which is not always possible and is often cumbersome).

    I have used it in GUI applications but it can deal with any kinds of event.

    0 讨论(0)
提交回复
热议问题