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 –