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 46 public final class CellAlignmentExample { 47 48 49 public static void main(String [] args) { 50 try { 51 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 52 } catch (Exception e) { 53 } 55 JFrame frame = new JFrame(); 56 frame.setTitle("Forms Tutorial :: Cell Alignments"); 57 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 58 JComponent panel = new CellAlignmentExample().buildPanel(); 59 frame.getContentPane().add(panel); 60 frame.pack(); 61 frame.show(); 62 } 63 64 65 public JComponent buildPanel() { 66 JTabbedPane tabbedPane = new JTabbedPane(); 67 tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE); 68 69 tabbedPane.add(buildHorizontalPanel(), "Horizontal"); 70 tabbedPane.add(buildVerticalPanel(), "Vertical"); 71 return tabbedPane; 72 } 73 74 75 private JComponent buildHorizontalPanel() { 76 FormLayout layout = new FormLayout( 77 "left:pref:g, center:pref:g, right:pref:g, pref:g", 78 "pref, 8dlu, pref, pref, pref, pref, pref"); 79 JPanel panel = new JPanel(layout); 80 panel.setBorder(Borders.DIALOG_BORDER); 81 82 panel.add(new JLabel("Left"), "1, 1, c, c"); 83 panel.add(new JLabel("Center"), "2, 1, c, c"); 84 panel.add(new JLabel("Right"), "3, 1, c, c"); 85 panel.add(new JLabel("Default"), "4, 1, c, c"); 86 87 int row = 3; 88 addHorizontalButton(panel, 1, row, CellConstraints.DEFAULT); 89 addHorizontalButton(panel, 2, row, CellConstraints.DEFAULT); 90 addHorizontalButton(panel, 3, row, CellConstraints.DEFAULT); 91 addHorizontalButton(panel, 4, row, CellConstraints.DEFAULT); 92 93 row++; 94 addHorizontalButton(panel, 1, row, CellConstraints.FILL); 95 addHorizontalButton(panel, 2, row, CellConstraints.FILL); 96 addHorizontalButton(panel, 3, row, CellConstraints.FILL); 97 addHorizontalButton(panel, 4, row, CellConstraints.FILL); 98 99 row++; 100 addHorizontalButton(panel, 1, row, CellConstraints.LEFT); 101 addHorizontalButton(panel, 2, row, CellConstraints.LEFT); 102 addHorizontalButton(panel, 3, row, CellConstraints.LEFT); 103 addHorizontalButton(panel, 4, row, CellConstraints.LEFT); 104 105 row++; 106 addHorizontalButton(panel, 1, row, CellConstraints.CENTER); 107 addHorizontalButton(panel, 2, row, CellConstraints.CENTER); 108 addHorizontalButton(panel, 3, row, CellConstraints.CENTER); 109 addHorizontalButton(panel, 4, row, CellConstraints.CENTER); 110 111 row++; 112 addHorizontalButton(panel, 1, row, CellConstraints.RIGHT); 113 addHorizontalButton(panel, 2, row, CellConstraints.RIGHT); 114 addHorizontalButton(panel, 3, row, CellConstraints.RIGHT); 115 addHorizontalButton(panel, 4, row, CellConstraints.RIGHT); 116 117 return panel; 118 } 119 120 121 private JComponent buildVerticalPanel() { 122 FormLayout layout = new FormLayout( 123 "left:pref, 8dlu, l:p, l:p, l:p, l:p, l:p", 124 "top:pref:g, center:pref:g, bottom:pref:g, pref:g"); 125 JPanel panel = new JPanel(layout); 126 panel.setBorder(Borders.DIALOG_BORDER); 127 128 panel.add(new JLabel("Top"), "1, 1, r, c"); 129 panel.add(new JLabel("Center"), "1, 2, r, c"); 130 panel.add(new JLabel("Bottom"), "1, 3, r, c"); 131 panel.add(new JLabel("Default"), "1, 4, r, c"); 132 133 int col = 3; 134 addVerticalButton(panel, col, 1, CellConstraints.DEFAULT); 135 addVerticalButton(panel, col, 2, CellConstraints.DEFAULT); 136 addVerticalButton(panel, col, 3, CellConstraints.DEFAULT); 137 addVerticalButton(panel, col, 4, CellConstraints.DEFAULT); 138 139 col++; 140 addVerticalButton(panel, col, 1, CellConstraints.FILL); 141 addVerticalButton(panel, col, 2, CellConstraints.FILL); 142 addVerticalButton(panel, col, 3, CellConstraints.FILL); 143 addVerticalButton(panel, col, 4, CellConstraints.FILL); 144 145 col++; 146 addVerticalButton(panel, col, 1, CellConstraints.TOP); 147 addVerticalButton(panel, col, 2, CellConstraints.TOP); 148 addVerticalButton(panel, col, 3, CellConstraints.TOP); 149 addVerticalButton(panel, col, 4, CellConstraints.TOP); 150 151 col++; 152 addVerticalButton(panel, col, 1, CellConstraints.CENTER); 153 addVerticalButton(panel, col, 2, CellConstraints.CENTER); 154 addVerticalButton(panel, col, 3, CellConstraints.CENTER); 155 addVerticalButton(panel, col, 4, CellConstraints.CENTER); 156 157 col++; 158 addVerticalButton(panel, col, 1, CellConstraints.BOTTOM); 159 addVerticalButton(panel, col, 2, CellConstraints.BOTTOM); 160 addVerticalButton(panel, col, 3, CellConstraints.BOTTOM); 161 addVerticalButton(panel, col, 4, CellConstraints.BOTTOM); 162 163 return panel; 164 } 165 166 167 private void addHorizontalButton(JPanel panel, int col, int row, 168 CellConstraints.Alignment hAlignment) { 169 JButton button = new JButton(hAlignment.toString()); 170 panel.add(button, new CellConstraints(col, row, 171 hAlignment, 172 CellConstraints.DEFAULT)); 173 } 174 175 176 private void addVerticalButton(JPanel panel, int col, int row, 177 CellConstraints.Alignment vAlignment) { 178 JButton button = new JButton(vAlignment.toString()); 179 panel.add(button, new CellConstraints(col, row, 180 CellConstraints.DEFAULT, 181 vAlignment)); 182 } 183 184 185 } 186 187 | Popular Tags |