通过FXML的JavaFX ImageView不起作用 [英] JavaFX ImageView via FXML does not work
本文介绍了通过FXML的JavaFX ImageView不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在FXML上使用ImageView
加载图像时遇到问题.
I have a problem with loading Images with ImageView
on FXML.
我的控制器类:
public class BoxViewController {
@FXML
private Label label_boxID;
@FXML
private ImageView boximage;
public void initData(ObservableList<BoxProperty> observableList,
BoxService sBox,
TableView tableview) {
this.label_boxID.setText(
String.valueOf(this.boxproperty.getPboxid()));
Image image = new Image("boximage.jpg");
this.boximage = new ImageView();
this.boximage.setImage(image);
}
}
因此,使用文本设置标签是可行的,但是图像不会出现在我的ImageView中. 对于ImageView,我向FXML文件添加了一个ID:
So, setting the label with a text works, but the image won't appear in my ImageView. For the ImageView, I added an ID to the FXML file:
<ImageView fx:id="boximage"
disable="false"
fitHeight="150.0" fitWidth="200.0"
layoutX="69.0" layoutY="322.0"
pickOnBounds="true"
preserveRatio="true" />
我很困惑为什么这不能正常工作,因为标签可以工作,但是图像无法加载.
我还检查了boximage
是否不为null,但不是.也没有例外.
I'm confused why this is not working because the label works, but the image won't load.
I also checked whether boximage
isn't null, but it isn't. There are also no Exceptions.
推荐答案
这有效:
BufferedImage bufferedImage;
bufferedImage = ImageIO.read(new File(this.path));
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
this.boximage.setImage(image);
这篇关于通过FXML的JavaFX ImageView不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文