2016-03-01 118 views
0

對不起奇怪的標題,不知道究竟寫什麼。我正在使用Java8開發JavaFX應用程序。爲此,我有javafx-maven-plugincom.zenjava。我有一個簡單的測試代碼,其中有一個Main類,還有一個用於樣式的css頁面。我有沒有maven的同一個項目,它工作正常。但對於Maven,我無法運行它。我爲CSS文件獲得NPE。我已經嘗試了多個選項。你能幫忙的話,我會很高興。謝謝。JavaFX:Maven項目無法找到css文件,沒有找到Maven css文件

錯誤日誌:

Exception in Application start method 
Exception in thread "main" 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$155(LauncherImpl.java:182) 
    at java.lang.Thread.run(Thread.java:745) 
Caused by: java.lang.NullPointerException 
    at View.Main.start(Main.java:110) 
    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.gtk.GtkApplication._runLoop(Native Method) 
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$49(GtkApplication.java:139) 
    ... 1 more 

的pom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<project xmlns="http://maven.apache.org/POM/4.0.0" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 
    <modelVersion>4.0.0</modelVersion> 

    <groupId>MavenFX</groupId> 
    <artifactId>MavenFX</artifactId> 
    <version>1.0-SNAPSHOT</version> 
    <properties> 
     <maven.compiler.source>1.8</maven.compiler.source> 
     <maven.compiler.target>1.8</maven.compiler.target> 
    </properties> 
    <dependencies> 

    </dependencies> 

    <build> 
     <plugins> 
     <plugin> 

      <groupId>com.zenjava</groupId> 
      <artifactId>javafx-maven-plugin</artifactId> 
      <version>8.2.0</version> 
      <configuration> 
       <mainClass>View.Main</mainClass> 
      </configuration> 
     </plugin> 
     </plugins> 
    </build> 
</project> 

主要類:

package View; 

import javafx.application.Application; 
import javafx.geometry.Insets; 
import javafx.scene.Scene; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 
import javafx.scene.control.PasswordField; 
import javafx.scene.control.TextField; 
import javafx.scene.effect.DropShadow; 
import javafx.scene.effect.Reflection; 
import javafx.scene.layout.BorderPane; 
import javafx.scene.layout.GridPane; 
import javafx.scene.layout.HBox; 
import javafx.scene.paint.Color; 
import javafx.scene.text.Font; 
import javafx.scene.text.FontWeight; 
import javafx.scene.text.Text; 
import javafx.stage.Stage; 

public class Main extends Application { 


    String user = "JavaFX2"; 
    String pw = "password"; 
    String checkUser, checkPw; 


    @Override 
    public void start(Stage primaryStage) throws Exception{ 
     primaryStage.setTitle("Test Login"); 

     BorderPane bp = new BorderPane(); 
     bp.setPadding(new Insets(10,50,50,50)); 

     //Adding HBox 
     HBox hb = new HBox(); 
     hb.setPadding(new Insets(20,20,20,30)); 

     //Adding GridPane 
     GridPane gridPane = new GridPane(); 
     gridPane.setPadding(new Insets(20,20,20,20)); 
     gridPane.setHgap(5); 
     gridPane.setVgap(5); 

     //Implementing Nodes for GridPane 
     Label lblUserName = new Label("Username"); 
     final TextField txtUserName = new TextField(); 
     Label lblPassword = new Label("Password"); 
     final PasswordField pf = new PasswordField(); 
     Button btnLogin = new Button("Login"); 
     final Label lblMessage = new Label(); 

     //Adding Nodes to GridPane layout 
     gridPane.add(lblUserName, 0, 0); 
     gridPane.add(txtUserName, 1, 0); 
     gridPane.add(lblPassword, 0, 1); 
     gridPane.add(pf, 1, 1); 
     gridPane.add(btnLogin, 2, 1); 
     gridPane.add(lblMessage, 1, 2); 


     //Reflection for gridPane 
     Reflection r = new Reflection(); 
     r.setFraction(0.7f); 
     gridPane.setEffect(r); 

     //DropShadow effect 
     DropShadow dropShadow = new DropShadow(); 
     dropShadow.setOffsetX(5); 
     dropShadow.setOffsetY(5); 

     //Adding text and DropShadow effect to it 
     Text text = new Text("Test Login"); 
     text.setFont(Font.font("Courier New", FontWeight.BOLD, 28)); 
     text.setEffect(dropShadow); 

     //Adding text to HBox 
     hb.getChildren().add(text); 

     //Add ID's to Nodes 
     bp.setId("bp"); 
     gridPane.setId("root"); 
     btnLogin.setId("btnLogin"); 
     text.setId("text"); 

     //Action for btnLogin 
     btnLogin.setOnAction(event -> { 
      checkUser = txtUserName.getText(); 
      checkPw = pf.getText(); 
      if(checkUser.equals(user) && checkPw.equals(pw)){ 
       lblMessage.setText("Congratulations!"); 
       lblMessage.setTextFill(Color.GREEN); 
      } 
      else{ 
       lblMessage.setText("Incorrect user or pw."); 
       lblMessage.setTextFill(Color.RED); 
      } 
      txtUserName.setText(""); 
      pf.setText(""); 
     }); 

     //Add HBox and GridPane layout to BorderPane Layout 
     bp.setTop(hb); 
     bp.setCenter(gridPane); 

     //Adding BorderPane to the scene and loading CSS 
     Scene scene = new Scene(bp); 

     scene.getStylesheets().add(getClass().getClassLoader().getResource("UI/login.css").toExternalForm()); 
     primaryStage.setScene(scene); 
     primaryStage.setTitle("Test Login"); 
     /*primaryStage.titleProperty().bind(
       scene.widthProperty().asString(). 
         concat(" : "). 
         concat(scene.heightProperty().asString()));*/ 
     primaryStage.setResizable(false); 
     primaryStage.show(); 
    } 


    public static void main(String[] args) { 
     launch(args); 
    } 
} 

CSS文件:

#root { 
    -fx-background-color: linear-gradient(lightgray, gray); 
    -fx-border-color: white; 
    -fx-border-radius: 20; 
    -fx-padding: 10 10 10 10; 
    -fx-background-radius: 20; 
} 

#bp { 
    -fx-background-color: linear-gradient(gray,DimGrey); 

} 

#btnLogin { 
    -fx-background-radius: 30, 30, 29, 28; 
    -fx-padding: 3px 10px 3px 10px; 
    -fx-background-color: linear-gradient(orange, orangered); 
} 

#text { 
    -fx-fill: linear-gradient(orange , orangered); 
} 

如果我運行mvn jfx:jar,我得到了jar文件,但它也有同樣的問題。爲什麼如果這是一個Maven項目,JavaFX有問題找到文件。你能幫忙的話,我會很高興。謝謝。

回答

1

你可以試着輸出的路徑,你的資源文件:

 System.err.println(getClass().getClassLoader().getResource("UI/login.css")); 

,並看到它指向。 maven可能會改變你的資源位置。我不確定,但你可以試試。請告訴我。

+0

你的代碼直接不起作用。我必須去解決它。之後,它只是印刷正確。這有什麼幫助? –

+0

你說得對,maven感動了它。我只是將文件移動到資源,它的工作。 –

+2

完美!問題解決了 :); – beniaminp