ManyPanels.java

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;


/**
Three panels, three times the excitement!
*/
class ManyPanels extends JFrame
{
    // Just plop the main() method right in the frame class...
    public static void main(String argv[])
    {
        ManyPanels f = new ManyPanels();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.show();
    }


    public ManyPanels()
    {
        Toolkit kit = Toolkit.getDefaultToolkit();
        Dimension ss = kit.getScreenSize();
        setSize(ss.width/3,ss.height/4);
        setLocation(ss.width/4,ss.height/4);
        setTitle("CISC370-011 -- multiple panels");
        Container contentPane = getContentPane();

        // Create a top-level panel to hold multiple ButtonPanels.
        JPanel top = new JPanel();
        contentPane.add(top);

        // Create three distinct ButtonPanels.
        for (int i=0; i<3; i++)
            top.add(new ButtonPanel());
    }
}


/**
Simple panel with three buttons, each of which sets the 
panel's background color.
*/
class ButtonPanel extends JPanel
{
    public ButtonPanel() {
        mkButton("Red",Color.RED);
        mkButton("Yellow",Color.YELLOW);
        mkButton("Green",Color.GREEN);    
    }


    void mkButton(String name,final Color bg) {
        JButton jb = new JButton(name);
        add(jb);
        jb.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e)
                { setBackground(bg); }
            });
    }
}