Java Swing-将一个活动从孩子转到父母而没有存储父母的孩子

发布于 2025-01-25 12:43:12 字数 760 浏览 1 评论 0原文

我正在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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文