我有一個Utility
類,其中我試圖創建一個名爲switchScene
的static
方法,以便能夠輕鬆切換場景中的場景。這是我嘗試使用代碼:JavaFX問題切換場景
public class Utility {
public static void switchScene(String path) {
Stage stage = getMainStage(); // Assume this returns the primary stage
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource(path));
try {
Parent root = loader.load();
Controller controller = loader.getController();
controller.start();
Scene scene = new Scene(root);
stage.setScene(scene);
} catch (IOException e) {
e.printStackTrace();
}
}
}
我想這個代碼將工作(我正在盡一切控制器我認爲實現Controller
接口,只是上有一個start
方法),但是我得到此錯誤消息:
java: non-static method getClass() cannot be referenced from a static context
有什麼我可以做些什麼來解決這個問題?
非常感謝,特別是關於使用良好做法的提示! – saadq
不客氣。我很高興它有幫助。 – Omid