我使用此Main.java
文件启动我的应用程序:
package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
public class Main extends Application {
Parent root;
@Override
public void start(Stage stage) throws Exception {
root = FXMLLoader.load(getClass().getResource("../fxml/mainpage.fxml"));
Scene scene = new Scene(root);
stage.setTitle("Smart City - Main Page");
stage.setScene(scene);
stage.setMaximized(true);
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
我还创建了一个名为ScreenChanger.java
的小组:
package utils;
import java.io.IOException;
import javafx.event.Event;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class ScreenChanger {
public static void screenChanger(Event event, String path, String title) throws IOException{
Parent root = FXMLLoader.load(getClass().getResource(path));
Scene scene = new Scene(root);
Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
stage.setScene(scene);
stage.setTitle(title);
stage.setMaximized(false);
stage.setMaximized(true);
stage.show();
}
}
问题是,当我想在场景之间切换时,有时我的代码中没有Event
。我想用其他东西而不是Stage stage = (Stage) ((Node) event.getSource()).getScene().getWindow();
来改变屏幕。