I've written a code that creates a Checkerboard Array application.

There are no errors when I compile the code.

Once I run the application, the window pops up but its just a blank white window.

The code is as follows:

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

public class Checkerboard 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 Checkerboard()
    {
        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 p.350
        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);
            }

}

Recommended Answers

All 3 Replies

Looks like you populate the content of your Panels, but you never add the panels to the main window.

JC gave you the response but there is a lot of mistakes in your coding. Don't use System.exit(0); in closing event, it's like you kill your app two times. Then there will be a problem if the end user type letters in your textfield! Try to extend your textfield to have a better custom object, you can even add placeholders.

If we're discussing the code in general the most scary thing is that it uses AWT.
AWT was replaced in the last century by Swing, and Swing has now been superceeded by JavaFX (although most apps are still using Swing).
Why still using AWT????

(Not to mention using == with Strings.)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.