【问题标题】:Java GDK+: Pixbuf image not foundJava GDK+:找不到 Pixbuf 图像
【发布时间】:2019-01-30 19:24:17
【问题描述】:

org.gnome.gdk.Pixbuf 构造函数的文件路径参数相对于哪个目录?我应该把文件放在哪里?

Main.java

public class Main
{
    public static void main( String... args )
    {
        Gtk.init( args );
        new Example();
        Gtk.main();
    }
}

Example.java

public class Example
{
    private Pixbuf icon;

    public Example()
    {
        try {
            icon = new Pixbuf( "images/bolt.png" );
        } catch ( FileNotFoundException e ) {
            e.printStackTrace();
        }
    }
}

它会抛出以下异常:

java.io.FileNotFoundException:找不到图像/web.png

我的目录结构是:

【问题讨论】:

    标签: java gnome gdk


    【解决方案1】:

    使用this.getClass().getResourceAsStream(/images/bolt.png) 获取InputStream 并使用此answer 将其转换为byte[],您可以将其传递给Pixbuf 构造函数。

    如果您使用的是 Java 9,您还可以在 InputStream 上调用 readAllBytes()

    【讨论】:

    • 谢谢。我将在今天晚些时候检查并标记您的答案是否有效。
    • 错误消失了。该图标仍未显示,但这将是另一个问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-11-17
    • 1970-01-01
    • 2022-07-18
    • 2011-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多