【发布时间】:2014-01-30 09:35:39
【问题描述】:
我的项目包含一个名为 images 的文件夹,其中包含图像。我想在按下按钮时将图像随机显示到框架中的JLabel。我尝试了以下代码:
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt)
{
Image im=new ImageIcon(this.getClass().getResource("/images/a1.jpg")).getImage();
ImageIcon iconLogo = new ImageIcon(im);
jLabel2.setIcon(iconLogo);
}
此代码仅显示图像 a1。但我需要随机的图像(一次一张)。
【问题讨论】:
-
查看
ImageViewer以获取有关显示多个图像的提示。 1) 为了尽快获得更好的帮助,请发帖MCVE。 2) 例如,获取图像的一种方法是热链接到this answer 中看到的图像(另一种方法显示在ImageViewer代码中)。
标签: java image swing random netbeans-7