存储具有多个阶段的动态屏幕
Store dynamic screen with multiple stages
我有 2 个不同的屏幕,在第一个屏幕中,用户可以加载图像,另一个只是一个按钮(舞台是不可见的,所以舞台必须与屏幕 1 不同)用于回到第一阶段。问题是当我从屏幕 2 返回到屏幕 1 时,我不知道如何保持图像加载(当我进入第 2 阶段时我隐藏第 1 阶段)。这是我目前在两个屏幕上的方法。
@FXML
private void goToScreen1(ActionEvent event) throws Exception{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
Stage primaryStage = new Stage();
primaryStage.setResizable(true);
primaryStage.setOpacity(0.0);
primaryStage.show();
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setHeight(primScreenBounds.getHeight());
primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}
如果 primaryStage
是 "user can load images" 做 :
static Stage primaryStage;
@FXML
private void goToScreen1(ActionEvent event) throws Exception{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
if(primaryStage==null)
primaryStage = new Stage();
primaryStage.setResizable(true);
primaryStage.setOpacity(0.0);
primaryStage.show();
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setHeight(primScreenBounds.getHeight());
primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}
您必须存储对阶段内容的引用,因此您不必重新实例化它。
一种可能是将它存储在 mainController 中,并为您的 subController 提供一个 EventHandler,以切换舞台
public interface ScreenController {
void setOnSwitchStage(EventHandler<ActionEvent> handler);
}
public class Screen1Controller implements Initializable, ScreenController {
@FXML
private Button btnSwitchScreen
public Screen1Controller() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@Override
public void setOnSwitchStage(EventHandler<ActionEvent> handler) {
btnSwitchScreen.setOnAction(handler);
}
}
public class YourApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage secondaryStage = new Stage();
initScreen(primaryStage, secondaryStage, "screen1.fxml");
initScreen(secondaryStage, primaryStage, "screen2.fxml");
primaryStage.show();
}
private void initScreen(Stage parentStage, Stage nextStage, String resource) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
Scene scene = new Scene(loader.load());
parentStage.setScene(scene);
ScreenController screenController = loader.getController();
screenController.setOnSwitchStage(evt -> {
nextStage.show();
parentStage.hide();
});
}
public static void main(String[] args) {
launch(args);
}
}
我有 2 个不同的屏幕,在第一个屏幕中,用户可以加载图像,另一个只是一个按钮(舞台是不可见的,所以舞台必须与屏幕 1 不同)用于回到第一阶段。问题是当我从屏幕 2 返回到屏幕 1 时,我不知道如何保持图像加载(当我进入第 2 阶段时我隐藏第 1 阶段)。这是我目前在两个屏幕上的方法。
@FXML
private void goToScreen1(ActionEvent event) throws Exception{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
Stage primaryStage = new Stage();
primaryStage.setResizable(true);
primaryStage.setOpacity(0.0);
primaryStage.show();
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setHeight(primScreenBounds.getHeight());
primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}
如果 primaryStage
是 "user can load images" 做 :
static Stage primaryStage;
@FXML
private void goToScreen1(ActionEvent event) throws Exception{
Stage stage = (Stage) showStage.getScene().getWindow();
FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/sample.fxml"));
Parent root = fxmlLoader.load();
if(primaryStage==null)
primaryStage = new Stage();
primaryStage.setResizable(true);
primaryStage.setOpacity(0.0);
primaryStage.show();
primaryStage.setX(0);
primaryStage.setY(0);
primaryStage.setHeight(primScreenBounds.getHeight());
primaryStage.setWidth(primScreenBounds.getWidth() / 2);
}
您必须存储对阶段内容的引用,因此您不必重新实例化它。 一种可能是将它存储在 mainController 中,并为您的 subController 提供一个 EventHandler,以切换舞台
public interface ScreenController {
void setOnSwitchStage(EventHandler<ActionEvent> handler);
}
public class Screen1Controller implements Initializable, ScreenController {
@FXML
private Button btnSwitchScreen
public Screen1Controller() {
}
@Override
public void initialize(URL location, ResourceBundle resources) {
}
@Override
public void setOnSwitchStage(EventHandler<ActionEvent> handler) {
btnSwitchScreen.setOnAction(handler);
}
}
public class YourApp extends Application {
@Override
public void start(Stage primaryStage) throws Exception {
Stage secondaryStage = new Stage();
initScreen(primaryStage, secondaryStage, "screen1.fxml");
initScreen(secondaryStage, primaryStage, "screen2.fxml");
primaryStage.show();
}
private void initScreen(Stage parentStage, Stage nextStage, String resource) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource(resource));
Scene scene = new Scene(loader.load());
parentStage.setScene(scene);
ScreenController screenController = loader.getController();
screenController.setOnSwitchStage(evt -> {
nextStage.show();
parentStage.hide();
});
}
public static void main(String[] args) {
launch(args);
}
}