26.07.2013 Views

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

Java How to Program Fourth Edition - DCC

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 12 Graphical User Interface Components: Part 1 715<br />

user interface localization window<br />

validate method WindowAdapter class<br />

valueChanged method windowClosing method<br />

vertical gap space windowing system<br />

widget (window gadget) WindowListener interface<br />

SELF-REVIEW EXERCISES<br />

12.1 Fill in the blanks in each of the following statements:<br />

a) Method<br />

<strong>to</strong> handle the event.<br />

is called when the mouse is moved and an event listener is registered<br />

b) Text that cannot be modified by the user is called text.<br />

c) A arranges GUI components on a Container.<br />

d) The add method for attaching GUI components is a class method.<br />

e) GUI is an acronym for .<br />

f) Method is used <strong>to</strong> set the layout manager for a container.<br />

g) A mouseDragged method call is preceded by a method call and followed<br />

by a method call.<br />

12.2 State whether each of the following is true or false. If false, explain why.<br />

a) BorderLayout is the default layout manager for a content pane.<br />

b) When the mouse cursor is moved in<strong>to</strong> the bounds of a GUI component, method mouse-<br />

Over is called.<br />

c) A JPanel cannot be added <strong>to</strong> another JPanel.<br />

d) In a BorderLayout, two but<strong>to</strong>ns added <strong>to</strong> the NORTH region will be placed side by side.<br />

e) When one is using BorderLayout, a maximum of five components may be used.<br />

12.3 Find the error(s) in each of the following and explain how <strong>to</strong> correct it (them).<br />

a) but<strong>to</strong>nName = JBut<strong>to</strong>n( "Caption" );<br />

b) JLabel aLabel, JLabel; // create references<br />

c) txtField = new JTextField( 50, "Default Text" );<br />

d) Container container = getContentPane();<br />

setLayout( new BorderLayout() );<br />

but<strong>to</strong>n1 = new JBut<strong>to</strong>n( "North Star" );<br />

but<strong>to</strong>n2 = new JBut<strong>to</strong>n( "South Pole" );<br />

container.add( but<strong>to</strong>n1 );<br />

container.add( but<strong>to</strong>n2 );<br />

ANSWERS TO SELF-REVIEW EXERCISES<br />

12.1 a) mouseMoved. b) uneditable (read-only). c) layout manager. d) Container. e) graphical<br />

user interface. f) setLayout. g) mousePressed, mouseReleased.<br />

12.2 a) True.<br />

b) False. Method mouseEntered is called.<br />

c) False. A JPanel can be added <strong>to</strong> another JPanel because JPanel derives indirectly<br />

from Component. Therefore, a JPanel is a Component. Any Component can be<br />

added <strong>to</strong> a Container.<br />

d) False. Only the last but<strong>to</strong>n added will be displayed. Remember that only one component<br />

can be added <strong>to</strong> each region in a BorderLayout.<br />

e) True.<br />

12.3 a) new is needed <strong>to</strong> instantiate the object.<br />

b) JLabel is a class name and cannot be used as a variable name.<br />

© Copyright 1992–2002 by Deitel & Associates, Inc. All Rights Reserved. 7/7/01

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!