現在、ボタンの画像を変更することに基づいて、netbeans を使用して Java プログラムを作成しています。
実際、私の要件は、別のボタンをクリックしたときにボタンの画像アイコンを変更することです(Aと言います).....
私は次のプログラムで出てきました........
       // Following function is included inside the button's (Here A) ActionListener........
       public void change_image()
       {
             if(sex==0)
             {
                   ic=new ImageIcon("E:\\java_images\\female_profile.jpg");
                   sex=1;
             }
             else if(sex==1)
             {
                   ic = new ImageIcon("E:\\java_images\\male_profile.png");
                   sex=0;
             }
              // To resize the image into the size of the button... 
               labelicon.setImage(ic.getImage().getScaledInstance(image_btn.getWidth(),image_btn.getHeight(), Image.SCALE_DEFAULT));
             img_btn.setIcon(labelicon);
         }
私が含めた変数は
           private int sex;    // 0  - female, 1 - male
           private ImageIcon ic,labelicon;  // variables meant for storing ImageIcons.....
           private JButton img_btn;  // the button at which the image is to  be displayed....
今、私が観察した奇妙な行動は......
最小化ボタンをクリックした場合にのみ、ボタンのクリック時に画像が表示されます。つまり、ボタン A をクリックすると、ActionListener で指定されたコードが実行されます。しかし、画像の変更の効果は、ウィンドウを最小化して再度画面に表示した場合にのみ表示されます....なぜこれが起こっているのか、どうすれば問題を解決できるのか誰にもわかりますか??
私が欲しいのは、Aボタンをクリックした瞬間に画像を変更することだけです.....まあ..ボタンを作成するためのコードは含めていません.netbeansのswing GUIビルダーによって簡単に実行できるためです....


