-2

I am really new to Java and just started learning. I took code from the internet but whenever I try to compile it and error shows up saying:

JavaTutorial.java:11: error: cannot find symbol
                new BasicSwing();
                    ^
  symbol:   class BasicSwing
  location: class JavaTutorial
1 error

This is the code:

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;

public class JavaTutorial extends JFrame{

    JPanel p = new JPanel();
    JButton b = new JButton("Hello");

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

    public void BasicSwing(){
        setTitle("Basic Swing app");
        setSize(400,300);
        setResizable(true);

        p.add(b);
        add(p);


        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
    }
}

Can anyone tell me what am I doing wrong?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
João Areias
  • 1,192
  • 11
  • 41

3 Answers3

0

BasicSwing is declared as a method of class JavaTutorial

You can't instantiate a method

Probably, your class should be named BasicSwing in your example. By doing that, you method BasicSwing will be no more a method but a constructor

Prim
  • 2,880
  • 2
  • 15
  • 29
0

Change your code to following :

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

public JavaTutorial(){
    setTitle("Basic Swing app");
    setSize(400,300);
    setResizable(true);

    p.add(b);
    add(p);


    setDefaultCloseOperation(EXIT_ON_CLOSE);
    setVisible(true);
}
Madushan Perera
  • 2,568
  • 2
  • 17
  • 36
0
public class JavaTutorial extends JFrame

Should be changed to

public class BasicSwing extends JFrame

Because you want to have an instance of this class apparently since you wrote a constructor in it.


And the constructor should be changed to

public BasicSwing() // Without the void because constructors have no return type

Useful Links

Community
  • 1
  • 1
Yassin Hajaj
  • 21,337
  • 9
  • 51
  • 89