如何使用“后退”按钮和不同的控制器返回上一页?JAVAFX,JAVA
我对JAVAFX相当陌生,所以请容忍我。非常感谢您的帮助。首先,我有一个加载addItems.fxml和addItemsController的主应用程序如何使用“后退”按钮和不同的控制器返回上一页?JAVAFX,JAVA,java,javafx,fxml,scenebuilder,Java,Javafx,Fxml,Scenebuilder,我对JAVAFX相当陌生,所以请容忍我。非常感谢您的帮助。首先,我有一个加载addItems.fxml和addItemsController的主应用程序 public class UncookedApp extends Application { private Stage stage; @Override public void start(Stage primaryStage) { stage = primaryStage; try { FXMLLoade
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();
}
}
我也发布了一个类似的问题,然后我继续我的方式 您可以像我对我的应用程序所做的那样,在应用程序开始时创建两个静态列表,然后使用Stage和Fxml页面名称(如果您使用Fxml)填充它,然后在每个页面上使用一个中央后退按钮方法,通过迭代添加到列表中的最后一项来移动上一页
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);
}
主类中的两个静态列表
public static List<Stage>stageval = new ArrayList<Stage>();
public static List<String>fxmlval = new ArrayList<String>();
还有一个中央后退按钮方法,当用户单击后退按钮时,我迭代到两个列表的最后一项,然后在更改阶段后从列表中删除值
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);
}
希望这有助于解决问题,但解决方案可行。切换到“添加肉”页面的代码在哪里?你也可以把你想返回上一页的部分贴出来吗?你现在发布的代码与我上面提到的部分无关,“它不工作”是什么意思?会发生什么?您可以看看哪种方法是合理的(它比将全局数据存储在
公共静态
列表中要好得多)。