我正在使用 SceneBuilder 制作项目的 GUI。我有 JavaFx 知识,但 FXML 知识很少。我已经构建了我的父窗口、我的 Controller 类和我的主类。我的父窗口 AdminView 应该有一个可通往另一个窗口的功能按钮。但是,我在显示父窗口时遇到了麻烦。
我认为这可能是库或 URL 引用的问题。但我对这个话题了解不够,不知道该怎么做。
我的主要类(class):
@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);
}
}
我的 Controller :
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):
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)
最佳答案
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 添加到 Pane ,请使用此选项:
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();
}
}
然后这是我的 Controller :
public class Controller {
@FXML
Pane pane;
@FXML
Button button;
@FXML
private void buttonOnClicked() {
doSomething()
}
}
为此,您必须在场景构建器中的“代码”下添加方法名称。
希望可以帮到你!
关于java - 如何使我的 FXML 文件显示在窗口上?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56425351/