Search code examples
javaswingjframejpanel

Adding JPanel to JFrame


I have a program in which a JPanel is added to a JFrame:

public class Test{

    Test2 test = new Test2();
    JFrame frame = new JFrame();

    Test(){

    ...
    frame.setLayout(new BorderLayout());
    frame.add(test, BorderLayout.CENTER);
    ...

    }

    //main

    ...

    }

    public class Test2{

    JPanel test2 = new JPanel();

    Test2(){

    ...

    }

}

I get an error asking me to change type of 'panel' to 'component'. I do I fix this error? It wants me to do: Component panel = new Component();


Solution

  • public class Test{
    
    Test2 test = new Test2();
    JFrame frame = new JFrame();
    
    Test(){
    ...
    frame.setLayout(new BorderLayout());
    frame.add(test, BorderLayout.CENTER);
    ...
    }
    
    //main
    ...
    }
    
    //public class Test2{
    public class Test2 extends JPanel {
    
    //JPanel test2 = new JPanel();
    
    Test2(){
    ...
    }