2016-12-17 87 views
-2

大家好,我正在學習javafx和我有問題來運行hello world的簡單基本程序我知道javafx工作在MVC類型結構fxml,mainclass和控制器,所以我正在做的是我剛剛在JavaFX中創建了第一個程序,其中有三件事是HelloWorld.Java,HelloWorld.fxml和HelloWorldController.java,併爲HelloWorld.java創建了com.bean包,爲HelloWorld.fxml創建了com.bean,爲HelloWorldController創建了com.gui .java但我面臨一個問題,而我運行它它顯示了一個例外,fxml沒有加載一個位置是必需的.....請幫助我也bild和清理它很多時間,但不工作.....JavaFX Netbeans FXML沒有找到

HelloWorld.java

package com.bean; 

import javafx.application.Application; 
import javafx.fxml.FXMLLoader; 
import javafx.scene.Parent; 
import javafx.scene.Scene; 
import javafx.stage.Stage; 
/* 
* To change this license header, choose License Headers in Project Properties. 
* To change this template file, choose Tools | Templates 
* and open the template in the editor. 
*/ 


/** 
* 
* @author Love Poet 
*/ 
public class HelloWorld extends Application{ 

    @Override 
    public void start(Stage stage) throws Exception { 
     Parent root=FXMLLoader.load(this.getClass().getResource("com/gui/HelloWorld.fxml")); 
     Scene scene=new Scene(root); 
     stage.setTitle("Hellow World Example"); 
     stage.setScene(scene); 
     stage.show(); 
    } 

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

HelloWorldController.java

package com.controller; 

/** 
* Sample Skeleton for 'HelloWorld.fxml' Controller Class 
*/ 

import java.net.URL; 
import java.util.ResourceBundle; 
import javafx.event.ActionEvent; 
import javafx.fxml.FXML; 
import javafx.fxml.Initializable; 
import javafx.scene.control.Button; 
import javafx.scene.control.Label; 

public class HelloWorldController implements Initializable{ 
    @FXML // fx:id="button" 
    private Button button; // Value injected by FXMLLoader 

    @FXML // fx:id="label" 
    private Label label; // Value injected by FXMLLoader 

    @FXML 
    void handleButtonAction(ActionEvent event) { 
     label.setText("Hello World !"); 
    } 

    @Override 
    public void initialize(URL url, ResourceBundle rb) { 

    } 
} 

HelloWorld.fxml

<?xml version="1.0" encoding="UTF-8"?> 

<?import java.lang.*?> 
<?import java.util.*?> 
<?import javafx.scene.*?> 
<?import javafx.scene.control.*?> 
<?import javafx.scene.layout.*?> 
<?import com.controller.*?> 
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1" fx:controller="HelloWorldController"> 
    <children> 
     <Button fx:id="button" layoutX="47.0" layoutY="129.0" onAction="#handleButtonAction" prefHeight="25.0" prefWidth="211.0" text="Click Me!" /> 
     <Label fx:id="label" layoutX="35.0" layoutY="35.0" minHeight="16" minWidth="69" prefHeight="45.0" prefWidth="274.0" /> 
    </children> 
</AnchorPane> 

爲了這個,我正在使用NetBeans我這樣的結構是: -

This is an image of netbeans project structure

回答

1

你」重新使用com.bean中的類來獲取資源,這意味着使用相對路徑com/gui/HelloWorld.fxml創建一個url,該url將指向com.bean.com.gui包中的HelloWorld.fxml

在這種情況下,您可能需要使用ClassLoader具有相同的相對路徑:

this.getClass().getClassLoader().getResource("com/gui/HelloWorld.fxml") 

或使用路徑相對於classpath根目錄

this.getClass().getResource("/com/gui/HelloWorld.fxml") 
+0

我所做的這一切,這是不是一個解決方案,我已經找到了這個問題的解決方案,我自己非常感謝您的答覆,解決方案很簡單,在netbeans中編輯fxml而不是打開只需編輯文本編輯器中的文本編輯器和fx-controller在borderpane中,您必須通過你的控制器,然後在你說的步驟之後該程序將運行的類路徑 –