Search code examples
javacannot-find-symbol

java error, can not find symbol


I am trying to build a chess board, but I keep getting an error that I can not seem to fix.

I have tried to find what it could be, but I am just lost.

my code:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;


public class chess extends JFrame {
int column_want, column_curr, column_diff;
int row_want, row_curr, row_diff;

    public static void main(String[] args) {
        System.out.println("Welcome to java");
        board frame = new boardLayout();
        frame.setSize(500, 500);
        frame.setTitle("chess Board");
        frame.setLocationRelativeTo(null); //center of frame
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setVisible(true);
    }
public void boardLayout() {
    JPanel board = new JPanel(new GridLayout(6,5,1,1)); // gridLayout (row, column, hgap, vgap)
    add(board, BorderLayout.NORTH);
}


}

it claims to be on line 12, but I do not know why there might be an error there.

line 12:

    board frame = new boardLayout();

Solution

  • You declare a type board, which is neither in the imports, nor in the file. Then you try to create an instance of a class boardLayout, which is unknown just as the type was.

    public void boardLayout() is a method declaration, it won't work as an constructor, unless it has the same name as the class it belongs to, and you will remove the return type void. Like public chess().

    A constructor does have its return type declared, as it is known, that it returns the object it instantiates. So new chess() will return a new object of a type chess.