import javax.swing.*; // for JFrame, JLabel, JPanel import java.awt.*; // for Graphics public class Ch13_4 // Creates a GUI multiplication table { 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(700,700); // set size to something reasonable for this GUI MultPanel mt=new MultPanel(); // create the JPanel which will draw the multiplication table frame.add(mt); // insert the JPanel into the JFrame and make the JFrame visible frame.setVisible(true); } public static class MultPanel extends JPanel { public void paintComponent(Graphics g) // draw the parts of the multiplication table { g.setFont(new Font("Courier", Font.BOLD, 36)); // use big font for the title g.drawString("Multiplication Table", 100, 50); // write the title near the top, somewhat centered g.setFont(new Font("Courier", Font.PLAIN, 16)); // change font size for the numbers for(int i=1;i<10;i++) // output the numbers running along the top of the table (the column numbers) g.drawString(""+i,100+i*50, 100); for(int i=1;i<10;i++) // now do each row of the table { g.drawString(""+i,100, 100+i*50); // output the leftmost number (the row number) for(int j=1;j<10;j++) // now for each column g.drawString(""+(i*j), 100+i*50, 100+j*50); // compute i*j and place the number in its proper place in the table } // requires a little math and experimentation to get this to print out correctly g.drawRect(130,130,450,450); // surround the table with a rectangle to make it look more like a table } // note: we could add grid lines between each row and column - how would you do this? } }