001: /*
002: * Copyright (c) 2002-2007 JGoodies Karsten Lentzsch. All Rights Reserved.
003: *
004: * Redistribution and use in source and binary forms, with or without
005: * modification, are permitted provided that the following conditions are met:
006: *
007: * o Redistributions of source code must retain the above copyright notice,
008: * this list of conditions and the following disclaimer.
009: *
010: * o Redistributions in binary form must reproduce the above copyright notice,
011: * this list of conditions and the following disclaimer in the documentation
012: * and/or other materials provided with the distribution.
013: *
014: * o Neither the name of JGoodies Karsten Lentzsch nor the names of
015: * its contributors may be used to endorse or promote products derived
016: * from this software without specific prior written permission.
017: *
018: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
019: * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
020: * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
021: * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
022: * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
023: * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
024: * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
025: * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
026: * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
027: * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
028: * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029: */
030:
031: package com.jgoodies.forms.tutorial.basics;
032:
033: import javax.swing.*;
034:
035: import com.jgoodies.forms.factories.Borders;
036: import com.jgoodies.forms.layout.CellConstraints;
037: import com.jgoodies.forms.layout.FormLayout;
038:
039: /**
040: * Demonstrates the different FormLayout alignments.
041: *
042: * @author Karsten Lentzsch
043: * @version $Revision: 1.13 $
044: */
045: public final class AlignmentExample {
046:
047: public static void main(String[] args) {
048: try {
049: UIManager
050: .setLookAndFeel("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");
051: } catch (Exception e) {
052: // Likely PlasticXP is not in the class path; ignore.
053: }
054: JFrame frame = new JFrame();
055: frame.setTitle("Forms Tutorial :: Alignments");
056: frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
057: JComponent panel = new AlignmentExample().buildPanel();
058: frame.getContentPane().add(panel);
059: frame.pack();
060: frame.setVisible(true);
061: }
062:
063: public JComponent buildPanel() {
064: JTabbedPane tabbedPane = new JTabbedPane();
065: tabbedPane.putClientProperty("jgoodies.noContentBorder",
066: Boolean.TRUE);
067:
068: tabbedPane.add("Horizontal", buildHorizontalButtons());
069: tabbedPane.add("Vertical", buildVerticalButtons());
070: return tabbedPane;
071: }
072:
073: private JComponent buildHorizontalButtons() {
074: FormLayout layout = new FormLayout(
075: "left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref, 15px, pref",
076: "pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref");
077:
078: // Create a panel that uses the layout.
079: JPanel panel = new JPanel(layout);
080:
081: // Set a default border.
082: panel.setBorder(Borders.DIALOG_BORDER);
083:
084: // Create a reusable CellConstraints instance.
085: CellConstraints cc = new CellConstraints();
086:
087: // Add components to the panel.
088: panel.add(new JLabel("Left"), cc.xy(1, 1));
089: panel.add(new JButton("Name"), cc.xy(1, 3));
090: panel.add(new JButton("Phone"), cc.xy(1, 5));
091: panel.add(new JButton("Fax"), cc.xy(1, 7));
092: panel.add(new JButton("Email"), cc.xy(1, 9));
093: panel.add(new JButton("Address"), cc.xy(1, 11));
094:
095: panel.add(new JLabel("Center"), cc.xy(3, 1));
096: panel.add(new JButton("Name"), cc.xy(3, 3));
097: panel.add(new JButton("Phone"), cc.xy(3, 5));
098: panel.add(new JButton("Fax"), cc.xy(3, 7));
099: panel.add(new JButton("Email"), cc.xy(3, 9));
100: panel.add(new JButton("Address"), cc.xy(3, 11));
101:
102: panel.add(new JLabel("Right"), cc.xy(5, 1));
103: panel.add(new JButton("Name"), cc.xy(5, 3));
104: panel.add(new JButton("Phone"), cc.xy(5, 5));
105: panel.add(new JButton("Fax"), cc.xy(5, 7));
106: panel.add(new JButton("Email"), cc.xy(5, 9));
107: panel.add(new JButton("Address"), cc.xy(5, 11));
108:
109: panel.add(new JLabel("Fill"), cc.xy(7, 1, "center, center"));
110: panel.add(new JButton("Name"), cc.xy(7, 3));
111: panel.add(new JButton("Phone"), cc.xy(7, 5));
112: panel.add(new JButton("Fax"), cc.xy(7, 7));
113: panel.add(new JButton("Email"), cc.xy(7, 9));
114: panel.add(new JButton("Address"), cc.xy(7, 11));
115:
116: panel.add(new JLabel("Default"), cc.xy(9, 1, "center, center"));
117: panel.add(new JButton("Name"), cc.xy(9, 3));
118: panel.add(new JButton("Phone"), cc.xy(9, 5));
119: panel.add(new JButton("Fax"), cc.xy(9, 7));
120: panel.add(new JButton("Email"), cc.xy(9, 9));
121: panel.add(new JButton("Address"), cc.xy(9, 11));
122:
123: return panel;
124: }
125:
126: private JComponent buildVerticalButtons() {
127: FormLayout layout = new FormLayout(
128: "pref, 8dlu, pref, 4dlu, pref",
129: "top:pref, 9dlu, center:pref, 9dlu, bottom:pref, 9dlu, fill:pref, 9dlu, pref");
130:
131: // Create a panel that uses the layout.
132: JPanel panel = new JPanel(layout);
133:
134: // Set a default border.
135: panel.setBorder(Borders.DIALOG_BORDER);
136:
137: // Create a reusable CellConstraints instance.
138: CellConstraints cc = new CellConstraints();
139:
140: // Add components to the panel.
141: panel.add(new JLabel("Top"), cc.xy(1, 1));
142: panel.add(createSmallButton(), cc.xy(3, 1));
143: panel.add(createMediumButton(), cc.xy(5, 1));
144:
145: panel.add(new JLabel("Center"), cc.xy(1, 3));
146: panel.add(createSmallButton(), cc.xy(3, 3));
147: panel.add(createMediumButton(), cc.xy(5, 3));
148:
149: panel.add(new JLabel("Bottom"), cc.xy(1, 5));
150: panel.add(createSmallButton(), cc.xy(3, 5));
151: panel.add(createMediumButton(), cc.xy(5, 5));
152:
153: panel.add(new JLabel("Fill"), cc.xy(1, 7));
154: panel.add(createSmallButton(), cc.xy(3, 7));
155: panel.add(createMediumButton(), cc.xy(5, 7));
156:
157: panel.add(new JLabel("Default"), cc.xy(1, 9));
158: panel.add(createSmallButton(), cc.xy(3, 9));
159: panel.add(createMediumButton(), cc.xy(5, 9));
160:
161: return panel;
162: }
163:
164: private JButton createSmallButton() {
165: return new JButton("<html>One</html>");
166: }
167:
168: private JButton createMediumButton() {
169: return new JButton("<html>One<br>Two</html>");
170: }
171:
172: }
|