我经常看到这个问题被问及,但是我还没有看到一个非常通用的解决方式。我正在使用JavaFX和Java Eclipse Oxygen开发桌面应用程序,并且当通过http加载图像时,我有一个加载器要展示。当用户点击不同的行时,必须通过http加载图像。因此,在图像上方放置了加载器,我只需使用productPreviewLoader.setVisible(true)
来显示或隐藏它。在GridPane中,fxml文件中的加载器ImageView如下所示。
<GridPane>
<columnConstraints>
<ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" />
</columnConstraints>
<rowConstraints>
<RowConstraints minHeight="10.0" valignment="TOP" vgrow="ALWAYS" />
<RowConstraints vgrow="SOMETIMES" />
<RowConstraints vgrow="SOMETIMES" />
</rowConstraints>
<children>
<ImageView fx:id="productPreview" fitHeight="282.0" fitWidth="200.0" pickOnBounds="true" preserveRatio="true" />
<!-- This is the loader-->
<ImageView fx:id="productPreviewLoader" fitHeight="112.0" fitWidth="136.0" pickOnBounds="true" preserveRatio="true" />
<TextFlow fx:id="productPreviewTextFlow" GridPane.rowIndex="1">
<GridPane.margin>
<Insets right="10.0" />
</GridPane.margin>
<padding>
<Insets bottom="30.0" left="10.0" right="10.0" top="30.0" />
</padding></TextFlow>
设置加载图像的代码如下:
//Set up the product preview image loader
BufferedImage bufferedImage;
File file = new File("resources\\images\\loaders\\img-loader-icon.gif");
try {
bufferedImage = ImageIO.read(file);
productPreviewLoader.setImage(SwingFXUtils.toFXImage(bufferedImage, null));
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
productPreviewLoader.setVisible(false);
productPreviewLoader.setImage(SwingFXUtils.toFXImage(bufferedImage, null));
时,图像只是静止的,没有动画。 - user5500750productPreviewLoader.setImage(new Image("file:resources\\images\\loaders\\img-loader-icon.gif"));
就可以加载一个播放图像。在你的回答中,我认为这对于本地图片不起作用this.getClass().getResource("java.gif").toExternalForm()
我猜你是想加载本地图片而不是通过HTTP加载图片。 - user5500750