13.10. Centering a Display Using the FontMetrics Class

 
[Page 420 ( continued )]

Review Questions

Sections 12.3 “12.4

12.1 Which class is the root of the Java GUI component classes? Is a container class a subclass of Component ? Which class is the root of the Swing GUI component classes?
12.2 Explain the difference between AWT GUI components , such as java.awt.Button , and Swing components, such as javax.swing.JButton .
12.3 How do you create a frame? How do you set the size for a frame? How do you get the size of a frame? How do you add components to a frame? What would happen if the statements frame.setSize(400, 300) and frame.setVisible(true) were swapped in the MyFrameWithComponents class in Section 12.4.2, "Adding Components to a Frame"?
12.4 Determine whether the following statements are true or false:
  • You can add a button to a frame.

  • You can add a frame to a panel.

  • You can add a panel to a frame.

  • You can add any number of components to a panel or a frame.

  • You can derive a class from JButton , JPanel , JFrame , or JApplet .

12.5 The following program is supposed to display a button in a frame, but nothing is displayed. What is the problem?
 1   public class   Test   extends   javax.swing.JFrame { 2   public   Test() { 3 add(   new   javax.swing.JButton(   "OK"   )); 4 } 5 6   public static void   main(String[] args) { 7 javax.swing.JFrame frame =   new   javax.swing.JFrame(); 8 frame.setSize(   100   ,   200   ); 9 frame.setVisible(   true   ); 10 } 11 } 


[Page 421]
12.6 Which of the following statements have syntax errors?
 Component c1 =   new   Component(); JComponent c2 =   new   JComponent(); Component c3 =   new   JButton(); JComponent c4 =   new   JButton(); Container c5 =   new   JButton(); c5.add(c4); Object c6 =   new   JButton(); c5.add(c6); 

Section 12.5 Layout Managers

12.7 Why do you need to use layout managers? What is the default layout manager for a frame? How do you add a component to a frame?
12.8 Describe FlowLayout . How do you create a FlowLayout manager? How do you add a component to a FlowLayout container? Is there a limit to the number of components that can be added to a FlowLayout container?
12.9 Describe GridLayout . How do you create a GridLayout manager? How do you add a component to a GridLayout container? Is there a limit to the number of components that can be added to a GridLayout container?
12.10 Describe BorderLayout . How do you create a BorderLayout manager? How do you add a component to a BorderLayout container? Can you add multiple components in the same section?

Sections 12.6 “12.7

12.11 How do you create a color ? What is wrong about creating a Color using new Color(400, 200, 300) ? Which of the following two colors are darker , new Color(10, 0, 0) or new Color(200, 0, 0) ?
12.12 How do you create a font? How do you find all the available fonts on your system?

Section 12.8 Using Panels as Subcontainers

12.13 How do you create a panel with a specified layout manager?
12.14 What is the default layout manager for a JPanel ? How do you add a component to a JPanel ?
12.15 Can you use the setTitle method in a panel? What is the purpose of using a panel?
12.16 Since a GUI component class such as JButton is a subclass of Container , can you add components into a button?

Sections 12.9 “12.10

12.17 How do you set background color, foreground color, font, and tool tip text on a Swing GUI component? Why is the tool tip text not displayed in the following code?
 1   import   javax.swing.*; 2 3   public class   Test   extends   JFrame { 4   private   JButton jbtOK =   new   JButton(   "OK"   ); 5 6   public static void   main(String[] args) { 7  // Create a frame and set its properties  8 JFrame frame =   new   Test(); 9 frame.setTitle(   "Logic Error"   ); 

[Page 422]
 10 frame.setSize(   200   ,   100   ); 11 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 12 frame.setVisible(   true   ); 13 } 14 15   public   Test() { 16 jbtOK.setToolTipText(   "This is a button"   ); 17 add(    new   JButton(   "OK"   )  ); 18 } 19 } 

12.18 Show the output of the following code:
   import   javax.swing.*;   public class   Test {   public static void   main(String[] args) { JButton jbtOK =   new   JButton(   "OK"   ); System.out.println(jbtOK.isVisible()); JFrame frame =   new   JFrame(); System.out.println(frame.isVisible()); } } 

12.19 How do you create an ImageIcon from the file image/us.gif in the class directory?
12.20 What happens if you add a button to a container several times, as shown below? Does it cause syntax errors? Does it cause runtime errors?
 JButton jbt =   new   JButton(); JPanel panel =   new   JPanel(); panel.add(jbt); panel.add(jbt); panel.add(jbt); 

12.21 Will the following code display three buttons? Will the buttons display the same icon?
 1   import   javax.swing.*; 2   import   java.awt.*; 3 4   public class   Test   extends   JFrame { 5   public static void   main(String[] args) { 6  // Create a frame and set its properties  7 JFrame frame =   new   Test(); 8 frame.setTitle(   "ButtonIcons"   ); 9 frame.setSize(   200   ,   100   ); 10 frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); 11 frame.setVisible(   true   ); 12 } 13 14   public   Test() { 15 ImageIcon usIcon =   new   ImageIcon(   "image/usIcon.gif"   ); 16 JButton jbt1 =   new   JButton(usIcon); 17 JButton jbt2 =   new   JButton(usIcon); 18 19 JPanel p1 =   new   JPanel(); 20 p1.add(jbt1); 

[Page 423]
 21 22 JPanel p2 =   new   JPanel(); 23 p2.add(jbt2); 24 25 JPanel p3 =   new   JPanel(); 26 p2.add(jbt1); 27 28 add(p1, BorderLayout.NORTH); 29 add(p2, BorderLayout.SOUTH); 30 add(p3, BorderLayout.CENTER); 31 } 32 } 

12.22 Can a border or an icon be shared by GUI components?
 


Introduction to Java Programming-Comprehensive Version
Introduction to Java Programming-Comprehensive Version (6th Edition)
ISBN: B000ONFLUM
EAN: N/A
Year: 2004
Pages: 503

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net