2

その質問をしているのは私だけではないようですが、問題を解決する答えを見つけることができません。

を作成し、を使用してLabel割り当てます。 ここで、実行時にアイコンを動的に変更したいと思います。IconWYSIWYG interface designer

論理的な方法は次のようになります(私の最初の試み):

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

これを行うと、アイコンがインターフェイスから消えてしまうので、グーグルで検索すると、誰かがアイコンを「フラッシュ」すると言ったのですが、これは私が試したことを意味します。

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

まだ同じ問題があります。アイコンが消えます。

私は何を間違っているのですか?


更新(フルメソッド):

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

これは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;

}

コード全体が必要かどうかはわかりませんが、それでも役立つ場合があります。

寿命変数は0から始まります。
チェックしたところ、UpdatePictureで、がヒットし"/hang1.gif";て返されました。

4

3 に答える 3

3

ファイルがsrcフォルダにある場合:

ImageIcon ii = new ImageIcon(getClass().getResource("/myFile.gif"));
于 2012-11-07T20:27:48.513 に答える
1

アイコンの名前の前にそのスラッシュを置くべきではありません。それはそのように機能します。

于 2012-11-07T20:21:53.917 に答える
0

これは古い質問スレッドであることは知っていますが、アプリの実行中にjLabelのアイコンを置き換えるのに時間がかかっていました。これが最終的に修正されたものです。ソースパッケージにimagesという名前の新しいフォルダを作成し、そこに画像を配置しました。フレーム(メインクラス)で、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とCL.pngは、ラベルアイコンとして必要な画像です。

別のクラスで、アイコンを変更したいときに次を追加しました。

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

次のインポートが必要です。

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

そして、変数宣言のすぐ下に次の2つの宣言を追加します。

 static ImageIcon lcImage;
 static ImageIcon clImage;

これが誰かが答えをウェブで検索するのに役立つことを願っています。

于 2015-08-20T19:53:48.453 に答える