gpt4 book ai didi

java - 为什么我的图像不显示? (Java 图形用户界面)

转载 作者:行者123 更新时间:2023-12-01 13:00:49 26 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。今天我在 Linux 上编写了程序,输入了图像的地址,一切都很好。不知何故,当我在家尝试Windows时,图像无法显示!(当然我更新了地址)图片上的地址应该怎么写?我应该把它放在程序包的什么位置?我保留第二个地址不变,这样你就可以明白了。

正如您所看到的,还有另一个问题。我需要显示两张图像,但我只看到一张。我应该使用哪种布局才能相邻显示 2 个图像?

抱歉,如果我的问题很愚蠢,我还是个初学者:)

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();
}
}

最佳答案

在 Windows 中为 Chrysanthemum.jpg使用 jpg 而不是 jpeg

在 Windows 中附加 C: 以创建绝对路径。

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

--编辑--

如果是 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";

关于java - 为什么我的图像不显示? (Java 图形用户界面),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23525922/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com