Java Swing-将一个活动从孩子转到父母而没有存储父母的孩子
我正在Java挥杆中制作游戏,并且遇到了一个问题,我需要向我的UI类发出信号mainWindow
(其中包含GameLogic
的实例)播放器已经升级,需要显示适当的UI。
正如我在上面提到的,MainWindow包含一个GameLogic
的实例,并且其名称意味着它负责更新和维护游戏。当播放器升级时,我想调用mainWindow
可以处理的事件。我该如何实现?
我想避免在GameLogic
中存储MainWindow实例的引用,因为当我使用GSON进行保存时,这可能会导致问题。我尝试简单地让GameLogic
扩展了ActionEvent,但我认为这不是正确的途径,因为我需要在GameLogic中调用
作为ActionEvent
super
,但actionEvent
需要一个非编号source
不能为gameLogic
正在构造尚不可用,但是没有其他对象
实例在那里使用是有意义的。
我想到的另一种方法是在GameLogic
中仅具有ActionEvent
的实例?
在这一点上,我意识到这可能已经做了很多事情,因此最好求助于尝试和测试的解决方案,所以我在这里。我会感谢一般建议或链接到探索和解决此特定问题的网站的链接。
I'm making a game in Java Swing and I've come across an issue where I need to signal to my UI class MainWindow
(that contains an instance of GameLogic
) that the player has leveled up and that it needs to display the appropriate UI.
As I mentioned above MainWindow contains an instance of GameLogic
and as its name implies it is responsible for updating and maintaining the game. When the player levels up I want to invoke an Event which the MainWindow
can handle. How can I make this happen?
I want to avoid storing the MainWindow instance's reference in GameLogic
as that could lead to problems when I'm deserializing a save using Gson. I tried simply having GameLogic
extend ActionEvent, but I don't think that's a correct path to take as I'd need to call ActionEvent
's constructor within GameLogic
as super
but ActionEvent
requires a non-null source
which can't be GameLogic
as it is being constructed so this
isn't yet usable, but no other Object
instance would make sense to be used there.
Another approach I thought of was just having an instance of ActionEvent
within GameLogic
but how would I set its actionListener to MainWindow
without storing a reference for it?
At this point I realized that this was probably already done a lot, so it's better to resort to tried and tested solutions, so here I am. I'd appreciate general advice or links to sites where this specific issue is explored and solved/explained.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论