Java - 如何使用 JPanel 访问/共享游戏中的数据

发布于 2024-09-25 21:33:39 字数 434 浏览 0 评论 0原文

我上周做了一个简单的java游戏。当时,一个类扩展了 Jframe 并保存了数据(通过组合由其他三个类组成)。

现在,我正在尝试制作相同的游戏,但使用多个 JPanel。一个面板将是游戏图形(基本上是旧的 JFrame 缩小为一个面板)。其他面板上也会有按钮和文本字段。

但我对程序的数据共享/访问如何工作感到困惑。我现在有一个带有 3 个子面板的 JFrame 扩展类。这些子面板之一是游戏图形,其中当前包含所有玩家数据。

1)有什么办法可以将数据保留在该面板中并仅使用 getter/setter 访问它吗? (我已经尝试过,但没能成功。)

2)游戏通常是如何布局的?例如,我是否应该将 Player 数据移动到包含所有面板的 JFrame 类,并在那里添加 getter/setter。 (理想情况下,所有面板都将使用/编辑两个玩家数组列表。)

I made a simple java game last week. At that time, one class extended Jframe and held the data (which comprised three other class, through composition)

Now, I'm trying to make the same game, but with several JPanels. One panel will be the game graphics (basically the old JFrame shrunk into one panel). And there will be buttons and text fields on other panels.

But I'm confused about how the program's data sharing/accessing will work. I now have one JFrame extending class with 3 subpanels. One of those subpanels is the game graphics, which currently has all the Player data.

1) Is there someway I can leave the data in that panel and just access it with getters/setters? (I've tried but not been able to make anything work.)

2) How are games usually laid out? Should I, for instance, move the Player data to the JFrame class which holds all the panels, and add getter/setters there. (Ideally, the two player arraylists will be used/edited by all panels.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-10-02 21:33:39

我建议您将 Player 移至 Player 类,并为每个面板提供对其的引用,并遵循大多数数据的模式。

I would recommend you move the Player to a Player class, and provide each panel a reference to it, and following that pattern for most of your data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文