简体   繁体   English

JavaFX新场景或新窗格

[英]JavaFX new scene or new pane

I'm looking for some kind of advice for organizing an application. 我正在寻找某种有关组织应用程序的建议。

When you are having a main-menu containing the content-selection, you are pressing a button, the fetching controller has 2 choices: a) change the content where currently the mainmenu is displayed b) create a new scene and switch the stage to that one 当您具有包含内容选择的主菜单时,按一个按钮,则获取控制器有两个选择:a)更改当前显示主菜单的内容b)创建一个新场景并将舞台切换到该场景一

What are you using? 你在用什么 Is there any kind of best practice? 有什么最佳实践吗?

At the moment I'm prefering a) because I can keep things linke statusbar and such, but is this the "clean" way? 目前,我更喜欢a),因为我可以保持事物链接状态栏等,但这是“干净”的方式吗?

First of all there's some things to consider: 首先,需要考虑一些事项:
1) Changing scenes will un-link event handlers that you have attached to it, and probably the "statusbar" you mentioned as well. 1)更改场景将取消链接已附加到它的事件处理程序,也可能会取消链接您提到的“状态栏”。

2) Java is a highly Object-Oriented language and that also means changing between different objects and using polymorphism is a good practice when that is possible. 2)Java是一种高度面向对象的语言,这也意味着在不同的对象之间进行更改,并且在可能的情况下使用多态是一种很好的做法。

My experience relates to event handlers as they are often linked to the scene besides when they are linked to specific nodes (buttons), but there are probably other similar concepts to consider. 我的经验与事件处理程序有关,因为事件处理程序除了链接到特定节点(按钮)外,还经常链接到场景,但是可能还需要考虑其他类似的概念。 In a project I did I was changing between menu, pause, play and game over display's a lot, but as I was using mainly the same keys and the mouse was hardly involved much I realized I had no benefit from having to create new event handlers for separate Scene objects. 在一个项目中,我经常在显示菜单,暂停,播放和游戏之间进行切换,但是由于我主要使用相同的键,而鼠标几乎不参与其中,所以我意识到我不必从创建新的事件处理程序中受益。用于单独的Scene对象。 Instead I used the Scene.setRoot(); 相反,我使用了Scene.setRoot(); method to change the active display Node/Pane. 更改活动显示节点/窗格的方法。

However, if your handling is highly different, for instance going from a menu with buttons and text fields into a FPS game where every input has different effects opposed to that of the menu(s) then it would probably be better to have different Overrides of the event handlers in two different Scene objects and instead use the Stage.setScene(); 但是,如果您的处理方式有很大不同,例如从带有按钮和文本字段的菜单进入FPS游戏,其中每个输入的效果都与菜单相反,那么最好设置不同的Overrides两个不同Scene对象中的事件处理程序,而是使用Stage.setScene(); method, thus making handlers act polymorphed* instead of having another set of control structures inside the handler for the CPU to work through for every frame when you're not in the menu. 方法,从而使处理程序采取多态处理*,而不是在处理程序内部没有另一套控制结构,以便当您不在菜单中时,CPU可以为每一帧工作。

*I know that when I say polymorphed I am not referring to inheritance polymorphism, but it was the best way I could think of describing the avoidance of extra runtime code in a case you would probably like better performance. *我知道当我说多态时,我并不是在指继承多态,但是这是我想描述避免在可能需要更好性能的情况下避免额外的运行时代码的最佳方法。

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

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