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

问题描述:

我刚刚开始使用JavaFX,并且遇到了使用Netbeans使用Scene Builder的诸多问题。无法在Netbeans中为JavaFX初始化图像

我想要完成的最新项目非常简单:单击按钮时,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); 
    } 

} 

初始化方法应改为:

@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"); 
    } 

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

构造函数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> 

(假设加载第一个图像的工作...)

+0

我认为图片被加载时,按钮被按下....不是从窗体加载.....检查它.... –

+0

@NewbieJavaDeveloper:它应该是***设置***当按钮被点击。在fxml中,已经有一个Image属性指定给ImageView的'image'属性,而另一个图像是否由'initialize'方法加载或者使用fxml本身并不重要,但是后一种方法具有在同一文件中指定图像的好处。此外,在你的回答中,你也不会在单击按钮时加载图像。 – f*

+0

我的错误,我没有看到他在谈论两张照片。 –