import java.awt.*; // for Graphics, Color import javax.swing.*; // for JFrame, JPanel public class Ch13_3b // draw a Checkerboard using JButtons { public static void main(String[] args) { JFrame frame=new JFrame("Exercise12_1"); // establish title for JFrame frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // establish what to do if JFrame is closed frame.setSize(340,360); // set size to something reasonable for this GUI JPanel p1=new JPanel(new GridLayout(8,8)); // we will insert 64 JButtons into an 8x8 grid JButton[][] b=new JButton[8][8]; // create an 8x8 array for the JButtons for(int i=0;i<8;i++) for(int j=0;j<8;j++) { b[i][j]=new JButton(); // create the next JButton, notice we pass nothing to the constructor b[i][j].setPreferredSize(new Dimension(40,40)); // set its size and color if((i+j)%2==0) b[i][j].setBackground(Color.RED); else b[i][j].setBackground(Color.BLACK); p1.add(b[i][j]); // add it to the JPanel } frame.add(p1); // add the JPanel to this JFrame and make the JFrame visible frame.setVisible(true); } }