1 30 31 package com.jgoodies.forms.tutorial.building; 32 33 import java.awt.Component ; 34 35 import javax.swing.*; 36 37 import com.jgoodies.forms.builder.ButtonStackBuilder; 38 import com.jgoodies.forms.factories.Borders; 39 import com.jgoodies.forms.layout.CellConstraints; 40 import com.jgoodies.forms.layout.FormLayout; 41 42 49 public final class ButtonStacksExample { 50 51 public static void main(String [] args) { 52 try { 53 UIManager.setLookAndFeel("com.jgoodies.plaf.plastic.PlasticXPLookAndFeel"); 54 } catch (Exception e) { 55 } 57 JFrame frame = new JFrame(); 58 frame.setTitle("Forms Tutorial :: Button Stacks"); 59 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); 60 JComponent panel = new ButtonStacksExample().buildPanel(); 61 frame.getContentPane().add(panel); 62 frame.pack(); 63 frame.show(); 64 } 65 66 67 public JComponent buildPanel() { 68 JTabbedPane tabbedPane = new JTabbedPane(); 69 tabbedPane.putClientProperty("jgoodies.noContentBorder", Boolean.TRUE); 70 71 tabbedPane.add(buildButtonStackNoBuilder(), "No Builder"); 72 tabbedPane.add(buildButtonStackWithBuilder(), "With Builder"); 73 tabbedPane.add(buildButtonStackRelated(), "Related"); 74 tabbedPane.add(buildButtonStackUnrelated(), "Unrelated "); 75 tabbedPane.add(buildButtonStackMixedDefault(), "Mix Default"); 76 tabbedPane.add(buildButtonStackMixedNarrow(), "Mix Narrow"); 77 return tabbedPane; 78 } 79 80 private Component buildButtonStackNoBuilder() { 81 JPanel buttonStack = new JPanel( 82 new FormLayout("p", "p, 4px, p")); 83 buttonStack.add(new JButton("Yes"), "1, 1"); 84 buttonStack.add(new JButton("No"), "1, 3"); 85 86 return wrap(buttonStack, 87 "\nThis stack has been built without a ButtonStackBuilder.\n" + 88 " o The buttons have no minimum width and\n" + 89 " o The gaps use pixel sizes and do not scale with the font\n" + 90 " o The gaps may become inconsisten in a team."); 91 } 92 93 private Component buildButtonStackWithBuilder() { 94 ButtonStackBuilder builder = new ButtonStackBuilder(); 95 builder.addGridded(new JButton("Yes")); 96 builder.addRelatedGap(); 97 builder.addGridded(new JButton("No")); 98 return wrap(builder.getPanel(), 99 "\nThis stack has been built with a ButtonStackBuilder.\n" + 100 " o The buttons have a minimum width and\n" + 101 " o The button gap is a logical size that follows a style guide."); 102 } 103 104 private Component buildButtonStackRelated() { 105 ButtonStackBuilder builder = new ButtonStackBuilder(); 106 builder.addGridded(new JButton("Related")); 107 builder.addRelatedGap(); 108 builder.addGridded(new JButton("Related")); 109 builder.addRelatedGap(); 110 builder.addGridded(new JButton("Related")); 111 112 return wrap(builder.getPanel(), 113 "\nThis stack uses the logical gap for related buttons.\n"); 114 } 115 116 private Component buildButtonStackUnrelated() { 117 ButtonStackBuilder builder = new ButtonStackBuilder(); 118 builder.addGridded(new JButton("Unrelated")); 119 builder.addUnrelatedGap(); 120 builder.addGridded(new JButton("Unrelated")); 121 builder.addUnrelatedGap(); 122 builder.addGridded(new JButton("Unrelated")); 123 124 return wrap(builder.getPanel(), 125 "\nThis stack uses the logical gap for unrelated buttons.\n"); 126 } 127 128 private Component buildButtonStackMixedDefault() { 129 ButtonStackBuilder builder = new ButtonStackBuilder(); 130 builder.addGridded(new JButton("OK")); 131 builder.addRelatedGap(); 132 builder.addGridded(new JButton("Cancel")); 133 builder.addUnrelatedGap(); 134 builder.addGridded(new JButton("Help")); 135 builder.addUnrelatedGap(); 136 builder.addGlue(); 137 builder.addFixed(new JButton("Copy to Clipboard")); 138 139 return wrap(builder.getPanel(), 140 "\nDemonstrates a glue (between Help and Copy),\n" + 141 "has related and unrelated buttons and\n" + 142 "a button with long label with the default margin."); 143 } 144 145 private Component buildButtonStackMixedNarrow() { 146 ButtonStackBuilder builder = new ButtonStackBuilder(); 147 builder.addGridded(new JButton("OK")); 148 builder.addRelatedGap(); 149 builder.addGridded(new JButton("Cancel")); 150 builder.addUnrelatedGap(); 151 builder.addGridded(new JButton("Help")); 152 builder.addUnrelatedGap(); 153 builder.addGlue(); 154 builder.addGriddedNarrow(new JButton("Copy to Clipboard")); 155 156 return wrap(builder.getPanel(), 157 "\nDemonstrates a glue (between Help and Copy),\n" + 158 "has related and unrelated buttons and\n" + 159 "a button with long label with a narrow margin."); 160 } 161 162 163 165 private Component wrap(Component buttonStack, String text) { 166 Component textPane = new JScrollPane(new JTextArea(text)); 167 168 FormLayout layout = new FormLayout( 169 "fill:p:grow, 6dlu, p", 170 "fill:p:grow"); 171 JPanel panel = new JPanel(layout); 172 CellConstraints cc = new CellConstraints(); 173 panel.setBorder(Borders.DIALOG_BORDER); 174 panel.add(textPane, cc.xy(1, 1)); 175 panel.add(buttonStack, cc.xy(3, 1)); 176 return panel; 177 } 178 179 180 } 181 182 | Popular Tags |