简体   繁体   English

Java Swing 不加延长

[英]Java Swing without extending

I am trying to make my first 2D game in Java, and I am stuck at the very last thing.我正在尝试在 Java 中制作我的第一个 2D 游戏,但我被困在最后一件事上。
I have already done all the logic in game, and so do I have a class named World which holds all the information about the world, makes turns.我已经完成了游戏中的所有逻辑,所以我有一个名为World的 class ,它包含有关世界的所有信息,轮流。
The problem occurs when I am trying to implement some methods to show world in GUI - by that I mean: I have my parent class: World , and then I have child classes WorldGraphic , where i want to show a beautiful world, with images etc, and WorldStrategy where it is mentioned to show only the most important aspects(not so beautiful but much more 'clear' i would say), and I would like them to be swapped in UI.当我尝试实现一些在 GUI 中显示世界的方法时会出现问题 - 我的意思是:我有我的父母 class: World ,然后我有子类WorldGraphic ,我想在其中展示一个美丽的世界,带有图像等, 和WorldStrategy ,其中提到它只显示最重要的方面(不是那么漂亮,但我会说更“清晰”),我希望它们在 UI 中被交换。
And i have decided to choose Java Swing library to do it, but I have came across a big problem - I cannot inherit both World and JFrame(which was neccessary to do GUI in my swing guide), Is there any way to omit it? And i have decided to choose Java Swing library to do it, but I have came across a big problem - I cannot inherit both World and JFrame(which was neccessary to do GUI in my swing guide), Is there any way to omit it?
My best idea so far was to make public function in my world that would output the data to print, but I have doubts about it effectiveness, I would like to keep it private(protected), and do it somehow on inheritance basis, as my GUI will also have some other options(probably I would have to make a lot public methods)到目前为止,我最好的想法是在我的世界中公开 function 将 output 数据打印,但我怀疑它的有效性,我想将其保密(受保护),并以 Z5FED1ZF4004 为基础做它GUI 还会有一些其他的选项(可能我必须做很多公共方法)

My question is more about how should the structure of my document look like(so the code is probably useless).我的问题更多是关于我的文档的结构应该如何(所以代码可能没用)。 As i have already mentioned I am fairly new to Java and I am not sure if it is correct to suddenly make every method public just so ONE of my classes(the GUI actually) could use them.正如我已经提到的,我对 Java 相当陌生,我不确定突然公开每个方法是否正确,以便我的一个类(实际上是 GUI)可以使用它们。

You have several questions in your one question, which is not fully allowed here, but regardless, let me try to approach the main ones您的一个问题中有几个问题,这里不完全允许,但无论如何,让我尝试处理主要问题

  • First of all, regarding the need to extend JFrame: This is absolutely not necessary and in fact, is usually not recommended.首先,关于需要扩展JFrame:这绝对没有必要,实际上通常不推荐。 Much better is for you to use JFrames (and other GUI component) where needed, but not extend them unless necessary.更好的是在需要时使用 JFrames(和其他 GUI 组件),但除非必要,否则不要扩展它们。 Extend the component if you are extending its underlying behaviors, in other words if you're overriding a method such as a JPanel's protected void paintComponent(Graphics g) method.如果要扩展组件的底层行为,也就是要覆盖诸如 JPanel 的protected void paintComponent(Graphics g)方法之类的方法,请扩展组件。
  • Should WorldGraphic and WorldStrategy both extend World? WorldGraphic 和 WorldStrategy 都应该扩展 World 吗? I'm not sure that this is needed or desired.我不确定这是需要或想要的。 It sounds as if you're trying to separate your program's GUI portion, its "view", from its logic, the "model", and if so, both aspects do not need to and probably shouldn't extend from a parent class, but rather should be connected by a controller class.听起来好像您正在尝试将程序的 GUI 部分(其“视图”)与其逻辑“模型”分开,如果是这样,这两个方面都不需要也可能不应该从父 class 扩展,而是应该通过 controller class 连接。
  • Regarding, "As i have already mentioned I am fairly new to Java and I am not sure if it is correct to suddenly make every method public just so ONE of my classes(the GUI actually) could use them."关于, “正如我已经提到的,我对 Java 相当陌生,我不确定突然公开每个方法是否正确,以便我的一个类(实际上是 GUI)可以使用它们。” : only make a method public if it is to be called by an outside class. :仅当要由外部 class 调用时才公开方法。

Of course, for a more detailed and concrete answer, you'll probably want to post your relevant minimal reproducible example code.当然,要获得更详细和具体的答案,您可能希望发布相关的最小可重现示例代码。

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

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