Click here to Skip to main content
15,908,254 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I'm a beginner and I've written a code that creates a Checkerboard Array application.

There are no errors when I compile the code but once I successfully compile and run the application, the application window pops up but its just a blank white window.

What I have tried:

import java.awt.*;
import java.awt.event.*;

public class Checkboard extends Frame implements ActionListener
{
    //declare variables
    TextField[] board = new TextField[16];
    TextField fieldStart = new TextField(5);
    TextField fieldStop = new TextField(5);
    TextField fieldStep = new TextField(5);
    int start, stop, step;
    Label LabelStop = new Label("Start");
    Label LabelStart = new Label("Stop");
    Label LabelStep = new Label("Step");
    Button goButton = new Button("Go");
    Button clearButton = new Button("Clear");
    Panel fieldPanel = new Panel();
    Panel checkerPanel = new Panel();
    Panel buttonsPanel = new Panel();
    public Checkboard()
    {
        start = 0;
        stop = 0;
        step = 0;
        this.setLayout(new BorderLayout());
        buttonsPanel.setLayout(new FlowLayout());
        for(int i = 0; i<16; i++)
        {
            board[i] =  new TextField();
            board[i].setText("" + i);
            board[i].setEditable(false);
            checkerPanel.add(board[i]);
        }
        checkerPanel.setLayout(new GridLayout(4, 4));
        fieldPanel.setLayout(new GridLayout(2, 3));
        fieldPanel.add(fieldStart);
        fieldPanel.add(fieldStop);
        fieldPanel.add(fieldStep);
        fieldPanel.add(LabelStart);
        fieldPanel.add(LabelStop);
        fieldPanel.add(LabelStep);
        buttonsPanel.add(goButton);
        buttonsPanel.add(clearButton);
        goButton.addActionListener(this);
        //add windowListener
        addWindowListener(
            new WindowAdapter()
            {
                public void windowClosing(WindowEvent e)
                {
                    System.exit(0);
                }
            }
        );
    }
    public void actionPerformed(ActionEvent e)
        {
            String arg = e.getActionCommand();
            if(arg == "Go")
            {
                start = Integer.parseInt(fieldStart.getText());
                stop = Integer.parseInt(fieldStop.getText());
                step = Integer.parseInt(fieldStep.getText());
                if (start < 0 || stop < 0 || step < 0) arg = "clear";
                for (int i=0; i<16; i++)
                    board[i].setBackground(Color.magenta);
                for (int i=start; i<=stop; i=i+step)
                    board[i].setBackground(Color.yellow);
            }
            if (arg== "Clear")
            {
                fieldStop.setText("");
                fieldStart.setText("");
                fieldStep.setText("");
                for (int i=0; i<16; i++)
                board[i].setBackground(Color.white);
            }
        }
            public static void main(String[] args)
            {
                Checkerboard f = new Checkerboard();
                f.setBounds(50, 100, 300, 400);
                f.setTitle("Checkerboard Array");
                f.setVisible(true);
            }
}
Posted
Comments
Mohibur Rashid 10-Sep-18 20:17pm    
Does this even compile? Your class name is Checkboard and you have declared Checkerboard in main.

You also need to add this line in your constructor
this.add(fieldPanel);
Member 13652359 11-Sep-18 13:19pm    
My bad, I got 2 files of the same code.
I'll update the question now with the correct code.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900