001package bradleyross.demonstrations; 002// import java.io.File; 003// import java.io.IOException; 004import java.awt.BorderLayout; 005import java.awt.FlowLayout; 006import java.awt.GridLayout; 007import java.awt.GridBagLayout; 008import java.awt.GridBagConstraints; 009// import java.awt.Insets; 010// import java.awt.LayoutManager; 011import java.awt.event.ActionListener; 012import java.awt.event.ActionEvent; 013import javax.swing.SwingUtilities; 014import javax.swing.JFrame; 015// import javax.swing.JPanel; 016import javax.swing.JTextArea; 017import javax.swing.JButton; 018/** 019 * Provides a demonstration of several features 020 * of the Swing methods. 021 * @author Bradley Ross 022 * 023 */ 024public class SwingDemo { 025 /** 026 * This provides a demonstration of the 027 * {@link FlowLayout} class.} 028 * 029 * @author Bradley Ross 030 */ 031 public class FlowDemo implements Runnable { 032 int option = 0; 033 JFrame frame = null; 034 FlowLayout layout = null; 035 protected int value = 0; 036 protected String title = "Border Demo"; 037 protected void setText(JTextArea area) { 038 setText(area, 15); 039 } 040 protected String getTitle() { 041 return title; 042 } 043 protected void setText(JTextArea area, int width) { 044 area.setLineWrap(true); 045 area.setWrapStyleWord(true); 046 area.setRows(5); 047 area.setColumns(width); 048 } 049 public FlowDemo() { 050 value = 0; 051 } 052 public FlowDemo(int i) { 053 value = i; 054 } 055 056 public void run() { 057 System.out.println("Starting run in FlowDemo"); 058 frame = new JFrame(); 059 title = "Demonstration of FlowLayout"; 060 061 frame.setTitle(title); 062 if (value == 0) { 063 layout = new FlowLayout(FlowLayout.LEFT); 064 } else if (value == 1) { 065 layout = new FlowLayout(FlowLayout.CENTER); 066 } else if (value == 2) { 067 layout = new FlowLayout(FlowLayout.RIGHT); 068 } else if (value == 3) { 069 layout = new FlowLayout(FlowLayout.LEADING); 070 } else if (value == 4) { 071 layout = new FlowLayout(FlowLayout.TRAILING); 072 } 073 frame.setLayout(layout); 074 for (int counter = 0; counter < 21; counter++) { 075 String text = "Button" + counter; 076 JButton button = new JButton(text); 077 frame.add(button); 078 } 079 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 080 frame.setSize(500,300); 081 frame.setVisible(true); 082 System.out.println("run method in FlowDemo is complete"); 083 } 084 } 085 /** 086 * This provides a demonstration of the 087 * {@link BorderLayout} class.} 088 * 089 * @author Bradley Ross 090 */ 091 public class BorderDemo implements Runnable { 092 JFrame frame = null; 093 protected int value = 0; 094 protected String title = "Border Demo"; 095 protected void setText(JTextArea area) { 096 setText(area, 15); 097 } 098 protected String getTitle() { 099 return title; 100 } 101 protected void setText(JTextArea area, int width) { 102 area.setLineWrap(true); 103 area.setWrapStyleWord(true); 104 area.setRows(5); 105 area.setColumns(width); 106 } 107 public BorderDemo() { 108 value = 0; 109 } 110 public BorderDemo(int i) { 111 value = i; 112 } 113 public void run() { 114 System.out.println("Starting run in BorderDemo"); 115 frame = new JFrame(); 116 if (value == 0) { 117 title = "Demonstration of BorderLayout"; 118 } else { 119 title = "Demonstration of BorderLayout with default"; 120 } 121 frame.setTitle(title); 122 BorderLayout layout = new BorderLayout(); 123 frame.setLayout(layout); 124 JTextArea areaNorth = new JTextArea("This is the NORTH/PAGE_START text that will go in the top component" + 125 " of the layout"); 126 setText(areaNorth, 40); 127 JTextArea areaSouth = new JTextArea("This is the SOUTH/PAGE_END text that will go in the bottom component" + 128 " of the layout. Space will be removed from the center before it is " + 129 "removed from the other components"); 130 setText(areaSouth, 40); 131 JTextArea areaEast = new JTextArea("This is the EAST/LINE_END text"); 132 setText(areaEast,10); 133 JTextArea areaWest = new JTextArea("This is the WEST/LINE_START text"); 134 setText(areaWest, 10); 135 JTextArea areaCenter = new JTextArea("This is the center text"); 136 setText(areaCenter, 30); 137 frame.add(areaCenter); 138 if (value==0) { 139 frame.add(areaNorth,BorderLayout.NORTH); 140 frame.add(areaSouth, BorderLayout.SOUTH); 141 frame.add(areaWest, BorderLayout.WEST); 142 frame.add(areaEast, BorderLayout.EAST); 143 frame.add(areaCenter, BorderLayout.CENTER); 144 } else { 145 frame.add(areaNorth, BorderLayout.PAGE_START); 146 frame.add(areaSouth, BorderLayout.PAGE_END); 147 frame.add(areaWest, BorderLayout.LINE_START); 148 frame.add(areaEast, BorderLayout.LINE_END); 149 frame.add(areaCenter, BorderLayout.CENTER); 150 } 151 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 152 frame.setSize(500,300); 153 frame.setVisible(true); 154 System.out.println("run method in BorderDemo is complete"); 155 } 156 } 157 /** 158 * This provides a demonstration of the 159 * {@link GridBagLayout} class. 160 * @author Bradley Ross 161 * 162 */ 163 public class GridBagDemo implements Runnable { 164 JFrame frame = null; 165 GridBagLayout layout = null; 166 protected String title = null; 167 public String getTitle() { 168 return title; 169 } 170 protected void addButton(String text, GridBagConstraints c) { 171 JButton button = new JButton(text); 172 layout.setConstraints(button, c); 173 frame.add(button); 174 } 175 public void run() { 176 System.out.println("Starting run in GridBagDemo"); 177 frame = new JFrame(); 178 title = "Demonstration of GridBagLayout"; 179 frame.setTitle(title); 180 layout = new GridBagLayout(); 181 frame.setLayout(layout); 182 GridBagConstraints c = new GridBagConstraints(); 183 GridBagConstraints cEnd = new GridBagConstraints(); 184 cEnd.gridwidth = GridBagConstraints.REMAINDER; 185 GridBagConstraints cDoubleWide = new GridBagConstraints(); 186 cDoubleWide.gridwidth = 2; 187 GridBagConstraints cDoubleHigh = new GridBagConstraints(); 188 GridBagConstraints cDoubleHighEnd = new GridBagConstraints(); 189 cDoubleHighEnd.gridwidth = GridBagConstraints.REMAINDER; 190 cDoubleHighEnd.gridheight = 2; 191 cDoubleHigh.gridheight = 2; 192 c.fill = GridBagConstraints.BOTH; 193 cEnd.fill = GridBagConstraints.BOTH; 194 cDoubleHigh.fill = GridBagConstraints.BOTH; 195 cDoubleHighEnd.fill = GridBagConstraints.BOTH; 196 cDoubleWide.fill = GridBagConstraints.BOTH; 197 frame.setLayout(layout); 198 addButton("first", c); 199 addButton("second", c); 200 addButton("third", cEnd); 201 addButton("first tall", cDoubleHigh); 202 addButton("middle", cDoubleHigh); 203 addButton("next", cEnd); 204 addButton("third row", c); 205 frame.setSize(500, 300); 206 frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); 207 frame.setVisible(true); 208 System.out.println("run in GridBagDemo is complete"); 209 } 210 } 211 /** 212 * Creates the main Swing page using the 213 * {@link GridLayout} class. 214 * @author Bradley Ross 215 * 216 */ 217 public class MainPage implements Runnable { 218 JFrame frame = null; 219 GridLayout layout = null; 220 public class Listener implements ActionListener { 221 public void actionPerformed(ActionEvent e) { 222 String name = e.getActionCommand(); 223 System.out.println(e.getActionCommand()); 224 if (name.equalsIgnoreCase("borderlayout")) { 225 SwingUtilities.invokeLater(new BorderDemo(0)); 226 } else if (name.equalsIgnoreCase("borderlayout2")) { 227 SwingUtilities.invokeLater(new BorderDemo(1)); 228 } else if (name.equalsIgnoreCase("gridbaglayout")) { 229 SwingUtilities.invokeLater(new GridBagDemo()); 230 } else if (name.equalsIgnoreCase("flowlayout0")) { 231 SwingUtilities.invokeLater(new FlowDemo(0)); 232 } else if (name.equalsIgnoreCase("flowlayout1")) { 233 SwingUtilities.invokeLater(new FlowDemo(1)); 234 } else if (name.equalsIgnoreCase("flowlayout2")) { 235 SwingUtilities.invokeLater(new FlowDemo(2)); 236 } else if (name.equalsIgnoreCase("flowlayout3")) { 237 SwingUtilities.invokeLater(new FlowDemo(3)); 238 }else if (name.equalsIgnoreCase("flowlayout4")) { 239 SwingUtilities.invokeLater(new FlowDemo(4)); 240 } 241 } 242 } 243 /** 244 * Sets up a group of buttons using the 245 * {@link GridLayout} class. 246 */ 247 public void run() { 248 frame = new JFrame(); 249 System.out.println("Starting run "); 250 frame.setTitle("Main Panel"); 251 Listener listener = new Listener(); 252 layout = new GridLayout(); 253 System.out.println("GridLayout and Listener objects created"); 254 layout.setColumns(5); 255 layout.setRows(0); 256 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 257 frame.setSize(700,400); 258 frame.setLayout(layout); 259 JButton b1 = new JButton("BorderLayout"); 260 b1.setActionCommand("BorderLayout"); 261 b1.addActionListener(listener); 262 frame.add(b1); 263 JButton b1a = new JButton("BorderLayout2"); 264 b1a.setActionCommand("BorderLayout2"); 265 b1a.addActionListener(listener); 266 frame.add(b1a); 267 JButton b2 = new JButton("GridBagLayout"); 268 b2.setActionCommand("GridBagLayout"); 269 b2.addActionListener(listener); 270 frame.add(b2); 271 JButton b3a = new JButton("FlowLayout Left"); 272 b3a.setActionCommand("flowlayout0"); 273 b3a.addActionListener(listener); 274 frame.add(b3a); 275 JButton b3b = new JButton("FlowLayout Center"); 276 b3b.setActionCommand("flowlayout1"); 277 b3b.addActionListener(listener); 278 frame.add(b3b); 279 JButton b3c = new JButton("FlowLayout Right"); 280 b3c.setActionCommand("flowlayout2"); 281 b3c.addActionListener(listener); 282 frame.add(b3c); 283 JButton b3d = new JButton("FlowLayout Leading"); 284 b3d.setActionCommand("flowlayout3"); 285 b3d.addActionListener(listener); 286 frame.add(b3d); 287 JButton b3e = new JButton("FlowLayout Trailing"); 288 b3e.setActionCommand("flowlayout4"); 289 b3e.addActionListener(listener); 290 frame.add(b3e); 291 for (int i = 0; i < 10; i++) { 292 String name = "Test" + Integer.toString(i); 293 JButton button = new JButton(name); 294 button.addActionListener(listener); 295 frame.add(button); 296 } 297 System.out.println("Buttons created on main page"); 298 frame.setVisible(true); 299 frame.repaint(); 300 System.out.println("run method finished in MainPage"); 301 } 302 } 303 public void run2() { 304 System.out.println("starting run2"); 305 try { 306 SwingUtilities.invokeLater(new MainPage()); 307 }catch (Exception e) { 308 System.out.println("Exception in main page"); 309 e.printStackTrace(); 310 } 311 System.out.println("run2 complete"); 312 } 313 public static void main(String[] args) { 314 SwingDemo instance = new SwingDemo(); 315 instance.run2(); 316 } 317}