Adding JPanel to JFrame

Anonymous181 picture Anonymous181 · May 4, 2012 · Viewed 109.2k times · Source

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();

Answer

Andrew Thompson picture Andrew Thompson · May 4, 2012
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(){
...
}