JLabel Icon不会在包图像内发生变化(Netbeans)

时间:2023-01-27 20:38:14

So I have this two images(.png) on my app package, and I want to change a JLabel's icon A to Icon B once the user clicks the original icon. The problem is, once the label i clicked, the image disappears and a text show's up instead ("jLabel1"). Here's the code for the listener:

所以我在我的应用程序包上有这两个图像(.png),并且我想在用户单击原始图标后将JLabel的图标A更改为图标B.问题是,一旦我点击了标签,图像就会消失,而文字显示为向上(“jLabel1”)。这是监听器的代码:

private void login_IngresarBMouseClicked(java.awt.event.MouseEvent evt){                                             
    String imageName = "login_buttonHighlight.png";
    ImageIcon icon = new ImageIcon(imageName);
    icon.getImage().flush();
    login_IngresarB.setIcon(icon);     
}                                

1 个解决方案

#1


Nevermind, I found the answer, used getResourse() instead of just the image name:

没关系,我找到答案,使用getResourse()而不仅仅是图像名称:

String imageName = "login_buttonHighlight.png";
ImageIcon icon = new ImageIcon(imageName);

For this one:

对于这个:

ImageIcon nuevo = new ImageIcon(getClass().getResource("login_buttonHighlight.png"));

#1


Nevermind, I found the answer, used getResourse() instead of just the image name:

没关系,我找到答案,使用getResourse()而不仅仅是图像名称:

String imageName = "login_buttonHighlight.png";
ImageIcon icon = new ImageIcon(imageName);

For this one:

对于这个:

ImageIcon nuevo = new ImageIcon(getClass().getResource("login_buttonHighlight.png"));