1 30 31 package com.jgoodies.forms.tutorial.basics; 32 33 import javax.swing.*; 34 35 import com.jgoodies.forms.factories.Borders; 36 import com.jgoodies.forms.layout.CellConstraints; 37 import com.jgoodies.forms.layout.FormLayout; 38 39 45 public final class AlignmentExample { 46 47 48 public static void main(String [] args) { 49 try { 50 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 51 } catch (Exception e) { 52 } 54 JFrame frame = new JFrame(); 55 frame.setTitle("Forms Tutorial :: Alignments"); 56 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 57 JComponent panel = new AlignmentExample().buildPanel(); 58 frame.getContentPane().add(panel); 59 frame.pack(); 60 frame.show(); 61 } 62 63 64 public JComponent buildPanel() { 65 JTabbedPane tabbedPane = new JTabbedPane(); 66 tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE); 67 68 tabbedPane.add("Horizontal", buildHorizontalButtons()); 69 tabbedPane.add("Vertical", buildVerticalButtons()); 70 return tabbedPane; 71 } 72 73 74 private JComponent buildHorizontalButtons() { 75 FormLayout layout = new FormLayout( 76 "left:pref, 15px, center:pref, 15px, right:pref, 15px, fill:pref, 15px, pref", 77 "pref, 12px, pref, 4px, pref, 4px, pref, 4px, pref, 4px, pref"); 78 79 JPanel panel = new JPanel(layout); 81 82 panel.setBorder(Borders.DIALOG_BORDER); 84 85 CellConstraints cc = new CellConstraints(); 87 88 panel.add(new JLabel("Left"), cc.xy(1, 1)); 90 panel.add(new JButton("Name"), cc.xy(1, 3)); 91 panel.add(new JButton("Phone"), cc.xy(1, 5)); 92 panel.add(new JButton("Fax"), cc.xy(1, 7)); 93 panel.add(new JButton("Email"), cc.xy(1, 9)); 94 panel.add(new JButton("Address"), cc.xy(1, 11)); 95 96 panel.add(new JLabel("Center"), cc.xy(3, 1)); 97 panel.add(new JButton("Name"), cc.xy(3, 3)); 98 panel.add(new JButton("Phone"), cc.xy(3, 5)); 99 panel.add(new JButton("Fax"), cc.xy(3, 7)); 100 panel.add(new JButton("Email"), cc.xy(3, 9)); 101 panel.add(new JButton("Address"), cc.xy(3, 11)); 102 103 panel.add(new JLabel("Right"), cc.xy(5, 1)); 104 panel.add(new JButton("Name"), cc.xy(5, 3)); 105 panel.add(new JButton("Phone"), cc.xy(5, 5)); 106 panel.add(new JButton("Fax"), cc.xy(5, 7)); 107 panel.add(new JButton("Email"), cc.xy(5, 9)); 108 panel.add(new JButton("Address"), cc.xy(5, 11)); 109 110 panel.add(new JLabel("Fill"), cc.xy(7, 1, "center, center")); 111 panel.add(new JButton("Name"), cc.xy(7, 3)); 112 panel.add(new JButton("Phone"), cc.xy(7, 5)); 113 panel.add(new JButton("Fax"), cc.xy(7, 7)); 114 panel.add(new JButton("Email"), cc.xy(7, 9)); 115 panel.add(new JButton("Address"), cc.xy(7, 11)); 116 117 panel.add(new JLabel("Default"), cc.xy(9, 1, "center, center")); 118 panel.add(new JButton("Name"), cc.xy(9, 3)); 119 panel.add(new JButton("Phone"), cc.xy(9, 5)); 120 panel.add(new JButton("Fax"), cc.xy(9, 7)); 121 panel.add(new JButton("Email"), cc.xy(9, 9)); 122 panel.add(new JButton("Address"), cc.xy(9, 11)); 123 124 return panel; 125 } 126 127 128 private JComponent buildVerticalButtons() { 129 FormLayout layout = new FormLayout( 130 "pref, 8dlu, pref, 4dlu, pref", 131 "top:pref, 9dlu, center:pref, 9dlu, bottom:pref, 9dlu, fill:pref, 9dlu, pref"); 132 133 JPanel panel = new JPanel(layout); 135 136 panel.setBorder(Borders.DIALOG_BORDER); 138 139 CellConstraints cc = new CellConstraints(); 141 142 panel.add(new JLabel("Top"), cc.xy(1, 1)); 144 panel.add(createSmallButton(), cc.xy(3, 1)); 145 panel.add(createMediumButton(), cc.xy(5, 1)); 146 147 panel.add(new JLabel("Center"), cc.xy(1, 3)); 148 panel.add(createSmallButton(), cc.xy(3, 3)); 149 panel.add(createMediumButton(), cc.xy(5, 3)); 150 151 panel.add(new JLabel("Bottom"), cc.xy(1, 5)); 152 panel.add(createSmallButton(), cc.xy(3, 5)); 153 panel.add(createMediumButton(), cc.xy(5, 5)); 154 155 panel.add(new JLabel("Fill"), cc.xy(1, 7)); 156 panel.add(createSmallButton(), cc.xy(3, 7)); 157 panel.add(createMediumButton(), cc.xy(5, 7)); 158 159 panel.add(new JLabel("Default"), cc.xy(1, 9)); 160 panel.add(createSmallButton(), cc.xy(3, 9)); 161 panel.add(createMediumButton(), cc.xy(5, 9)); 162 163 return panel; 164 } 165 166 private JButton createSmallButton() { 167 return new JButton("<html>One</html>"); 168 } 169 170 private JButton createMediumButton() { 171 return new JButton("<html>One<br>Two</html>"); 172 } 173 174 175 } 176 177 | Popular Tags |