JavaFX Fxml项目的Hello世界

时间:2012-08-28 06:13:35

标签: java javafx scenebuilder

今天我要用JavaFX Fxml项目创建Hello World应用程序。这是代码。        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="temp.Temp">
<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>

Sample.java

package temp;
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;
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
}    
}

Temp.java

package temp;
import javafx.application.Appl
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
public class Temp extends Application {
public static void main(String[] args) {
    Application.launch(Temp.class, args);
}
@Override
public void start(Stage stage) throws Exception {
    try{
    Parent root = FXMLLoader.load(getClass().getResource("Sample.fxml"));
    stage.setScene(new Scene(root));
    stage.show();
    }
    catch(Exception e){
        System.out.print(e);
    }      
}
}

但它给出了错误 /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1757:执行此行时发生以下错误: /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1392:jarsigner返回:1 BUILD FAILED(总时间:6秒)

但是当我使用异常处理时,它找不到位置? 请帮忙

1 个答案:

答案 0 :(得分:3)

您正尝试从fx:controller="temp.Temp"加载Controller。这不是您的控制器类,您应该使用fx:controller="temp.Sample"