java 更改 jLabel 图标

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13277086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 12:05:46  来源:igfitidea点击:

Change jLabel icon

javaswingnetbeansjlabelimageicon

提问by phadaphunk

It seems like i'm not the only one with that question but I can't find an answer that solves the problem.

似乎我不是唯一一个有这个问题的人,但我找不到解决问题的答案。

I created a Labeland assign an Iconto it using WYSIWYG interface designer.
Now I want to change the icon dynamically during runtime.

我创建了一个LabelIcon使用WYSIWYG interface designer.
现在我想在运行时动态更改图标。

The logic way would be like this (my first attempt) :

逻辑方式是这样的(我的第一次尝试):

ImageIcon newIcon = new ImageIcon("SomePath");
jLabel1.setIcon(newIcon); 

When I do this the Icon simply disapears from the interface so I googled it and someone said to "flush" the icon whatever this means I tried it :

当我这样做时,图标只是从界面上消失了,所以我用谷歌搜索了它,有人说要“刷新”图标,无论这意味着我尝试过它:

ImageIcon newIcon = new ImageIcon("SomePath");
newIcon.getImage().flush();
jLabel1.setIcon(newIcon);

Still having the same problem.. The icon disapears.

还是有同样的问题。。图标消失了。

What am I doing wrong ?

我究竟做错了什么 ?



Update (Full Method) :

更新(完整方法):

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
attempted = myEngine.Attempt('q', word);
if(attempted)
  {
      this.jTextArea1.setText(myEngine.ChangeEncrypt('q', word, this.jTextArea1.getText()));    
  }
  else
  {
      JOptionPane.showMessageDialog(null,"The Letter Q is not in the word", "Error",JOptionPane.WARNING_MESSAGE);
      jButton1.setEnabled(false);
      life ++;
      ImageIcon newIcon = myEngine.UpdatePicture(life);
      newIcon.getImage().flush();
      jLabel1.setIcon(newIcon);
  }

This is the UpdatePicture Method :

这是 UpdatePicture 方法:

public ImageIcon UpdatePicture(int life)
{
  ImageIcon emptyIcon = new ImageIcon();
  if (life == 0)
  {
       ImageIcon iconZero = new ImageIcon("/hang0.gif");
       return iconZero;
  }
  if (life == 1)
  {
      ImageIcon iconOne = new ImageIcon("/hang1.gif");
      return iconOne;
  }
  if (life == 2)
  {
      ImageIcon iconTwo = new ImageIcon("/hang2.gif");
      return iconTwo;
  }
  if (life == 3)
  {
      ImageIcon iconThree = new ImageIcon("/hang3.gif");
      return iconThree;
  }
  if (life == 4)
  {
      ImageIcon iconFour = new ImageIcon("/hang4.gif");
      return iconFour;
  }
  if (life == 5)
  {
      ImageIcon iconFive = new ImageIcon("/hang5.gif");
      return iconFive;
  }
  if (life == 6)
  {
      ImageIcon iconSix = new ImageIcon("/hang6.gif");
      return iconSix;
  }

  return emptyIcon;

}

}

Not sure the whole code was necessary but still it might help.

不确定整个代码是否必要,但它仍然可能有所帮助。

The life variable starts at 0.
I checked and in the UpdatePicture it hits the "/hang1.gif";and returns it.

life 变量从 0 开始。
我检查并在 UpdatePicture 中命中"/hang1.gif";并返回它。

回答by flawyte

If the file is in your srcfolder then :

如果文件在您的src文件夹中,则:

ImageIcon ii = new ImageIcon(getClass().getResource("/myFile.gif"));

回答by gore_obsessed

You really should not put that slash before the name of your icon. It works like that.

你真的不应该把斜线放在你的图标名称之前。它是这样工作的。

回答by Phil Longenecker

I know this is an old question thread, but was having a hell of a time getting a icon replaced on a jLabel when my app was running. This is what finally fixed it: I created a new folder in the source package named images and put the images in there. In my Frame (main class) I added this below the initComponents method:

我知道这是一个旧的问题线程,但是当我的应用程序运行时,在 jLabel 上替换图标很费时间。这就是最终修复它的原因:我在名为 images 的源包中创建了一个新文件夹并将图像放在那里。在我的 Frame(主类)中,我在 initComponents 方法下面添加了这个:

  private void moreComponents() {
//    get images for initial screen prompts (Skd2_startPanel, loading label1).
// StartPanel is a panel on the Frame that has the loading label on it))
try {
  lcImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/LC.png")));
  clImage= new ImageIcon(ImageIO.read(getClass().getResource("/images/CL.png")));
} catch (IOException ex) {
  Logger.getLogger(Skd2_Frame.class.getName()).log(Level.SEVERE, null, ex);
 }
}

LC and CL.png are the images I want as the label icons.

LC 和 CL.png 是我想要作为标签图标的图像。

In a different class I added the following when I wanted the icon to change:

在另一个类中,当我想要更改图标时,我添加了以下内容:

   loadingLabel1.setIcon(lcImage); // or clImage as needed.

You'll need the following imports:

您将需要以下导入:

import java.util.logging.Logger;
import javax.imageio.ImageIO;

And add these two declarations just below the variables declarations:

并在变量声明的正下方添加这两个声明:

 static ImageIcon lcImage;
 static ImageIcon clImage;

Hope this helps someone searching the web for an answer.

希望这有助于有人在网上搜索答案。