0

Help My friends

Help me please, I'm trying to call several images from my package with the setIcon method, actually I want the method to show me the images that I assign in my jlabel. But somehow my code throws me an error. import javax.swing.Icon; import javax.swing.ImageIcon; setColor(btnInicio2,Lbl_inicio,"/iconosV2/home-altVr.png"); resetColor(btnCliente2,Lbl_clientes,"/iconosV1/users-altV.png"); resetColor(btnProveedor2,Lbl_proveedor,"/iconosV1/truckV.png"); private void setColor(JPanel pane, JLabel lbl, String url) { pane.setBackground(new Color(89, 106, 158)); lbl.setForeground(new Color(255,255,255)); lbl.setIcon(new ImageIcon(getClass().getResource(url))); } private void resetColor(JPanel pane, JLabel lbl, String url) { pane.setBackground(new Color(57,76,135)); lbl.setForeground(new Color(166, 166, 166)); lbl.setIcon(new ImageIcon(getClass().getResource(url))); }

2nd Apr 2022, 7:56 AM
Gilmar
Gilmar - avatar
1 Answer
0
I checked this, it works: import javax.swing.*; import java.awt.Color; public class SetColor_test { public static void main(String[] args) { new SetColor_test().run(); } void run() { JFrame frame = new JFrame(); frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE); frame.setSize(500, 700); JPanel panel = new JPanel(); JLabel label = new JLabel(""); panel.add(label); frame.add(panel); frame.setVisible(true); setColor( panel, label, "javaColor.png"); sleep(3); resetColor(panel, label, "javaBW.png"); } private void setColor( ... ) { ... } private void resetColor( ... ) { ... } void sleep(int sec) { try { Thread.sleep(sec * 1000); } catch (InterruptedException e) { System.out.println(e); } } }
4th Apr 2022, 5:38 PM
zemiak