0

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();
Ereun1939
  • 3
  • 3
  • `new` is for object contruction - not for calling functions (especially with `void` return type..) – rzysia Apr 03 '15 at 08:53

2 Answers2

2

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.

arataj
  • 373
  • 3
  • 12
  • I am trying to implement your suggestions, but I must not be understanding them. I removed `void` from `public void boardLayout`, I tried renaming the constructor to `chess`, but I am still finding the same error on the same line. I moved `JPanel board = new JPanel(new GridLayout(6,5,1,1));` up to the variable declare block, but it changed nothing but the line the error was on. @EddyG – Ereun1939 Apr 03 '15 at 18:20
  • There is nothing wrong with `new JPanel()` being within the constructor. Did you modify the types to be correct, like `chess frame = new chess()`? – arataj Apr 04 '15 at 20:24
0

You are mixing up syntax element. You only need to use the new keyword when creating new objects. When calling methods, no need to use "new".

Hint on naming: a method name should contain a verb, so createBoard would be more "java style".

GhostCat
  • 137,827
  • 25
  • 176
  • 248