0

I have this for a checker board and I connected it to a JPanel:

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Color;

public class CheckerBoard extends JFrame
{

public void paint(Graphics g)
{
  int row;
  int col;
  int x;
  int y;

  for ( row = 0;  row < 9;  row++ )
  {
     for ( col = 0;  col < 8;  col++)
     {
        x = col * 22;
        y = row * 22;
         if ( (row % 2) == (col % 2) )
           g.setColor(Color.WHITE);
        else
           g.setColor(Color.BLACK);

        g.fillRect(x, y, 22, 22);
     }
  }
 }

This is the Connceted JPanel:

import javax.swing.*;
import java.awt.Graphics;
import java.awt.Color;
public class JFRAME
{   public static void main() 
    {

        CheckerBoard check = new CheckerBoard();
        check.setTitle("CheckerBoard");
        check.setSize(180, 200);
        check.setDefaultCloseOperation(EXIT_ON_CLOSE);
        check.setVisible(true);
    }
}    

When I compile the Jpanel it says "cannot find symbol-variable EXIT_ON_CLOSE"

Im trying to make an 8 by 8 checker board that prompts the user for the number of rows and columns of the board before displaying them.

Selinnxox
  • 45
  • 1
  • 7

3 Answers3

2

EXIT_ON_CLOSE is actually a public static final int field defined in JFrame, this means you need to use JFrame to reference it, like JFrame.EXIT_ON_CLOSE

Or you could add import static javax.swing.JFrame.EXIT_ON_CLOSE; to your import statements

Updated...

As pointed out by Hovercraft, you shouldn't be overriding paint of top level containers like JFrame, there are lots of reasons for this, but it's generally going to give you lots of trouble as your application complexity grows.

Instead, start with a JPanel and override it's paintComponent method, making sure you call super.paintComponent first. Then, simply add this to what ever container you want to use.

Have a look at Performing Custom Painting for more details

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
0

Welcome to SO. It's good that you're trying to provide some context. However, in this case, the answer appears to just be: You need WindowConstants.EXIT_ON_CLOSE instead of just EXIT_ON_CLOSE

Foon
  • 6,148
  • 11
  • 40
  • 42
  • (Note that per answer in http://stackoverflow.com/questions/7799940/jframe-exit-on-close-java, WindowConstants.EXIT_ON_CLOSE is prefered to JFRAME.EXIT_ON_CLOSE but they're both the same integer) Also... does that solve your question, or was there another issue beyond the compile error? – Foon Oct 09 '15 at 03:39
0

Should be JFrame.EXIT_ON_CLOSE And please don't call your class JFRAME that is just messed up!

John3136
  • 28,809
  • 4
  • 51
  • 69