My icon wont show up, I had this problem before even with images on labels.
import javax.swing.*;
public class Main {
public static void main(String[] args){
JFrame frame1 = new JFrame();
frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame1.setSize(600,600);
frame1.setVisible(true);
ImageIcon image1 = new ImageIcon("n.png");
frame1.setIconImage(image1.getImage());
}
}
Found the solution for JFrame Icon and JLabel icon: MyFrame as separated class
import java.awt.*;
public class MyFrame extends JFrame {
MyFrame(){
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.setSize(600,600);
this.setIconImage(new ImageIcon(getClass().getResource("p.PNG")).getImage());
this.getContentPane().setBackground(new Color(136,51,240));
}
}
MyLabel also separated:
import javax.swing.*;
public class MyLabel extends JLabel {
MyLabel(){
this.setText("Random generic text..");
this.setSize(200,200);
this.setIcon(new ImageIcon(getClass().getResource("P.PNG")));
}
}
Main:
public class Main {
public static void main(String[] args){
MyFrame frame1 = new MyFrame();
MyLabel label1 = new MyLabel();
frame1.add(label1);
frame1.setVisible(true);
}
}