无法在Netbeans中初始化JavaFX的图像

时间:2016-12-02 07:01:02

标签: java netbeans javafx scenebuilder

我刚刚开始使用JavaFX,并且在使用带有Netbeans的Scene Builder时遇到了很多问题。

我想要完成的最新项目非常简单:当单击按钮时,ImageView将显示另一个图像。

我根据我的书告诉我要做的事情,但代码一直在Netbeans中崩溃。

这是我的代码:

package javafxapplication1;

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;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;

public class FXMLDocumentController implements Initializable 
{

    @FXML
    public Label label;
    @FXML
    public ImageView img;

    private Image sample;

    @FXML
    public void handleButtonAction(ActionEvent event) 
    {
        System.out.println("You clicked me!");
        label.setText("Hello World!");
        img.setImage(sample);

    }

    @Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        sample = new Image("Users/limyunsoon/Desktop/models.jpg");
    }    

}

FXML文件:

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

<?import javafx.scene.image.*?>
<?import java.lang.*?>
<?import java.util.*?>
<?import javafx.scene.*?>
<?import javafx.scene.control.*?>
<?import javafx.scene.layout.*?>

<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320"         xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8"      fx:controller="javafxapplication1.FXMLDocumentController">
<children>
    <Button fx:id="button" layoutX="123.0" layoutY="142.0"   onAction="#handleButtonAction" text="Click Me!" />
    <Label fx:id="label" layoutX="126.0" layoutY="169.0" minHeight="16" minWidth="69" />
  <ImageView fx:id="img" fitHeight="115.0" fitWidth="200.0"    layoutX="60.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
     <image>
        <Image url="file:/Users/limyunsoon/Desktop/roadster.jpg" />
     </image>
  </ImageView>
</children>
</AnchorPane>

java文件:

package javafxapplication1;

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class JavaFXApplication1 extends Application 
{

    @Override
    public void start(Stage stage) throws Exception 
    {
        Parent root =    FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));

        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.show();
    }

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

}

2 个答案:

答案 0 :(得分:0)

应该更改initialize方法:

@Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        sample = new Image("Users/limyunsoon/Desktop/models.jpg");
    }

应该是

@Override
    public void initialize(URL url, ResourceBundle rb) 
    {
        //added full image path and added "file" before the path
        sample = new Image("file:c:/Users/limyunsoon/Desktop/models.jpg");
    }

试试这个。希望它会有所帮助。

答案 1 :(得分:0)

Image构造函数需要与fxml中的图像使用相同类型的URL。我建议将图像作为资源添加到程序中,否则在将程序移动到另一台机器后不太可能找到图像。

此外,在这种情况下,我建议从fxml:

初始化Image
@FXML
private Image sample;

...

@Override
public void initialize(URL url, ResourceBundle rb) {
}
<AnchorPane id="AnchorPane" prefHeight="200" prefWidth="320" xmlns:fx="http://javafx.com/fxml/1" xmlns="http://javafx.com/javafx/8" fx:controller="javafxapplication1.FXMLDocumentController">
    <children>
        <Button fx:id="button" layoutX="123.0" layoutY="142.0"   onAction="#handleButtonAction" text="Click Me!" />
        <Label fx:id="label" layoutX="126.0" layoutY="169.0" minHeight="16" minWidth="69" />
      <ImageView fx:id="img" fitHeight="115.0" fitWidth="200.0"    layoutX="60.0" layoutY="14.0" pickOnBounds="true" preserveRatio="true">
         <image>
            <Image url="file:/Users/limyunsoon/Desktop/roadster.jpg" />
         </image>
      </ImageView>
    </children>
    <fx:define>
        <Image fx:id="sample" url="file:/Users/limyunsoon/Desktop/models.jpg" />
    </fx:define>
</AnchorPane>

(这假设加载第一张图片......)