问题描述
我使用 SceneBuilder 在 ImageView
上创建了一个带有 AnchorPane
的 fxml 文件,并通过控制器类我用代码设置了图像,有点像......
@FXML ImageView;
...
void func(File file) {
Image image = new Image(file.toURI().toString());
ImageView.setimage(image);
}
但问题是只要文件是 jpeg,它就不会显示。虽然它与 png 完全正常。我查找了 Image
的 openjfx 文档。它说支持jpeg。这里有什么问题?
FXML 代码(test.fxml):
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.image.ImageView?>
<?import javafx.scene.layout.AnchorPane?>
<AnchorPane prefheight="1080.0" prefWidth="1920.0" xmlns="http://javafx.com/javafx/15.0.1" xmlns:fx="http://javafx.com/fxml/1" fx:controller="client.Testctrl">
<children>
<ImageView fx:id="imageView" fitHeight="1080.0" fitWidth="1440.0" pickOnBounds="true" preserveRatio="true" />
</children>
</AnchorPane>
控制器类代码(Testctrl.java):
package client;
import javafx.fxml.FXML;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import java.io.File;
public class Testctrl {
@FXML ImageView imageView;
private Main main;
public void setMain(Main main) {
this.main = main;
}
public void load() {
File file = new File("image.jpg");
Image image = new Image(file.toURI().toString());
imageView.setimage(image);
imageView.setFitHeight(1080);
imageView.setPreserveRatio(true);
}
}
主类代码(Main.java):
package client;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Main extends Application {
private Testctrl testctrl;
private Stage stage;
@Override
public void start(Stage primaryStage) throws Exception{
stage = primaryStage;
stage.setFullScreen(true);
stage.setResizable(false);
showtest();
}
public void showtest(){
FXMLLoader loader = new FXMLLoader();
loader.setLocation(getClass().getResource("test.fxml"));
Parent root=null;
try {
root = loader.load();
}
catch (IOException e)
{
e.printstacktrace();
System.exit(-1);
}
testctrl = loader.getController();
testctrl.setMain(this);
testctrl.load();
stage.setScene(new Scene(root,1920,1080));
stage.show();
}
public static void main(String[] args) {
launch(args);
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)