Java Reference
In-Depth Information
b) The first argument to the constructor should be SwingConstants.HORIZONTAL or Swing-
Constants.VERTICAL , and the keyword new must be used after the = operator. Also, the
minimum value should be less than the maximum and the initial value should be in range.
c) The constant should be either BOTH , HORIZONTAL , VERTICAL or NONE .
d) paintcomponent should be paintComponent , and the method should call super.paint-
Component(g) as its first statement.
e)
The JFrame 's setSize method must also be called to establish the size of the window.
Exercises
22.4
(Fill-in-the-Blanks) Fill in the blanks in each of the following statements:
a)
A JMenuItem that is a JMenu is called a(n)
.
b) Method attaches a JMenuBar to a JFrame .
c) Container class has a default BoxLayout .
d) A(n) manages a set of child windows declared with class JInternalFrame .
22.5 (True or False) State whether each of the following is true or false . If false , explain why.
a) Menus require a JMenuBar object so they can be attached to a JFrame .
b) BoxLayout is the default layout manager for a JFrame .
c) JApplet s can contain menus.
22.6 (Find the Code Errors) Find the error(s) in each of the following. Explain how to correct
the error(s).
a) x.add( new JMenuItem( "Submenu Color" )); // create submenu
b) container.setLayout( new GridbagLayout());
22.7 (Display a Circle and Its Attributes) Write a program that displays a circle of random size
and calculates and displays the area, radius, diameter and circumference. Use the following equa-
tions: diameter = 2 × radius , area =
π
π
× radius 2 , circumference = 2 ×
× radius . Use the constant
π
Math.PI for pi (
). All drawing should be done on a subclass of JPanel , and the results of the calcu-
lations should be displayed in a read-only JTextArea .
22.8 (Using a JSlider ) Enhance the program in Exercise 22.7 by allowing the user to alter the
radius with a JSlider . The program should work for all radii in the range from 100 to 200. As the
radius changes, the diameter, area and circumference should be updated and displayed. The initial
radius should be 150. Use the equations from Exercise 22.7. All drawing should be done on a sub-
class of JPanel , and the results of the calculations should be displayed in a read-only JTextArea .
22.9 (Varying weightx and weighty ) Explore the effects of varying the weightx and weighty
values of the program in Fig. 22.21. What happens when a slot has a nonzero weight but is not al-
lowed to fill the whole area (i.e., the fill value is not BOTH )?
22.10 (Synchronizing a JSlider and a JTextField ) Write a program that uses the paintCompo-
nent method to draw the current value of a JSlider on a subclass of JPanel . In addition, provide a
JTextField where a specific value can be entered. The JTextField should display the current value
of the JSlider at all times. Changing the value in the JTextField should also update the JSlider .
A JLabel should be used to identify the JTextField . The JSlider methods setValue and getValue
should be used. [ Note: The setValue method is a public method that does not return a value and
takes one integer argument, the JSlider value, which determines the position of the thumb.]
22.11 (Creating a Color Chooser) Declare a subclass of JPanel called MyColorChooser that pro-
vides three JSlider objects and three JTextField objects. Each JSlider represents the values from
0 to 255 for the red, green and blue parts of a color. Use these values as the arguments to the Color
constructor to create a new Color object. Display the current value of each JSlider in the corre-
 
Search WWH ::




Custom Search