22.4 |
Fill in the blanks in each of the following statements:
- A JMenuItem that is a JMenu is called a(n) _____
- Method _____ attaches a JMenuBar to a JFrame.
- Container _____ class has a default BoxLayout.
- A(n) _____ manages a set of child windows declared with class JInternalFrame.
|
22.5 |
State whether each of the following is true or false . If false, explain why.
- Menus require a JMenuBar object so they can be attached to a JFrame.
- BoxLayout is the default layout manager for a JFrame.
- Method setEditable is a JTextComponent method.
- Class JFrame directly extends class Container.
- JApplets can contain menus.
|
22.6 |
Find the error(s) in each of the following. Explain how to correct the error(s).
-
x.add( new JMenuItem( "Submenu Color" ) );// create submenu
-
container.setLayout( m = new GridbagLayout() );
|
22.7 |
Write a program that displays a circle of random size and calculates and displays the area, radius, diameter and circumference. Use the following equations: diameter = 2 area = p circumference = 2 p). All drawing should be done on a subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
|
22.8 |
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 subclass of JPanel, and the results of the calculations should be displayed in a read-only JTextArea.
|
22.9 |
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 allowed to fill the whole area (i.e., the fill value is not BOTH)?
|
22.10 |
Write a program that uses the paintComponent 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. 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 |
Modify the program in Fig. 22.13 by adding a minimum of two new tabs.
|
22.12 |
Declare a subclass of JPanel called MyColorChooser that provides 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 corresponding JTextField. When the user changes the value of the JSlider, the JTextField should be changed accordingly. Use your new GUI component as part of an application that displays the current Color value by drawing a filled rectangle.
|
22.13 |
Modify the MyColorChooser class of Exercise 22.12 to allow the user to enter an integer value into a JTextField to set the red, green or blue value. When the user presses Enter in the JTextField, the corresponding JSlider should be set to the appropriate value.
|
22.14 |
Modify the application in Exercise 22.13 to draw the current color as a rectangle on an instance of a subclass of JPanel which provides its own paintComponent method to draw the rectangle and provides set methods to set the red, green and blue values for the current color. When any set method is invoked, the drawing panel should automatically repaint itself.
|
22.15 |
Modify the application in Exercise 22.14 to allow the user to drag the mouse across the drawing panel (a subclass of JPanel) to draw a shape in the current color. Enable the user to choose what shape to draw.
|
22.16 |
Modify the application in Exercise 22.15 to provide the user with the ability to terminate the application by clicking the close box on the window that is displayed and by selecting Exit from a File menu. Use the techniques shown in Fig. 22.5.
|
22.17 |
(Complete Drawing Application) Using the techniques developed in this chapter and Chapter 11, create a complete drawing application. The program should use the GUI components from Chapter 11 and Chapter 22 to enable the user to select the shape, color and fill characteristics. Each shape should be stored in an array of MyShape objects, where MyShape is the superclass in your hierarchy of shape classes. Use a JDesktopPane and JInternalFrames to allow the user to create multiple separate drawings in separate child windows. Create the user interface as a separate child window containing all the GUI components that allow the user to determine the characteristics of the shape to be drawn. The user can then click in any JInternalFrame to draw the shape.
|