java JavaFX:在 UI 屏幕之间导航的最佳实践
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40750526/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
JavaFX: Best practice for navigating between UI screens
提问by MordechayS
I want to change UI screens from login.fxml
to home.fxml
.
我想将 UI 屏幕从 更改login.fxml
为home.fxml
。
Should I change the Stage
or the Scene
? I'm not sure which is the best practice?
Also, can I use a lambda expression for the handler in the controller?
我应该更改Stage
还是Scene
?我不确定哪个是最佳实践?另外,我可以为控制器中的处理程序使用 lambda 表达式吗?
回答by MordechayS
First, let's start out with the Stage
.vs. Scene
issue:
首先,让我们从Stage
.vs开始。Scene
问题:
As known, the JavaFX
hierarchy is based on: Stage
-> Scene
-> Nodes
(etc).
众所周知,JavaFX
层次结构基于:Stage
-> Scene
-> Nodes
(等)。
See here:
看这里:
Practically speaking, a rule-of-thumb in my opinion is the future:
实际上,在我看来,经验法则是未来:
If you plan on going forward to a different placein the flow of your program (login -> profile, for example) - change the
Stage
.If you are in the same enviroment(login for the first time -> login after multiple wrong tries) - change the
Scene
.
如果您计划前进到程序流程中的不同位置(例如,登录 -> 配置文件) - 将
Stage
.如果您处于相同的环境中(第一次登录 -> 多次错误尝试后登录) - 更改
Scene
.
As for lambdas: Ahhmmm... if your current Java
/JavaFX
version has the abillity - there is no reason not to use.
For more about controller handlers in
Java 8, see this great tutorial.
至于 lambdas:啊哈...如果您当前的Java
/JavaFX
版本有能力 - 没有理由不使用。有关 Java 8 中的控制器处理程序的更多信息,请参阅这个很棒的教程。
回答by PeterB
I use this approach for changing scenes in JavaFX
:
我使用这种方法来改变场景JavaFX
:
/**
* Controller class for menuFrame.fxml
*/
public class MenuFrameControl implements Initializable {
@FXML private Button sceneButton1;
@FXML private Button sceneButton2;
@FXML private Button sceneButton3;
/**
* Event handling method, loads new scene from .fxml file
* according to clicked button and initialize all components.
* @param event
* @throws IOException
*/
@FXML
private void handleMenuButtonAction (ActionEvent event) throws IOException {
Stage stage = null;
Parent myNewScene = null;
if (event.getSource() == sceneButton1){
stage = (Stage) sceneButton1.getScene().getWindow();
myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene1.fxml"));
} else if (event.getSource() == sceneButton2){
stage = (Stage) flightBtn.getScene().getWindow();
myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene2.fxml"));
} else if (event.getSource() == sceneButton3) {
stage=(Stage) staffBtn.getScene().getWindow();
myNewScene = FXMLLoader.load(getClass().getResource("/mvc/view/scene3.fxml"));
}
Scene scene = new Scene(myNewScene);
stage.setScene(scene);
stage.setTitle("My New Scene");
stage.show();
}
@Override
public void initialize(URL location, ResourceBundle resources) { }
So basically when you click the button, it saves actually displayed Stage
object into stage
variable. Then it loads new Scene
object from .fxml file into myNewScene
variable and then put this fresh loaded Scene
object into your saved Stage
object.
所以基本上当你点击按钮时,它会将实际显示的Stage
对象保存到stage
变量中。然后它将Scene
.fxml 文件中的新对象加载到myNewScene
变量中,然后将这个新加载的Scene
对象放入您保存的Stage
对象中。
With this code you can make basic three button menu, where each button switch to different scene, using just single Stage
object.
使用此代码,您可以制作基本的三按钮菜单,其中每个按钮只需使用单个Stage
对象即可切换到不同的场景。