Problem on Compiling the JavaFX/FXML project with the default code
936642May 11 2012 — edited Oct 17 2012I just installed NetBeans 7.1.2, and JDK 7U4 and JavaFX2.1 Runtime, JavaFX2.1 SDK., then I used NetBeans to create a new JavaFx project with the default code, ans it ran fine to give "Hello World" on the console. Good... Then I used NetBeans to create a new JavaFX/FXML project with the default code -- Sample.java, Sample.fxml, and the main program, but then I got problem when I ran it. Please can someone tell me what gent wrong with my case. The following is the output on the console :
init:
deps-jar:
Created dir: C:\FXML Code\MyFXML002\build
Updating property file: C:\FXML Code\MyFXML002\build\built-jar.properties
Created dir: C:\FXML Code\MyFXML002\build\classes
Created dir: C:\FXML Code\MyFXML002\build\empty
Created dir: C:\FXML Code\MyFXML002\build\generated-sources\ap-source-output
Compiling 1 source file to C:\FXML Code\MyFXML002\build\classes
compile-single:
run-single:
Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.access$000(Unknown Source)
at com.sun.javafx.application.LauncherImpl$1.run(Unknown Source)
at java.lang.Thread.run(Thread.java:722)
Caused by: java.lang.IllegalStateException: Location is not set.
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at javafx.fxml.FXMLLoader.load(Unknown Source)
at myfxml002.MyFXML002.start(MyFXML002.java:25)
at com.sun.javafx.application.LauncherImpl$5.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$4.run(Unknown Source)
at com.sun.javafx.application.PlatformImpl$3.run(Unknown Source)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.access$100(Unknown Source)
at com.sun.glass.ui.win.WinApplication$2$1.run(Unknown Source)
... 1 more
Exception in thread "Thread-2" Java Result: 1
成功生成(总时间:3 秒)
The following are the copies of the code generated by NetBeans, none of them is my code.
1. MyFXML002.java---
package myfxml002;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
/**
*
* @author Administrator
*/
public class MyFXML002 extends Application {
public static void main(String[] args) {
Application.launch(MyFXML002.class, args);
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
stage.setScene(new Scene(root));
stage.show();
}
}
2. Sample.java---
package myfxml002;
import java.net.URL;
import java.util.ResourceBundle;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Label;
/**
*
* @author Administrator
*/
public class Sample implements Initializable {
@FXML
private Label label;
@FXML
private void handleButtonAction(ActionEvent event) {
System.out.println("You clicked me!");
label.setText("Hello World!");
}
@Override
public void initialize(URL url, ResourceBundle rb) {
// TODO
}
}
3. Sample.fxml---
<?xml version="1.0" encoding="UTF-8"?>
<?import java.lang.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml" fx:controller="myfxml002.Sample">
<children>
<Button id="button" layoutX="126" layoutY="90" text="Click Me!" onAction="#handleButtonAction" fx:id="button" />
<Label id="label" layoutX="126" layoutY="120" minHeight="16" minWidth="69" prefHeight="16" prefWidth="69" fx:id="label" />
</children>
</AnchorPane>
Edited by: 933639 on 2012-5-11 下午9:06