Hi I am trying to add an object for example a JLabel from a main class to a secondary class that inherits the attributes of a JPanel.I have created a basic example that shows what I was trying to do but it dosen’t work.Here is my code:
public class main extends JFrame{
public main(){
this.setVisible(true);
this.setSize(600, 600);
panel nou = new panel(new JLabel("a mers"));
}
public static void main (String[] args){
new main();
}
}
public class panel extends JPanel{
public panel(JLabel nou){
this.add(nou);
}
}
My original code has to add some images from an external class and I tried the same aproach but it dosen’t work.How can I achive this?
EDIT:This is just an example I need to add this component from an external class
Use Container#add(Component). You can use it the same way within the constructor as you can outside of it (without the this).