import java.awt.*; import java.awt.event.*; /** * class GridBagButtons * * This demonstration application is intended as a descriptive tour of * how to implement the GridBagLayout manager. The GridBagLayout manager * is the most complicated of the layout managers, yet it is the MOST * powerful. To fully exploit this layout manager, you must really * exercise this code and write your own. Another important point is * that this layout manager works very closely with the GridBagConstraints * object. * * In general, the GridBagLayout manager divides the container into a * series of grids arranged in rows and columns. Each of the grids * doe NOT have to be the same size. The manager IMPLICITLY determines * the number of rows and columns! In addition, the manager allows its * clients to specify many parameters concerning exactly how each * component is arranged in its grid(s). These parameters are specified * as constraints, constraining the components to certain sizes, locations, * reactions to being resized, borders around the components, and more. * The GridBagConstraints class is used to specify these constraints. * See GridBagLayout and GridBagLayout class definitions for more infomation. * * This example demonstrates how to use the layout manager and its associated * constraints to manage a complex application. This application creates * four panels, each panel has a number of buttons. Each panel has a default * background color, and the buttons on the panel can be used to change * the background color. The four panels are added to the frame and are * managed by a GridBagLayout manager and the associated GrigBagConstraints. * Each panel in turn, manages its buttons with the same GridBagLayout * manager, but with a different GridBagConstraint for each button. * * Clicking on a button results in the title of the frame updating to * inform the user the name of the button pressed in addition to changing * the color of the panels background. * * NOTE: This code is for educational purposes, it is not what I would * call *clean* code. There are many places where there are hard coded * variables. In addition this code could be far more modular, and * other classes could be added. Translation: your code should be tighter * than this. * * @author Dan Mazzola * @version 1.0beta - 3/30/97 - used JDK 1.0 event handling * @version 1.1beta - 2/10/98 - uses new JDK 1.1 events and AWT features * */ public class GridBagButtons extends Frame implements ActionListener { /*----------------------------------------------------------------------*/ /* Delcare objects that are visible to the entire class */ /*----------------------------------------------------------------------*/ // The GridBagLayout manager object used throughout - only 1 needed!! // This manager keeps track of each component (or container) separately. // Therefore there is no need for a separate manager for each. GridBagLayout gridBagLayout = new GridBagLayout(); // The panel containing the "blueish" buttons and the buttons Panel bluesPanel = new Panel(); Button blueButton = new Button("Blue"); Button cyanButton = new Button("Cyan"); Button magentaButton = new Button("Magenta"); // The panel containing the "redish" buttons and the buttons Panel redsPanel = new Panel(); Button redButton = new Button("Red"); Button pinkButton = new Button("Pink"); // The panel with the "color-blending" buttons and the buttons Panel blendsPanel = new Panel(); Button darkerButton = new Button("Darker"); Button lighterButton = new Button("Lighter"); // The panel holding the "shades-of-gray" buttons and the buttons Panel graysPanel = new Panel(); Button whiteButton = new Button("White"); Button lightGrayButton = new Button("LightGray"); Button grayButton = new Button("Gray"); Button darkGrayButton = new Button("DarkGray"); Button blackButton = new Button("Black"); public GridBagButtons(String title) { // Call the Frame's constructor super(title); // Use this font as the default font for the application this.setFont(new Font("Serif", Font.PLAIN, 24)); // Exit upon closing of Frame this.addWindowListener(new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0);} }); /*-------------------------------------------------------------------*/ /* Set properties of the bluesPanel */ /* */ /* Set the background color to the starting color and set the layout */ /* manager to the class's GridBagLayout manager. */ /* */ /* Then constrain the buttons for the panel as documented below */ /*-------------------------------------------------------------------*/ bluesPanel.setBackground(Color.blue); bluesPanel.setLayout(gridBagLayout); constrain(bluesPanel, // add() to the bluesPanel... blueButton, // the blueButton... 0, 0, // in the 0,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // filling both X and Y space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 5, 5, 5, 5); // with an all 5 pixel border constrain(bluesPanel, // add() to the bluesPanel... cyanButton, // the cyanButton... 0, 1, // in the 0,1 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // filling both X and Y space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 15, 15, 15, 15); // with an all 15 pixel border constrain(bluesPanel, // add() to the bluesPanel... magentaButton, // the magentaButton... 0, 2, // in the 0,2 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // filling both X and Y space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 5, 5, 5, 5); // with an all 5 pixel border /*-------------------------------------------------------------------*/ /* Set properties of the graysPanel */ /* */ /* Set the background color to the starting color and set the layout */ /* manager to the class's GridBagLayout manager. */ /* */ /* Then constrain the buttons for the panel as documented below */ /*-------------------------------------------------------------------*/ graysPanel.setBackground(Color.lightGray); graysPanel.setLayout(gridBagLayout); constrain(graysPanel, // add() to the graysPanel... whiteButton, // the whiteButton... 0, 0, // in the 0,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.HORIZONTAL,// filling only X space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 10, 10, 10, 10); // with an all 10 pixel border constrain(graysPanel, // add() to the graysPanel... lightGrayButton, // the whiteButton... 1, 0, // in the 1,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.HORIZONTAL,// filling only X space... GridBagConstraints.CENTER, // anchored to the CENTER... /*NOTE*/ 0.5, 0.5, // that will slightly resize... 10, 10, 10, 10); // with an all 10 pixel border constrain(graysPanel, // add() to the graysPanel... grayButton, // the whiteButton... 2, 0, // in the 2,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.HORIZONTAL,// filling only X space... GridBagConstraints.CENTER, // anchored to the CENTER... /*NOTE*/ 3.0, 3.0, // that will resize the MOST... 10, 10, 10, 10); // with an all 10 pixel border constrain(graysPanel, // add() to the graysPanel... darkGrayButton, // the whiteButton... 3, 0, // in the 3,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.HORIZONTAL,// filling only X space... GridBagConstraints.CENTER, // anchored to the CENTER... /*NOTE*/ 0.5, 0.5, // that will slightly resize... 10, 10, 10, 10); // with an all 10 pixel border constrain(graysPanel, // add() to the graysPanel... blackButton, // the whiteButton... 4, 0, // in the 4,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.HORIZONTAL,// filling only X space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 10, 10, 10, 10); // with an all 10 pixel border /*-------------------------------------------------------------------*/ /* Set properties of the redsPanel */ /* */ /* Set the background color to the starting color and set the layout */ /* manager to the class's GridBagLayout manager. */ /* */ /* Then constrain the buttons for the panel as documented below */ /*-------------------------------------------------------------------*/ redsPanel.setBackground(Color.red); redsPanel.setLayout(gridBagLayout); constrain(redsPanel, // add() to the redsPanel... redButton, // the redButton... 0, 0, // in the 0,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // filling both X and Y space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 3.0, // constrained to resize in Y... 5, 0, 5, 0); // with 5 pixel top/bottom border constrain(redsPanel, // add() to the redsPanel... pinkButton, // the pinkButton... 1, 1, // in the 1,1 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // filling both X and Y space... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 3.0, // constrained to resize in Y... 5, 0, 5, 0); // with 5 pixel top/bottom border /*-------------------------------------------------------------------*/ /* Set properties of the blendsPanel */ /* */ /* Set the background color to the starting color and set the layout */ /* manager to the class's GridBagLayout manager. */ /* */ /* Then constrain the buttons for the panel as documented below */ /*-------------------------------------------------------------------*/ blendsPanel.setBackground(Color.yellow); blendsPanel.setLayout(gridBagLayout); constrain(blendsPanel, // add() to the blendsPanel... darkerButton, // the darlerButton... 0, 0, // in the 0,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.NONE, // that does NOT expand... GridBagConstraints.CENTER, // anchored to the CENTER 0.0, 0.0, // constrained NOT to resize... 9, 9, 9, 9); // with an all 9 pixel border constrain(blendsPanel, // add() to the blendsPanel... lighterButton, // the lighterButton... 1, 0, // in the 1,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.NONE, // that does not expand... GridBagConstraints.CENTER, // anchored to the CENTER... 0.0, 0.0, // constrained NOT to resize... 9, 9, 9, 9); // with an all 9 pixel border /*-------------------------------------------------------------------*/ /* Set properties of the Frame (this) */ /* */ /* Set the background color to the starting color and set the layout */ /* manager to the class's GridBagLayout manager. */ /* */ /* Then constrain the panels for the frame as documented below: */ /*-------------------------------------------------------------------*/ this.setLayout(gridBagLayout); this.setBackground(Color.green); constrain(this, // add() to the frame... bluesPanel, // the bluePanel... 0, 0, // in the 0,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.VERTICAL, // that expands in Y space... GridBagConstraints.NORTHWEST, // anchored to the NORTHWEST... 0.0, 0.0, // constrained NOT to resize... 5, 5, 5, 5); // with an all 5 pixel border constrain(this, // add() to the frame... redsPanel, // the redsPanel... 1, 0, // in the 1,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.BOTH, // that expands in X & Y space... GridBagConstraints.NORTH, // anchored to the NORTH... 1.0, 1.0, // that WILL resize in X & Y... 5, 5, 5, 5); // with an all 5 pixel border constrain(this, // add() to the frame... blendsPanel, // the blendsPanel... 2, 0, // in the 2,0 grid coordinate... 1, 1, // occupying 1x1 grids... GridBagConstraints.VERTICAL, // that expands in Y space... GridBagConstraints.NORTHEAST, // anchored to the NORTHEAST... 0.0, 0.0, // constrained NOT to resize... 5, 5, 5, 5); // with an all 5 pixel border constrain(this, // add() to the frame... graysPanel, // the grayPanel... 0, 1, // in the 0,1 grid coordinate... 3, 1, // occupying 3x1 grids... GridBagConstraints.HORIZONTAL,// that expands in X space... GridBagConstraints.SOUTH, // anchored to the SOUTH... 0.0, 0.0, // constrained NOT to resize... 5, 5, 5, 5); // with an all 5 pixel border addListenerToButtons(); } /** * Sets the GridBagConstraints settings for a component * and adds the constraints to the GridBagLayout manager for the * container along with the component. * * @param container The Container to add the component and constraint to * @param component The Component at manage * @param gridx The column grid coordinate * @param gridy The row grrid coordinate * @param gridwidth The number of columns to occupy * @param gridheight The number of rows to occupy * @param fill The dimension(s) to expand into as space allows * @param anchor The location to attach the component to * @param weightx The relative expansion along the row * @param weidhty The relative expansion along the column * @param top The top inset exterior to the component * @param bottom The bottom inset exterior to the component * @param left The left inset exterior to the component * @param right The right inset exterior to the component */ public void constrain( Container container, Component component, int gridx, int gridy, int gridwidth, int gridheight, int fill, int anchor, double weightx, double weighty, int top, int left, int bottom, int right) { GridBagConstraints constraint = new GridBagConstraints(); constraint.gridx = gridx; constraint.gridy = gridy; constraint.gridwidth = gridwidth; constraint.gridheight = gridheight; constraint.fill = fill; constraint.anchor = anchor; constraint.weightx = weightx; constraint.weighty = weighty; constraint.insets = new Insets(top, left, bottom, right); container.add(component, constraint); } /** * Register the actionListener to each button */ private void addListenerToButtons() { blueButton.addActionListener(this); cyanButton.addActionListener(this); magentaButton.addActionListener(this); redButton.addActionListener(this); pinkButton.addActionListener(this); darkerButton.addActionListener(this); lighterButton.addActionListener(this); whiteButton.addActionListener(this); lightGrayButton.addActionListener(this); grayButton.addActionListener(this); darkGrayButton.addActionListener(this); blackButton.addActionListener(this); } /** * Update the frame title with the label of the button clicked. * In addition, set the background color of the panel containing the * button to the color specified by the button's label. Just setting * the background color in itself does not result in updating the * display, therefore, call repaint() for the appropriate container. * * @param e The ActionEvent */ public void actionPerformed(ActionEvent e) { if (e.getSource() instanceof Button) { String buttonLabel = e.getActionCommand(); this.setTitle("You pressed the " + buttonLabel + " Button"); if (buttonLabel.equals("Blue")) { bluesPanel.setBackground(Color.blue); bluesPanel.repaint(); } else if (buttonLabel.equals("Cyan")) { bluesPanel.setBackground(Color.cyan); bluesPanel.repaint(); } else if (buttonLabel.equals("Magenta")) { bluesPanel.setBackground(Color.magenta); bluesPanel.repaint(); } else if (buttonLabel.equals("Red")) { redsPanel.setBackground(Color.red); redsPanel.repaint(); } else if (buttonLabel.equals("Pink")) { redsPanel.setBackground(Color.pink); redsPanel.repaint(); } else if (buttonLabel.equals("White")) { graysPanel.setBackground(Color.white); graysPanel.repaint(); } else if (buttonLabel.equals("LightGray")) { graysPanel.setBackground(Color.lightGray); graysPanel.repaint(); } else if (buttonLabel.equals("Gray")) { graysPanel.setBackground(Color.gray); graysPanel.repaint(); } else if (buttonLabel.equals("DarkGray")) { graysPanel.setBackground(Color.darkGray); graysPanel.repaint(); } else if (buttonLabel.equals("Black")) { graysPanel.setBackground(Color.black); graysPanel.repaint(); } else if (buttonLabel.equals("Darker")) { blendsPanel.setBackground(blendsPanel.getBackground().darker()); blendsPanel.repaint(); } else if (buttonLabel.equals("Lighter")) { blendsPanel.setBackground(blendsPanel.getBackground().brighter()); blendsPanel.repaint(); } } } public static void main(String[] args) { GridBagButtons gbb = new GridBagButtons("GridBagButtons Test"); gbb.pack(); gbb.show(); } }