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