JavaFX新场景或新窗格 [英] JavaFX new scene or new pane

查看:217
本文介绍了JavaFX新场景或新窗格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找组织申请的一些建议。

I'm looking for some kind of advice for organizing an application.

当你有一个包含内容选择的主菜单时,你正在按按钮,获取控制器有2个选项:
a)更改当前主菜单显示的内容
b)创建一个新场景并将舞台切换到那个

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

你在用什么?有没有什么样的最佳实践?

What are you using? Is there any kind of best practice?

目前我更喜欢a)因为我可以保持事物状态等等,但这是干净的方式?

At the moment I'm prefering a) because I can keep things linke statusbar and such, but is this the "clean" way?

推荐答案

首先要考虑一些事项:

1)更改场景将取消链接您附加到它的事件处理程序,也可能是您提到的状态栏。

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.

2)Java是一种高度面向对象的语言,这也意味着更改在可能的情况下,在不同对象之间使用多态性是一种很好的做法。

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.

我的经验与事件处理程序有关,因为它们经常链接到场景,除非它们链接到特定节点(按钮),但可能还有其他类似的概念需要考虑。在我做的一个项目中,我在菜单,暂停,游戏和游戏之间进行了大量的更改,但由于我主要使用相同的键并且鼠标几乎没有涉及太多,我意识到我没有从创建新事件处理程序中获益用于单独的场景对象。相反,我使用 Scene.setRoot(); 方法来更改活动显示节点/窗格。

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. Instead I used the Scene.setRoot(); method to change the active display Node/Pane.

但是,如果您的处理方式差别很大,例如从带有按钮和文本字段的菜单进入FPS游戏,其中每个输入都有不同于菜单的效果,那么对事件进行不同的覆盖可能会更好两个不同的Scene对象中的处理程序,而是使用 Stage.setScene(); 方法,从而使处理程序执行多态*而不是在处理程序中使用另一组控件结构当你不在菜单中时,CPU会为每一帧工作。

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(); 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.

*我知道当我说多态时我并不是指继承多态,但它是在你可能希望获得更好性能的情况下,我能想到描述避免额外运行时代码的最好方法。

*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.

这篇关于JavaFX新场景或新窗格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆