简体   繁体   English

Java动作侦听器问题

[英]Java action listener question

I am creating a custom JPanel element (a login form). 我正在创建一个自定义的JPanel元素(登录表单)。

I want to allow people who use my panel to subscribe/listen to an event called "loginSuccessful". 我想允许使用我的面板的人订阅/收听名为“ loginSuccessful”的事件。

What is the best way to implement this in my JPanel object? 在我的JPanel对象中实现此目的的最佳方法是什么?

UPDATE: oh and i also want to add that when that action is triggered, i also want to return a "User" object containing the person that was just logged in 更新:哦,我还想补充一点,就是在触发该操作时,我还想返回一个“ User”对象,其中包含刚登录的人

I normally prefer EventBus for those kinds of Events. 对于这类事件,我通常更喜欢EventBus。

Library and Examples can be found here 库和示例可以在这里找到

Moreover you should consider to keep businesslogic out of your viewclass (panel) and create some kind of LoginController for your loginbusinesslogic. 此外,您应该考虑将businesslogic排除在您的视图类(面板)之外,并为您的loginbusinesslogic创建某种LoginController。 There are plenty of good examples out there. 有很多很好的例子。

EDIT: You can send an UserObject within an EventBusEvent as well. 编辑:您也可以在EventBusEvent中发送UserObject。

I would start by separating your code form widget code. 我将从分离您的代码表单小部件代码开始。 Don't extend where you don't need to. 不要扩展到不需要的地方。 Where classes are focussed on a particular job, everything becomes so much easier. 当班级专注于特定的工作时,一切变得非常容易。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM