TheOneBruce
TheOneBruce

Reputation: 33

The method JFrame is undefined for the type HelloWorld

I don't know why but I get an error on my hello world java project:

The method JFrame is undefined for the type HelloWorld".

I only just started, can someone please explain to me the error and the solution?

package helloworld;

import javax.swing.JFrame;

public class HelloWorld {
    public static HelloWorld HelloWorld;
    public final int WIDTH = 800, HEIGHT = 800;

    public HelloWorld() {
        JFrame jframe = JFrame();
        jframe.setSize(WIDTH, HEIGHT);
        jframe.setVisible(true);

    }

    public static void main(String[] args) {
        HelloWorld = new HelloWorld();
    }
}

Upvotes: 0

Views: 185

Answers (3)

Every object must be instantiated with new keyword in Java. JFrame jframe = new JFrame("It's not a Hello World Program");

Upvotes: 1

GhostCat
GhostCat

Reputation: 140505

The error is in your constructor:

JFrame();

doesn't work; you need:

... = new JFrame();

Long story short: watch your syntax! Especially when you are a beginner, a good practice is to run the compiler as often as possible. Every time you wrote down something that you think should compile ... run the compiler. Don't write 10, 20 lines of code; to then try to figure what is all wrong in there!

Upvotes: 5

dumb_terminal
dumb_terminal

Reputation: 1825

change JFrame jframe = JFrame() to JFrame jframe = new JFrame()

Upvotes: 2

Related Questions