3

I've got 3 classes in my program.

public class Field extends JLabel{

    private int x, y;

    public Field(int x, int y){
        this.x = x;
        this.y = y;
        setOpaque(true);
        setMinimumSize(new Dimension(50,50));
        setPreferredSize(new Dimension(75,75));
        if((x + y) % 2 == 0)
            setBackground(Color.GREEN);
        else
            setBackground(Color.YELLOW);
    }


public class Board extends JPanel{

    public Field[][] fields = new Field[8][8];

    public Board(){
        setLayout(new GridLayout(8,8));
        setMinimumSize(new Dimension(500,500));
        setPreferredSize(new Dimension(550,550));
        setBackground(Color.RED);
        fillBoard();
    }

    private void fillBoard(){
        for(int i = 0; i < 8; ++i){
            for(int j = 0; j < 8; ++j){
                fields[i][j] = new Field(i, j);
                add(fields[i][j]);
            }
        }
    }

public class GUI extends JFrame{

    public Board board;

    private GUI(){
        board = new Board();
        setLayout(new FlowLayout());
        add(board);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        pack();
    }

    public static void main(String[] args){
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new GUI();
            }
        });
    }

}

Whenever I run the program, this appears instead of a yellow-green board. Can anyone help please?

4
  • Can you describe in more detail what you expect it to look like? Commented Jun 28, 2011 at 0:28
  • 4
    When I run your code, I get a green/yellow checkerboard with a slight red border on the right side and on the bottom. Commented Jun 28, 2011 at 0:37
  • Presumably just a typo in the question but the class Field doesn't get closed Commented Jun 28, 2011 at 0:43
  • 1
    Note that you should generally call pack() before setVisible(true). Commented Jun 28, 2011 at 7:16

2 Answers 2

3

Your posted code does not compile. Re-factoring into nested classes made it work correctly as shown, so I'm guessing you have a project level problem.

enter image description here

import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class GUI extends JFrame {

    private Board board;

    private GUI() {
        board = new Board();
        setLayout(new FlowLayout());
        add(board);
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setVisible(true);
        pack();
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new GUI();
            }
        });
    }

    class Board extends JPanel {

        public Field[][] fields = new Field[8][8];

        public Board() {
            setLayout(new GridLayout(8, 8));
            setMinimumSize(new Dimension(500, 500));
            setPreferredSize(new Dimension(550, 550));
            setBackground(Color.RED);
            fillBoard();
        }

        private void fillBoard() {
            for (int i = 0; i < 8; ++i) {
                for (int j = 0; j < 8; ++j) {
                    fields[i][j] = new Field(i, j);
                    add(fields[i][j]);
                }
            }
        }
    }

    class Field extends JLabel {

        private int x, y;

        public Field(int x, int y) {
            this.x = x;
            this.y = y;
            setOpaque(true);
            setMinimumSize(new Dimension(50, 50));
            setPreferredSize(new Dimension(75, 75));
            if ((x + y) % 2 == 0) {
                setBackground(Color.GREEN);
            } else {
                setBackground(Color.YELLOW);
            }
        }
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

For reference, this is really just a picture of @Hovercraft Full Of Eels' insightful comment.
best for 2D Graphics around +1
-1

You should set layout manager and add component to the contentPane on JFrame, inside GUI you should call:

getContentPane().setLayout(new FlowLayout());
getContentPane().add(board);

Your code compiles if both Board and Field are refactored to inner classes, but the result looks like this Result

To make the interface look without the red line comment setPreferredSize(new Dimension(550,550)); on your Board constructor so it become:

public Board(){
        int rows = 8,cols = 8;
    setLayout(new GridLayout(rows,cols));
    setMinimumSize(new Dimension(500,500));
    //setPreferredSize(new Dimension(550,550));
    setBackground(Color.RED);
    fillBoard();
}

The result after removing setPreferredSize()

4 Comments

Calling those methods directly on the JFrame (as the OP is doing) just delegates to the content pane, this should make no difference.
He didn't mention what version of jdk he was working with so my answer is not really wrong per se
I didn't downvote, but you're absolutely right. It looks like this behaviour was added (or at least documented) starting in Java 5. But I would hope the OP isn't using a dreadfully out of date version of Java.
Thanks for answer, but this didn't solve my problem. I am using the newest Java version - jre6

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.