essevaiproale 26 de set. de 2021
Estranho , testei uma troca de ícones por botões e funcionou aqui:
import java.awt.EventQueue ;
import java.awt.event.ActionEvent ;
import java.awt.event.ActionListener ;
import javax.swing.ImageIcon ;
import javax.swing.JButton ;
import javax.swing.JFrame ;
import javax.swing.JLabel ;
import javax.swing.JPanel ;
import javax.swing.border.EmptyBorder ;
public class TesteIcon extends JFrame {
private static final long serialVersionUID = - 947621249806008126L ;
private JPanel contentPane ;
private boolean troca = false ;
private JLabel lblNewLabel ;
/**
* Launch the application.
*/
public static void main ( String [] args ) {
EventQueue . invokeLater ( new Runnable () {
public void run () {
try {
TesteIcon frame = new TesteIcon ();
frame . setVisible ( true );
} catch ( Exception e ) {
e . printStackTrace ();
}
}
});
}
/**
* Create the frame.
*/
public TesteIcon () {
setDefaultCloseOperation ( JFrame . EXIT_ON_CLOSE );
setBounds ( 100 , 100 , 450 , 300 );
contentPane = new JPanel ();
contentPane . setBorder ( new EmptyBorder ( 5 , 5 , 5 , 5 ));
setContentPane ( contentPane );
contentPane . setLayout ( null );
JButton btnNewButton = new JButton ( "New button" );
btnNewButton . addActionListener ( new ActionListener () {
public void actionPerformed ( ActionEvent e ) {
System . out . println ( troca );
if ( troca ) {
lblNewLabel . setIcon ( new ImageIcon ( TesteIcon . class . getResource ( "/imgs/reconnect.png" )));
troca = false ;
} else {
lblNewLabel . setIcon ( new ImageIcon ( TesteIcon . class . getResource ( "/imgs/disconnect.png" )));
troca = true ;
}
}
});
btnNewButton . setBounds ( 10 , 11 , 89 , 23 );
contentPane . add ( btnNewButton );
lblNewLabel = new JLabel ( "Icone" );
lblNewLabel . setIcon ( new ImageIcon ( TesteIcon . class . getResource ( "/imgs/reconnect.png" )));
lblNewLabel . setBounds ( 10 , 55 , 190 , 48 );
contentPane . add ( lblNewLabel );
}
}
Troque o caminho dos ícones por ícones existentes na sua máquina e teste pra ver se te atende.