gpt4 book ai didi

java - JLabel 图标未显示

转载 作者:行者123 更新时间:2023-11-30 06:27:16 24 4
gpt4 key购买 nike

我正在尝试在我的应用程序中显示图像...

    picture = new JLabel("No file selected");
picture.setFont(picture.getFont().deriveFont(Font.ITALIC));
picture.setHorizontalAlignment(JLabel.CENTER);

scrollPane.setViewportView(picture);

ImageIcon icon = new ImageIcon("map.jpg");
picture.setIcon(icon);
if (picture.getIcon() != null) // to see if the label picture has Icon
picture.setText("HERE IS ICON");

当我运行该代码时,只显示“HERE IS ICON”文本。对不起,如果这个问题听起来很愚蠢,但我真的不知道为什么不显示图像图标:(

最佳答案

您需要确保 map.jpg 作为文件存在。如果您想确定(仅用于测试目的),请尝试使用完整路径。按照您的方式,路径是相对于应用程序的起始目录的。

你可以仔细检查它是否存在:

System.out.println(new java.io.File("map.jpg").exists());

关于java - JLabel 图标未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13476469/

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