英文:
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进行这个程序,以防有人需要知道。
<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("file:testImage.JPG");
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>
专注分享java语言的经验与见解,让所有开发者获益!
评论