0
时正如标题建议试图从视图中分离的逻辑的时候,我只是想分开(组织)我的项目里面的文件,我读过的一切在这之前:位置错误使用JavaFX
- How to seperate Javafx(non fxml) to a fxml and a controller? (Invocation target exception)
- Is there a way to modularize a JavaFX application?
- How to seperate logic from controller JavaFx
这是最接近的答案。
- How to separate my gui in javafx?
但它没有对我来说很清楚仍然寿,(也许这是关于我无法理解语法的Cuz的英语不是我的母语郎)。
我试图手动复制最后一个,但仍然无法确定我应该如何指向fxml文件。
这里是我的结构:
我主要的应用程序的代码:
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.stage.Stage;
/**
* Principal
* @author Adrian
*
*/
public class Login extends Application {
@Override
public void start(Stage primaryStage) {
try {
Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
Scene scene = new Scene(root,300,200);
scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
primaryStage.setTitle("Login");
primaryStage.setScene(scene);
primaryStage.show();
Image icono = new Image(getClass().getResourceAsStream("/SiLi/src/main/recursos/img/usuario.gif"));
primaryStage.getIcons().add(icono);
} catch(Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
launch(args);
}
}
的错误,我得到:
> 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 org.ajfmo.sili.core.Login.start(Login.java:19)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(LauncherImpl.java:863)
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(PlatformImpl.java:326)
at com.sun.javafx.application.PlatformImpl.lambda$null$173(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(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$148(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
你可以找到越来越多的Q/A就像你想要的。对于你的错误,你应该验证你的fxml文件的路径。 –
[使用JavaFx应用MVC]的可能副本(https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) –
请参阅https://stackoverflow.com/questions/35630415/nullpointer-location-on -javafx-fxmlloader和https://stackoverflow.com/questions/34828952/jafafx-load-fxml-file-error-null-pointer-exception –