JavaFX:在 UI 屏幕之间导航的最佳实践
JavaFX: Best practice for navigating between UI screens
我想将 UI 屏幕从 login.fxml
更改为 home.fxml
。
我应该更改 Stage
还是 Scene
?我不确定哪个是最佳做法?
另外,我可以在控制器中为处理程序使用 lambda 表达式吗?
首先,让我们从 Stage
.vs. 开始。 Scene
问题:
众所周知,JavaFX
层次结构基于:Stage
-> Scene
-> Nodes
(等)。
看这里:
实际上,我认为的经验法则是未来:
如果您打算在程序流程中转到另一个 位置 (例如,登录 -> 配置文件)- 更改 Stage
。
如果您处于相同的环境(首次登录-> 多次错误尝试后登录)-更改Scene
。
至于 lambdas:啊嗯……如果你当前的 Java
/JavaFX
版本有能力 - 没有理由不使用。
有关控制器处理程序的更多信息,请访问
Java8、看到这个great tutorial.
我在 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) { }
所以基本上当您单击按钮时,它会将实际显示的 Stage
对象保存到 stage
变量中。然后它将新的 Scene
对象从 .fxml 文件加载到 myNewScene
变量中,然后将这个新加载的 Scene
对象放入您保存的 Stage
对象中。
使用此代码,您可以制作基本的三按钮菜单,其中每个按钮切换到不同的场景,仅使用单个 Stage
对象。
我想将 UI 屏幕从 login.fxml
更改为 home.fxml
。
我应该更改 Stage
还是 Scene
?我不确定哪个是最佳做法?
另外,我可以在控制器中为处理程序使用 lambda 表达式吗?
首先,让我们从 Stage
.vs. 开始。 Scene
问题:
众所周知,JavaFX
层次结构基于:Stage
-> Scene
-> Nodes
(等)。
看这里:
实际上,我认为的经验法则是未来:
如果您打算在程序流程中转到另一个 位置 (例如,登录 -> 配置文件)- 更改
Stage
。如果您处于相同的环境(首次登录-> 多次错误尝试后登录)-更改
Scene
。
至于 lambdas:啊嗯……如果你当前的 Java
/JavaFX
版本有能力 - 没有理由不使用。
有关控制器处理程序的更多信息,请访问
Java8、看到这个great tutorial.
我在 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) { }
所以基本上当您单击按钮时,它会将实际显示的 Stage
对象保存到 stage
变量中。然后它将新的 Scene
对象从 .fxml 文件加载到 myNewScene
变量中,然后将这个新加载的 Scene
对象放入您保存的 Stage
对象中。
使用此代码,您可以制作基本的三按钮菜单,其中每个按钮切换到不同的场景,仅使用单个 Stage
对象。