简体   繁体   English

JavaFX Fxml项目的Hello世界

[英]Hello world with JavaFX Fxml Project

Today i am Going to create Hello World Application with JavaFX Fxml project.Here is the code. 今天我要用JavaFX Fxml项目创建Hello World应用程序。这是代码。 Sample.fxml 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 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 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);
    }      
}
}

But It is Giving Error /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1757: The following error occurred while executing this line: /Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1392: jarsigner returned: 1 BUILD FAILED (total time: ६ seconds) 但是它给出了错误/Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml:1757:执行此行时发生以下错误:/Users/apple/NetBeansProjects/Temp/nbproject/jfx-impl.xml: 1392:jarsigner返回:1 BUILD FAILED(总时间:6秒)

But When i Use Exception Handling it Gives Location Not Found ? 但是,当我使用异常处理时,它找不到位置? Please help 请帮忙

You are trying to load Controller from fx:controller="temp.Temp" . 您正在尝试从fx:controller="temp.Temp"加载Controller。 This is not Your controller class, you should use fx:controller="temp.Sample" instead. 这不是你的控制器类,你应该使用fx:controller="temp.Sample"代替。

You also need a leading forward slash to load package resources. 您还需要一个前导斜杠来加载包资源。 Assuming that you have Sample.fxml in the temp package, it would look like this: 假设您在临时包中有Sample.fxml,它将如下所示:

Parent root = FXMLLoader.load(getClass().getResource("/temp/Sample.fxml"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM