/Users/lyon/j4p/src/bookExamples/ch18Swing/awt/LayoutExample.java

1    package bookExamples.ch18Swing.awt; 
2     
3    import gui.layouts.BoundableComponentPlacement; 
4    import gui.layouts.PreferredSizeGridLayout; 
5    import gui.run.RunButton; 
6     
7    import javax.swing.JButton; 
8    import javax.swing.JFrame; 
9    import javax.swing.JPanel; 
10   import javax.swing.JTextField; 
11   import javax.swing.JCheckBox; 
12   import java.awt.BorderLayout; 
13   import java.awt.Container; 
14   import java.awt.FlowLayout; 
15   import java.awt.GridLayout; 
16    
17   public class LayoutExample { 
18       public static void flowLayoutExample() { 
19           JFrame jf = new JFrame(); 
20           Container c = jf.getContentPane(); 
21           c.setLayout(new FlowLayout()); 
22           for (int i = 0; i < 100; i++) { 
23               c.add(new JButton("OK")); 
24               c.add(new JButton("cancel")); 
25               c.add(new JCheckBox("i="+i)); 
26           } 
27           jf.setSize(200, 200); 
28           jf.setVisible(true); 
29       } 
30    
31       public static JButton getOkButton() { 
32           JButton jb = new JButton("ok"); 
33           jb.setMinimumSize(jb.getPreferredSize()); 
34           jb.setMaximumSize(jb.getPreferredSize()); 
35           //jb.setAlignmentX(Component.LEFT_ALIGNMENT); 
36           return jb; 
37       } 
38    
39       public static JButton getCancelButton() { 
40           JButton jb = new JButton("cancel"); 
41           jb.setMinimumSize(jb.getPreferredSize()); 
42           jb.setMaximumSize(jb.getPreferredSize()); 
43           ///jb.setAlignmentX(Component.RIGHT_ALIGNMENT); 
44           return jb; 
45       } 
46    
47       public static void PreferredSizeGridLayoutExample() { 
48           JFrame jf = new JFrame(); 
49           Container c = jf.getContentPane(); 
50           PreferredSizeGridLayout psgl = 
51                   new PreferredSizeGridLayout(0, 2, 0, 0); 
52           psgl.setBoundableInterface(new BoundableComponentPlacement()); 
53           c.setLayout(psgl); 
54           for (int i = 0; i < 5; i++) { 
55               //rb.setAlignment((int)(Math.random()*8+1)); 
56               c.add(new RunButton("ok" + i) { 
57                   public void run() { 
58                   } 
59               }); 
60               c.add(new RunButton("Cancel" + i) { 
61                   public void run() { 
62                   } 
63               }); 
64           } 
65           jf.setSize(200, 200); 
66           jf.setVisible(true); 
67       } 
68    
69       public static void main(String args[]) { 
70           PreferredSizeGridLayoutExample(); 
71           GridLayoutExample(); 
72           flowLayoutExample(); 
73           BorderLayoutExample(); 
74       } 
75    
76       public static void GridLayoutExample() { 
77           JFrame jf = new JFrame(); 
78           Container c = jf.getContentPane(); 
79           c.setLayout(new GridLayout(3, 0)); 
80           c.add(new JButton("OK")); 
81           c.add(new JButton("cancel")); 
82           c.add(new JButton("OK")); 
83           c.add(new JButton("cancel")); 
84           c.add(new JButton("OK")); 
85           c.add(new JButton("cancel")); 
86           c.add(new JButton("OK")); 
87           c.add(new JButton("cancel")); 
88           c.add(new JButton("OK")); 
89           c.add(new JButton("cancel")); 
90           jf.setSize(200, 200); 
91           jf.setVisible(true); 
92       } 
93    
94       public static void BorderLayoutExample() { 
95           JFrame jf = new JFrame(); 
96           Container c = jf.getContentPane(); 
97           c.setLayout(new BorderLayout()); 
98           c.add(new JButton("N"), BorderLayout.NORTH); 
99           c.add(new JButton("S"), BorderLayout.SOUTH); 
100          c.add(new JButton("E"), BorderLayout.EAST); 
101          c.add(new JButton("C"), BorderLayout.CENTER); 
102          c.add(new JButton("W"), BorderLayout.WEST); 
103          jf.setSize(200, 200); 
104          jf.setVisible(true); 
105      } 
106   
107      public static JPanel getDialPanel() { 
108          JPanel jp = new JPanel(); 
109          jp.setLayout(new GridLayout(0, 1)); 
110          jp.add(new JButton("dial")); 
111          jp.add(new JButton("dial")); 
112          jp.add(new JButton("dial")); 
113          return jp; 
114      } 
115   
116      private static JPanel getPhoneNumberPanel() { 
117          JPanel textPanel = new JPanel(); 
118          textPanel.setLayout(new GridLayout(0, 1)); 
119          textPanel.add(new JTextField(20)); 
120          textPanel.add(new JTextField(20)); 
121          textPanel.add(new JTextField(20)); 
122          return textPanel; 
123      } 
124   
125      public static void addressBook() { 
126          JFrame jf = new JFrame(); 
127          Container c = jf.getContentPane(); 
128          c.add(getDialPanel(), BorderLayout.EAST); 
129          c.add(getPhoneNumberPanel(), BorderLayout.CENTER); 
130          c.add(getDialPanel(), BorderLayout.WEST); 
131          jf.setSize(200, 200); 
132          jf.setVisible(true); 
133      } 
134  } 
135   
136