【发布时间】:2012-06-19 15:13:06
【问题描述】:
目前我正在使用基于更改按钮图像的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 builder 完成......
【问题讨论】:
-
请看这个example
标签: java swing netbeans jbutton imageicon