Correct way to initialize JFrame with a JPanel

I'm learning game development in Java. Here are 2 ways I've learned to initialize a JPanel attached to a JFrame. What are the differences between the two, and which one would be more correct?

Note that in Method 1, Skeleton does not extend JFrame. In Method 2 it does.

Method 1:

public class Board extends JPanel {
    public Board() {
        setPreferredSize(new Dimension(300, 280));

public class Skeleton {
    public static void main(String[] args) {
        JFrame window = new JFrame("Skeleton");
        window.setContentPane(new Board());

Method 2:

public class Board extends JPanel {
    public Board() {}

public class Skeleton extends JFrame {
    public Skeleton() {
        add(new Board());
        setSize(300, 280);

    public static void main(String[] args) {
        new Skeleton();


Basically, if you want your skeleton class to only be a JFrame, you'll be fine extending it. If you want it to have other functions, you'll want to have the class contain the JFrame object, as in your 'method 1'. Both methods will work the same (in terms of operating as a JFrame object), but it is a matter of what you want to do in the application. Extending JFrame will limit what you can do (i.e. calling certain methods), but if it is solely functioning as a JFrame, that won't matter anyway.

You should search for inheritance. When Class A extends Class B,Class A is Class B.

For instance,if i make a class Car,then make a class BMW ,it is logical that BMW extends class Car,since BMW is a car. BMW inherits all the methods in class car,because logically,a BMW can do what a standard car could do. However,BMW might be able to make extra things,that is why you can override methods and write new methods in class BMW that only class BMW can use(and whatever class inherits BMW). Method overriding is rewriting a method from the super class(class Car) in the child class (class BMW),the child method's header should be exactly the same as the super method's header,but the methods can do different things. In the first class,Skeleton is not a JFrame,while in the second Skeleton is a JFrame.

Need Your Help

How can I synchronize a live SQL Server 2005 server with a dev box?

sql-server-2005 synchronization

We have a development box which is inside our network and a web server farm outside.

System tray Java

java user-interface

I want to display a tray notification using Java like the notifications that comes out in windows when disk space is low, there is no antivirus software etc.