001: /*
002: * @(#)GridBagLayoutDemo.java 1.5 06/10/10
003: *
004: * Copyright 1990-2006 Sun Microsystems, Inc. All Rights Reserved.
005: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License version
009: * 2 only, as published by the Free Software Foundation.
010: *
011: * This program is distributed in the hope that it will be useful, but
012: * WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * General Public License version 2 for more details (a copy is
015: * included at /legal/license.txt).
016: *
017: * You should have received a copy of the GNU General Public License
018: * version 2 along with this work; if not, write to the Free Software
019: * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
020: * 02110-1301 USA
021: *
022: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
023: * Clara, CA 95054 or visit www.sun.com if you need additional
024: * information or have any questions.
025: */
026: package basis.demos;
027:
028: import java.awt.*;
029: import basis.DemoButton;
030:
031: public class GridBagLayoutDemo extends Demo {
032: private GridBagLayout gbl;
033: private GridBagConstraints gbc;
034:
035: public GridBagLayoutDemo() {
036: gbl = new GridBagLayout();
037: gbc = new GridBagConstraints();
038: setLayout(gbl);
039: int cols = 7;
040: double weightx = 1.0;
041: double weighty = 0.0;
042: for (int x = 0; x < cols; x++) {
043: DemoButton b = new DemoButton("" + x);
044: add(b, x, 0, 1, 1, GridBagConstraints.BOTH, 0, 0,
045: new Insets(0, 0, 0, 0), GridBagConstraints.CENTER,
046: weightx, weighty);
047: }
048: DemoButton aC = new DemoButton("x");
049: DemoButton aN = new DemoButton("x");
050: DemoButton aS = new DemoButton("x");
051: DemoButton aE = new DemoButton("x");
052: DemoButton aW = new DemoButton("x");
053: DemoButton aNW = new DemoButton("x");
054: DemoButton aNE = new DemoButton("x");
055: DemoButton aSE = new DemoButton("x");
056: DemoButton aSW = new DemoButton("x");
057: add(aC, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
058: 0, 0, 0, 0), GridBagConstraints.CENTER, weightx,
059: weighty);
060: add(aN, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
061: 0, 0, 0, 0), GridBagConstraints.NORTH, weightx, weighty);
062: add(aS, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
063: 0, 0, 0, 0), GridBagConstraints.SOUTH, weightx, weighty);
064: add(aE, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
065: 0, 0, 0, 0), GridBagConstraints.EAST, weightx, weighty);
066: add(aW, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
067: 0, 0, 0, 0), GridBagConstraints.WEST, weightx, weighty);
068: add(aNW, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
069: 0, 0, 0, 0), GridBagConstraints.NORTHWEST, weightx,
070: weighty);
071: add(aNE, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
072: 0, 0, 0, 0), GridBagConstraints.NORTHEAST, weightx,
073: weighty);
074: add(aSE, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
075: 0, 0, 0, 0), GridBagConstraints.SOUTHEAST, weightx,
076: weighty);
077: add(aSW, 0, 1, 3, 3, GridBagConstraints.NONE, 0, 0, new Insets(
078: 0, 0, 0, 0), GridBagConstraints.SOUTHWEST, weightx,
079: weighty);
080: DemoButton normal = new DemoButton("n");
081: DemoButton padded = new DemoButton("p");
082: DemoButton insets = new DemoButton("i");
083: add(normal, 3, 1, 1, 1, GridBagConstraints.NONE, 0, 0,
084: new Insets(0, 0, 0, 0), GridBagConstraints.CENTER,
085: weightx, weighty);
086: add(padded, 3, 2, 1, 1, GridBagConstraints.NONE, 8, 8,
087: new Insets(0, 0, 0, 0), GridBagConstraints.CENTER,
088: weightx, weighty);
089: add(insets, 3, 3, 1, 1, GridBagConstraints.NONE, 0, 0,
090: new Insets(8, 8, 8, 8), GridBagConstraints.CENTER,
091: weightx, weighty);
092: DemoButton wide = new DemoButton("two");
093: DemoButton remainder = new DemoButton("rem");
094: DemoButton relative = new DemoButton("rel");
095: add(wide, 4, 1, 2, 1, GridBagConstraints.HORIZONTAL, 0, 0,
096: new Insets(0, 0, 0, 0), GridBagConstraints.CENTER,
097: weightx, weighty);
098: add(remainder, 4, 2, GridBagConstraints.REMAINDER, 1,
099: GridBagConstraints.HORIZONTAL, 0, 0, new Insets(0, 0,
100: 0, 0), GridBagConstraints.CENTER, weightx,
101: weighty);
102: add(relative, 4, 3, GridBagConstraints.RELATIVE, 1,
103: GridBagConstraints.HORIZONTAL, 0, 0, new Insets(0, 0,
104: 0, 0), GridBagConstraints.CENTER, weightx,
105: weighty);
106: }
107:
108: private void add(Component component, int gridx, int gridy,
109: int gridwidth, int gridheight, int fill, int ipadx,
110: int ipady, Insets insets, int anchor, double weightx,
111: double weighty) {
112: gbc.gridx = gridx;
113: gbc.gridy = gridy;
114: gbc.gridwidth = gridwidth;
115: gbc.gridheight = gridheight;
116: gbc.fill = fill;
117: gbc.ipadx = ipadx;
118: gbc.ipady = ipady;
119: gbc.insets = insets;
120: gbc.anchor = anchor;
121: gbc.weightx = weightx;
122: gbc.weighty = weighty;
123: gbl.setConstraints(component, gbc);
124: add(component);
125: }
126: }
|