- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想知道为什么我的 JWindow 不显示我的图像。我有这段代码:
JWindow window = new JWindow();
JPanel jp = new JPanel();
JLabel l = new JLabel(new ImageIcon("GenericApp.png"));
jp.add(l);
window.add(jp);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
window.setVisible(false);
编辑:我的代码现在是(回到原来的):
final JWindow window = new JWindow();
JPanel jp = new JPanel();
JLabel l = new JLabel(new ImageIcon(Asset.class.getResource("GenericApp.png")));
jp.add(l);
window.add(jp);
window.setBounds(500, 150, 300, 200);
window.setVisible(true);
try{
Thread.sleep(5000);
}catch(InterruptedException e){
e.printStackTrace();
}
window.setVisible(false);
是的,我决定使用Thread.sleep()
。我的 jar 从/Users/thomasokeeffe/Desktop/eclipse/AI/AI 执行,这就是图片所在的位置。启动时我仍然得到一个空白的 JWindow。 System.out.println(new File("GenericApp.png").exists());
返回 true。
又一次编辑 如果我把图片放在 jar 文件中,对我来说会更容易吗?最奇怪的事情之一是,当我不在应用程序中运行 jar 并将图片放在工作目录中时,它仍然显示一个空白矩形......
编辑 到目前为止,我已尝试使用 URL,并通过获取 user.dir 系统属性进行调试。我现在有这两行代码来获取图片(图片与类在同一个包中):
URL url = this.getClass().getResource("GenericApp.png");
JLabel l = new JLabel(new ImageIcon(url));
我也尝试过使用 InputStream:
InputStream is = this.getClass().getResourceAsStream("GenericApp.png");
Image image = null;
try {
image = ImageIO.read(is);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
我做错了什么?
最佳答案
如果图像在您的 jar 中或通常在您的类路径中,您可以通过以下方式获取它
getClass().getResource(resourcePath);
资源路径以类路径的根为根。
例如,与您的类(class)在同一目录中的图像将是
new JLabel(new ImageIcon(getClass().getResource("GenericApp.png")));
关于java - JWindow 不会显示 ImageIcon,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31497007/
我想知道是否有办法创建一个 ImageIcon,它是另一个 ImageIcon 的镜像。 在 Google 上搜索,我找到了如何使用许多 AWT 库来实现这一点。 有没有办法用 Swing 做到这一点
我想为 Tanks Game 编写代码,但在现有 ImageIcon 上设置 tank.jpg 时遇到问题,因为我希望这两个图像都可见并向用户显示,就像: JButton block = new JB
我有一个JTable,我可以在其中从数据库获取数据并显示。其中一列是 blob 对象类型,它包含一个图像(如果存在,否则为 null )。我还有一个用于 JTable 的双击事件处理程序,这样当用户仅
这是我的代码: /** * Opens file and gives a warning if non image. */ private void openFile() { final
我有一个 JLabel与 ImageIcon ,我使用 setIcon() JLabel中的函数. ImageIcon然后上来,坐在我的JLabel 的文字左侧.是否有可能拥有 ImageIcon在文
我有一个对象“Team”,其中包含一些分数变量、名称(字符串)和 Logo 。 Logo 的类型为 ImageIcon: public class Team { public String n
当我尝试在 JTable 中显示 ImageIcon 时,仅呈现描述。代码是: private void createTable() { dataModel = new AbstractTab
我编写了非常简单的代码来显示葡萄的图标,但代码仍然没有向我显示任何内容 这是我的代码 import javax.swing.*; import java.awt.*; public class Cod
我有两个问题: 我正在创建一个简单的内存游戏,它保存随机序列并期望玩家输入相同的输入,并且我正在尝试更改 JButton ImageIcon 当使用 setIcon() 单击到更亮版本的 img1 时
这个问题已经有答案了: What is a NullPointerException, and how do I fix it? (12 个回答) 已关闭 6 年前。 我试图向这些标签添加一些图像,但
是否有办法将字符串转换为 ImageIcon? 有点像这里的代码:http://www.java2s.com/Code/Java/SWT-JFace-Eclipse/Createaniconinmem
这是我的代码 import javax.swing.*; public class Option extends JFrame{ ImageIcon img = new ImageIcon("imag
我正在开发一个 Java swing 应用程序,我正在尝试加载国际象棋棋子的图像,一切都工作得很好,直到突然出现错误: Exception in thread "main" java.lang.Sta
这个问题已经有答案了: Displaying an ImageIcon (2 个回答) 已关闭10 年前。 我有四个面板,我想向这些面板添加图像。 当我点击开始按钮时,每个面板需要更改4张图像。 我正
我在java中制作了这样的图像列表: static String[] imageList1 = { "images/bgs/bg.png", "images/bgs/image1.png",
这个问题已经有答案了: Update JLabel repeatedly with results of long running task (2 个回答) 已关闭 8 年前。 您好,我在将图像名称读
我不知道这里发生了什么,因为我在这里画了很多画,但这让我发疯。 m_background 图像只显示了一半。当我调整大小时,它看起来像是那里有一些组件,所以当我将窗口底部向上拉时,显示的图像较少,而当
我想添加一个图标而不是复选框的默认方框。每当我添加图标时,唯一的复选框标签 View 就不会显示图标或默认方框。为什么没有看到图标..我搜索了很多..但没有任何效果对我有用这是代码.. import
我想在按下按钮一次时在标签上设置图标,如果按下两次,它将删除我已经使用了 label.setIcon(null); 但它对我来说效果不佳。 public void actionPerformed(Ac
我已经研究了很多其他问题,其中人们遇到的问题与我在这里遇到的问题类似(大多数解决方案都是通过简单的错误找到的),但我终究无法弄清楚为什么我的图形不会t 显示在我的 jframe 中。我是 Java G
我是一名优秀的程序员,十分优秀!