/** * LayoutExample.java * @author Jeff Ondich, 27 October 2010 * * This program, combined with ButtonPanel and TricolorPanel, provides * a small demonstration of: * * - Layout using the GridBagLayout manager class. The content pane of * the frame window is split into to unequal-sized columns using a * very simple setup, as GridBagLayout setups go. * - Layout by putting panels in panels (see how TricolorPanel separates * itself into three subpanels). * - Connection between a button in one panel and an action in another. */ import java.awt.*; import java.awt.event.*; import javax.swing.*; public class LayoutExample { public static void main(String[] args) { JFrame frame = new JFrame("Layout example"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel contentPane = new JPanel(); contentPane.setOpaque(true); contentPane.setLayout(new GridBagLayout()); frame.setContentPane(contentPane); ButtonPanel buttonPanel = new ButtonPanel(Color.BLUE); GridBagConstraints buttonPanelConstraints = new GridBagConstraints(); buttonPanelConstraints.fill = GridBagConstraints.BOTH; buttonPanelConstraints.gridx = 0; buttonPanelConstraints.gridy = 0; buttonPanelConstraints.gridwidth = 1; buttonPanelConstraints.weightx = 1.0; buttonPanelConstraints.weighty = 1.0; contentPane.add(buttonPanel, buttonPanelConstraints); TricolorPanel tricolorPanel = new TricolorPanel(Color.GREEN, Color.GRAY, Color.YELLOW); GridBagConstraints tricolorPanelConstraints = new GridBagConstraints(); buttonPanel.addButtonListener(tricolorPanel); tricolorPanelConstraints.fill = GridBagConstraints.BOTH; tricolorPanelConstraints.gridx = 1; tricolorPanelConstraints.gridy = 0; tricolorPanelConstraints.gridwidth = 2; tricolorPanelConstraints.weightx = 5.0; tricolorPanelConstraints.weighty = 1.0; contentPane.add(tricolorPanel, tricolorPanelConstraints); frame.setSize(500, 300); frame.setVisible(true); } }