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

1    package bookExamples.ch18Swing.awt; 
2     
3    import javax.swing.*; 
4    import java.awt.*; 
5     
6    public class PackExample extends JFrame { 
7        PackExample() { 
8            Container c = getContentPane(); 
9            c.setLayout(new GridLayout(2, 0)); 
10           c.add(new DebugButton()); 
11           c.add(new DebugButton()); 
12           c.add(new DebugButton()); 
13           c.add(new DebugButton()); 
14           c.add(new DebugButton()); 
15           setSize(200, 200); 
16           setVisible(true); 
17    
18       } 
19    
20       public static void main(String args[]) { 
21           PackExample le = new PackExample(); 
22           System.out.println("pack!"); 
23           le.pack(); 
24       } 
25    
26       class DebugButton extends JButton { 
27           DebugButton() { 
28               super("debugButton"); 
29               setPreferredSize(null); 
30           } 
31    
32           public Dimension getPreferredSize() { 
33               System.out.println("getPreferredSize:" 
34                       + super.getPreferredSize()); 
35               return super.getPreferredSize(); 
36           } 
37    
38           public Dimension getMinimumSize() { 
39               System.out.println("getMinimumSize:" 
40                       + super.getMinimumSize()); 
41               return super.getMinimumSize(); 
42           } 
43    
44           public void setPreferredSize(Dimension d) { 
45               System.out.println("setPrefferedSize:" 
46                       + d); 
47               super.setPreferredSize(d); 
48           } 
49    
50           public void setMinimumSize(Dimension d) { 
51               System.out.println("setMinimumSize:" 
52                       + d); 
53               super.setMinimumSize(d); 
54           } 
55       } 
56   } 
57