Jamie van Eesteren
Jamie van Eesteren

Reputation: 85

JFrame Eclipse 'add' command error

While trying to add a button with "click me" written on it I get the following error message:

Exception in thread "main" java.lang.Error: Unresolved compilation problem: Cannot make a static reference to the non-static method add(Component) from the type Container

import javax.swing.JButton;
import javax.swing.JFrame;
public class FirstFrame extends JFrame {

    public static void main(String[] args) {
        JFrame frame = new JFrame("My little frame");
        JButton button = new JButton("Click Me");
        add(button);
        frame.setSize(300,200);
        frame.setVisible(true);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    }
}

Any help to resolve this error would be much appreciated!

Upvotes: 1

Views: 268

Answers (1)

DrPhill
DrPhill

Reputation: 632

The add() method is an instance method, and can only be called on an instance of (in this case) frame.

try

frame.add(button);

Upvotes: 2

Related Questions