微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

JavaFX Image 和 ImageViewer 不支持 jpeg

如何解决JavaFX Image 和 ImageViewer 不支持 jpeg

我使用 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 举报,一经查实,本站将立刻删除。