Frage

I am having a strange problem. Today I wrote my program on Linux and I typed an adress to the image and everything was fine. Somehow, when I am trying on Windows at home the image just won't display!(I updated the adress of course) How should I write the adress to the picture? Where in the program package should I put it? I left the second adress unchanged, so you can get the idea.

As you can see, there is another problem as well. I need to display two images but I see only one. Which layout should I use, in order to display 2 images next to one another?

Sorry if my questions are very stupid, I am still a beginner :)

public class View extends JFrame {
    JPanel jp = new JPanel();
    JLabel jl = new JLabel();
    JPanel jg = new JPanel();
    JLabel jz = new JLabel();

    public View() {
        this.setTitle("Media");
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        JPanel contentAll =  new JPanel();
        contentAll.setLayout(new BorderLayout());

        //(...)  a pair of buttons here, not relevant I guess

        jl.setIcon(new ImageIcon("/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpeg")); ///Windows
        jp.add(jl);
        add(jp);

        jz.setIcon(new ImageIcon("/home/d/Downloads/chanel.jpg")); /// Linux
        jg.add(jz);
        add(jz);
        jg.setLayout(new FlowLayout());

        this.pack();
    }
}
War es hilfreich?

Lösung

Its Chrysanthemum.jpg in Windows. use jpg instead of jpeg.

append C: in Windows to make an absolute path.

new ImageIcon("C:/Users/Public/Pictures/Sample Pictures/Chrysanthemum.jpg");

--EDIT--

use absolute path in case of Windows.

String userHome = System.getProperty("user.home"); // C:/Users/USERNAME
String userPath = userHome.substring(0, userHome.lastIndexOf("\\")); // C:/Users
String fullPath = userPath + "/Public/Pictures/Sample Pictures/Chrysanthemum.jpg";
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top