その質問をしているのは私だけではないようですが、問題を解決する答えを見つけることができません。
を作成し、を使用してLabel
割り当てます。
ここで、実行時にアイコンを動的に変更したいと思います。Icon
WYSIWYG 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";
て返されました。