import javax.swing.*; // for JFrame, JLabel, JPanel import java.awt.*; // for GridLayout import java.util.*; // for Random public class Ch12_14 // Creates a GUI that will place into 100 JLabels a random number in each, and then { // insert the 100 JLabels into a JPanel using a 10x10 grid, the alignment of each public static void main(String[] args) // JLabel will be center-justified both horizontally and vertically { 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(600,600); // set size to something reasonable for this GUI Random g=new Random(); JLabel[][] lab=new JLabel[10][10]; // create a 2-d array of JLabels to store random numbers JPanel p1=new JPanel(new GridLayout(10,10)); for(int i=0;i<10;i++) for(int j=0;j<10;j++) { lab[i][j]=new JLabel(""+g.nextInt(2)); // instantiate each JLabel with a random binary number as its string lab[i][j].setHorizontalAlignment(AbstractButton.CENTER); // set the String's position within the JLabel lab[i][j].setVerticalAlignment(AbstractButton.CENTER); p1.add(lab[i][j]); // add the latest JLabel to the JPanel } frame.add(p1); // add the JPanel to this JFrame and make the JFrame visible frame.setVisible(true); } }