我正在使用SceneBuilder制作项目的GUI。我有JavaFx知识,但对FXML的了解很少。我已经建立了父窗口,控制器类和主类。我的父窗口AdminView应该具有一个功能按钮,该按钮会指向另一个窗口。但是,我无法使父窗口显示出来。
我认为这可能与库或URL参考有关。但是我对这个话题还不了解,该怎么做。
我的主班:
@Override
public void start(Stage primaryStage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("/application/view/Adminview.fxml"));
primaryStage.setScene(new Scene(root));
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
我的控制器:
public abstract class AdminViewController implements Initializable {
@FXML
private void Button(ActionEvent event) {
{
try {
Parent AdminViewParent = FXMLLoader.load(getClass().getResource("/application/view/Addhotel.fxml"));
Scene AdminViewScene = new Scene(AdminViewParent);
Stage window = (Stage) ((Node) event.getSource()).getScene().getWindow();
window.setScene(AdminViewScene);
window.show();
} catch (IOException e) {
System.out.println("Something went wrong.");
}
}
}
}
例外:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
at view.AdminView.start(AdminView.java:13)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
... 1 more
Exception running application view.AdminView
C:\Users\loku\AppData\Local\NetBeans\Cache\8.2\executor-snippets\run.xml:53: Java returned: 1
BUILD FAILED (total time: 2 seconds)
答案 0 :(得分:0)
public class Class extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/Folder/File.fxml"));
loader.setController(LoginController.class);
Parent parent = loader.load();
// Show the scene containing the root layout.
Scene scene = new Scene(parent);
primaryStage.setScene(scene);
primaryStage.show();
} catch (IOException e) {
e.printStackTrace();
}
}
这一直对我有用!当您要打开新窗口时,请使用它! 如果要将FXML添加到窗格中,请使用此选项:
public void showFXML() {
try {
// Load root layout from fxml file.
FXMLLoader loader = new FXMLLoader();
loader.setLocation(this.getClass().getResource("/layout/YourFXML.fxml"));
loader.setController(new YourController());
//yourController = loader.getController();
yourParent = loader.load();
//yourMainController.getPane().getChildren().clear();
yourMainController.getPane().getChildren().add(yourParent);
} catch (IOException e) {
e.printStackTrace();
}
}
然后这是我的控制器:
public class Controller {
@FXML
Pane pane;
@FXML
Button button;
@FXML
private void buttonOnClicked() {
doSomething()
}
}
为此,您必须在“代码”下的SceneBuilder中添加方法名称。
希望我能为您提供帮助!