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);
}
}