有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

如何使用“后退”按钮和不同的控制器返回上一页?JAVAFX,JAVA

我对JAVAFX相当陌生,所以请容忍我。非常感谢您的帮助。首先,我有一个加载附加项的主应用程序。fxml和addItemsController

public class UncookedApp extends Application {

private Stage stage;
@Override
public void start(Stage primaryStage) {
    stage = primaryStage;

    try {
        FXMLLoader loader=new FXMLLoader();
        loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
        AnchorPane root=loader.load();
        addItemsController itemsCtrl=loader.getController();
        itemsCtrl.setMainApp(this);
        Scene scene=new Scene(root,1024,768);
        primaryStage.setScene(scene);
        primaryStage.show();
    } catch(Exception e){
        e.printStackTrace();
    }
}

public Stage getStage() {
    return stage;
}

public static void main(String[] args) {
    launch(args);
}
}

这是addItemsController的一部分:

private UncookedApp mainApp;

public UncookedApp getMainApp() {
    return mainApp;
}
public void setMainApp(UncookedApp mainApp) {
    this.mainApp = mainApp;
}
public void toMeat(ActionEvent event) {
    Stage stage1=mainApp.getStage();
    try {
        FXMLLoader loader=new FXMLLoader();
          loader.setLocation(getClass().getResource("/uncooked/view/addMeat.fxml"));
        Parent root=loader.load();
        addMeatCtrl itemsCtrl=loader.getController();
    //  itemsCtrl.setMainApp(this);
        Scene scene=new Scene(root,1024,768);
        stage1.setScene(scene);
        stage1.show();
    } catch(Exception e){
        e.printStackTrace();
    }

}

addItemsController将加载另一个页面addMeat。我让它起作用了,但当我试着用按钮手柄从addMeat返回addItems时,它不起作用。这与检索mainApp的后台有关吗?我怎样才能解决这个问题

这就是我尝试过的

public void handleBackFromMeat(ActionEvent event) {
try{
    Stage stage=mainApp.getStage();
    FXMLLoader loader=new FXMLLoader();
    loader.setLocation(getClass().getResource("/uncooked/view/addItems.fxml"));
    Parent root=loader.load();
    addItemsController itemsCtrl=loader.getController();
    Scene scene=new Scene(root,1024,768);
    stage.setScene(scene);
    stage.show();
}catch (Exception e){
    e.printStackTrace();
}


}

共 (1) 个答案

  1. # 1 楼答案

    我也发布了一个类似的问题,然后我继续我的方式

    I need to create a back button functionality in javafx?

    你可以像我对我的应用程序所做的那样,在应用程序的开头创建两个静态列表,然后用Stage和Fxml页面名称填充它(如果你使用Fxml),然后在每个页面上使用一个中央后退按钮方法,通过迭代添加到列表中的最后一项来移动上一页

    主类中的两个静态列表

    public static List<Stage>stageval = new ArrayList<Stage>();
    public static List<String>fxmlval = new ArrayList<String>();
    

    每次切换fxml视图时,我都会将stage和fxml文件名添加到各自的列表中

    fxmlval.add("Instance.fxml");                                   
    stage = (Stage)validate.getScene().getWindow();
    stageval.add((Stage) delete.getScene().getWindow());
    

    还有一个中央后退按钮方法,当用户点击后退按钮时,我迭代到两个列表的最后一项,然后在更改阶段后从列表中删除值

    public class BackButton {
    
        public void back(String instance,Stage stage) throws IOException{
                        Parent parent = FXMLLoader.load(getClass().getResource(instance));
                        Scene scene = new Scene(parent);
                        scene.getStylesheets().add("/css/Style.css");
                        stage.setResizable(false);
                        stage.setScene(scene);
                        stage.show();
        }
    }
    
    @FXML
    public void back() throws IOException {
        BackButton bb = new BackButton();
    
        bb.back(fxmlval.get(fxmlval.size() - 1),        
        stageval.get(stageval.size() - 1));
        fxmlval.remove(fxmlval.size() - 1);
        stageval.remove(stageval.size() - 1);
    }
    

    希望这有助于解决问题,但解决方案是可行的