Code for class LayoutDemo:
import java.awt.*;
import javax.swing.*;
public class LayoutDemo extends JFrame
{
private JPanel p;
private JButton top1, top2, top3;
private JButton middle1, middle2, middle3;
private JButton bottom1, bottom2, bottom3;
public LayoutDemo(String title)
{
setTitle(title);
setSize(200,200);
setLocation(200,200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //see note 2
// Create the panel.
p = new JPanel();
//using four argument constructor as in 5th instruction
p.setLayout(new GridLayout(3,3,10,10));
// Create the buttons.
top1 = new JButton("O");
top2 = new JButton("X");
top3 = new JButton("O");
middle1 = new JButton("O");
middle2 = new JButton(" ");
middle3 = new JButton("X");
bottom1 = new JButton(" ");
bottom2 = new JButton(" ");
bottom3 = new JButton("X");
// Add the buttons to the panel.
p.add(top1);
p.add(top2);
p.add(top3);
p.add(middle1);
p.add(middle2);
p.add(middle3);
p.add(bottom1);
p.add(bottom2);
p.add(bottom3);
// Get the content pane of the frame.
Container cp = getContentPane();
// Add the panel to the content pane;
cp.add(p);
}
}
Code for class LayoutDemoTest:
public class LayoutDemoTest
{
public LayoutDemoTest()
{
}
public static void main(String[] args)
{
LayoutDemo ld =
new LayoutDemo("Tic-tac-toe");
ld.setVisible(true);
}
}