在JavaFX中图片未显示

huangapple 未分类评论42阅读模式
英文:

Image not displaying in JavaFX

问题

我正在尝试学习如何在JavaFX中显示图像为了学习我尝试创建了这个简单的程序然而图像不会显示在应用程序中

```java
package imagedemo;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

public class ImageDemo extends Application
{    
    public static void main(String[] args)
    {
    launch(args);
    }
    
    @Override
    public void start(Stage primaryStage)
    {
        //创建图像和ImageView
        Image image = new Image("file:testImage.JPG");
        ImageView iv = new ImageView(image);
        
        //创建HBox、场景和舞台
        HBox hbox = new HBox(iv);
        Scene scene = new Scene(hbox);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

当我运行程序时,结果是:一个空的应用程序窗口。是否有任何解决方案可以使图像像应该出现在应用程序中一样?

编辑1:我正在使用NetBeans IDE 8.2进行这个程序,以防有人需要知道。

编辑2:这是文件testImage.JPG的完整路径:在JavaFX中图片未显示


<details>
<summary>英文:</summary>

I am trying to learn how to display images in JavaFX. In order to learn, I tried to create this simple program. However, the image does not display in the application. 

package imagedemo;

import javafx.application.Application;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;

public class ImageDemo extends Application
{
public static void main(String[] args)
{
launch(args);
}

@Override
public void start(Stage primaryStage)
{
    //Create Image and ImageView
    Image image = new Image(&quot;file:testImage.JPG&quot;);
    ImageView iv = new ImageView(image);
    
    //Create HBox, Scene, and Stage
    HBox hbox = new HBox(iv);
    Scene scene = new Scene(hbox);
    primaryStage.setScene(scene);
    primaryStage.show();
}

}

When I run the program, the result is: [an empty application window][1]. Are there any solutions that will cause the image to appear on the application as it should?

Edit 1: I am using NetBeans IDE 8.2 for this program in case anyone needs to know.

Edit 2: Here is the full path to file `testImage.JPG`: [![full path][2]][2]


  [1]: https://i.stack.imgur.com/wCNoD.jpg
  [2]: https://i.stack.imgur.com/GjUiD.png

</details>


huangapple
  • 本文由 发表于 2020年7月26日 02:51:24
  • 转载请务必保留本文链接:https://java.coder-hub.com/63092266.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定