Hallo, wie kann ich ein Bild in Java zu meinem JFrame hinzufügen?
Das habe ich bis jetzt, zeigt aber "File not found" an.
package com.company;
import javax.swing.*;
import javax.swing.ImageIcon;
public class Image extends JFrame {
Image() {
JFrame frame = new JFrame("Display Test");
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
try {
ImageIcon image = new ImageIcon(getClass().getResource("/home/marc/Bilder/Apfel.png"));
JLabel displayfield = new JLabel(image);
frame.add(displayfield);
} catch (Exception e) {
System.out.println("Image cannot be found!");
}
frame.pack();
frame.setSize(1000, 500);
frame.setVisible(true);
}
public static void main(String[] args) {
Image gui = new Image();
}
}
Muss ich bei "Name" das Verzeichnis, wie ich es getan habe, oder doch den Namen angeben ? Weder das eine noch das andere funktioniert. Ich benutze Linux Ubuntu, was wahrscheinlich eher keine Rolle spielt.