15.1* | ( Revising Listing 15.2, ButtonDemo.java ) Rewrite Listing 15.2 to add a group of radio buttons to select background colors. The available colors are red, yellow, white, gray, and green (see Figure 15.33). Figure 15.33. The <= and => buttons move the message on the panel, and you can also set the background color for the message.
|
15.2* | ( Selecting geometric figures ) Write a program that draws various figures, as shown in Figure 15.34. The user selects a figure from a radio button and specifies whether it is filled in a check box. (Hint: Use the FigurePanel class introduced in §13.7 to display a figure.) Figure 15.34. The program displays lines, rectangles, and ovals when you select a shape type.
|
15.3** | ( Traffic lights ) Write a program that simulates a traffic light. The program lets the user select one of three lights: red, yellow, or green. When a radio button is selected, the light is turned on, and only one light can be on at a time (see Figure 15.35). No light is on when the program starts. Figure 15.35. The radio buttons are grouped to let you select only one color in the group to control a traffic light.
|
15.4* | ( Creating a simple calculator ) Write a program to perform add, subtract, multiply, and divide operations (see Figure 15.36). Figure 15.36. The program does addition, subtraction, multiplication, and division on double numbers . |
15.5* | ( Creating a miles/kilometers converter ) Write a program that converts miles and kilometers, as shown in Figure 15.37. If you enter a value in the Mile text field and press the Enter key, the corresponding kilometer is displayed in the Kilometer text field. Likewise, if you enter a value in the Kilometer text field and press the Enter key, the corresponding mile is displayed in the Mile text field. Figure 15.37. The program converts miles to kilometers, and vice versa. |
15.6* | ( Creating an investment value calculator ) Write a program that calculates the future value of an investment at a given interest rate for a specified number of years . The formula for the calculation is as follows : futureValue = investmentAmount * (1 + monthlyInterestRate) years*12 Use text fields for interest rate, investment amount, and years. Display the future amount in a text field when the user clicks the Calculate button, as shown in Figure 15.38. Figure 15.38. The user enters the investment amount, years, and interest rate to compute future value.
|
15.7* | ( Setting clock time ) Write a program that displays a clock time and sets the clock time with the input from three text fields, as shown in Figure 15.39. Use the StillClock in §13.12, "Case Study: The StillClock Class." Figure 15.39. The program displays the time specified in the text fields.
|
15.8** | ( Selecting a font ) Write a program that can dynamically change the font of a message to be displayed on a panel. The message can be displayed in bold and italic at the same time, or can be displayed in the center of the panel. You can select the font name or font size from combo boxes, as shown in Figure 15.40. The available font names can be obtained using getAvailableFontFamilyNames() in GraphicsEnvironment ( §12.7, "The Font Class"). The combo box for font size is initialized with numbers from 1 to 100. Figure 15.40. You can dynamically set the font for the message. |
15.9** | ( Demonstrating JLabel properties ) Write a program to let the user dynamically set the properties horizontalAlignment , verticalAlignment , horizontalTextAlignment , and verticalTextAlignment , as shown in Figure 15.41. Figure 15.41. You can set the alignment and text-position properties of a button dynamically. |
15.10* | ( Adding new features into Listing 15.2, ButtonDemo.java, incrementally ) Improve Listing 15.2 incrementally, as follows (see Figure 15.42):
Figure 15.42. The program uses buttons, labels, text fields, combo boxes, radio buttons, check boxes, and borders. |
15.11* | ( Demonstrating JTextField properties ) Write a program that sets the horizontal-alignment and column-size properties of a text field dynamically, as shown in Figure 15.43. Figure 15.43. You can set the horizontal-alignment and column-size properties of a text field dynamically.
|
15.12* | ( Demonstrating JTextArea properties ) Write a program that demonstrates the wrapping styles of the text area. The program uses a check box to indicate whether the text area is wrapped. In the case where the text area is wrapped, you need to specify whether it is wrapped by characters or by words, as shown in Figure 15.44. Figure 15.44. You can set the options to wrap a text area by characters or by words dynamically.
|
15.13* | ( Comparing loans with various interest rates ) Rewrite Exercise 4.21 to create a user interface, as shown in Figure 15.45. Your program should let the user enter the loan amount and loan period in number of years from a text field, and should display the monthly and total payments for each interest rate starting from 5 percent to 8 percent, with increments of one- eighth , in a text area. Figure 15.45. The program displays a table for monthly payments and total payments on a given loan based on various interest rates. |
15.14* | ( Using JComboBox and JList ) Write a program that demonstrates selecting items in a list. The program uses a combo box to specify a selection mode, as shown in Figure 15.46. When you select items, they are displayed in a label below the list. Figure 15.46. You can choose single selection, single-interval selection, or multiple-interval selection in a list.
|
15.15** | ( Using JScrollBar ) Write a program that uses scroll bars to select the foreground color for a label, as shown in Figure 15.47. Three horizontal scroll bars are used for selecting the red, green, and blue components of the color. Use a title border on the panel that holds the scroll bars. Figure 15.47. The foreground color changes in the label as you adjust the scroll bars. |
15.16** | ( Using JSlider ) Revise the preceding exercise using sliders. |
15.17*** | ( Displaying a calendar ) Write a program that displays the calendar for the current month, as shown in Figure 15.48. Use labels, and set texts on the labels to display the calendar. Use the GregorianCalendar class in §10.3, "The Calendar and GregorianCalendar classes," to obtain the information about month, year, first day of the month, and number of days in the month. Figure 15.48. The program displays the calendar for the current month.(This item is displayed on page 531 in the print version) |
15.18* | ( Revising Listing 15.11, MultipleWindowsDemo.java ) Instead of displaying the occurrences of the letters using the Histogram component in Listing 15.11, use a bar chart, so that the display is as shown in Figure 15.49. Figure 15.49. The number of occurrences of each letter is displayed in a bar chart. |
15.19** | ( Text Viewer ) Write a program that displays a text file in a text area, as shown in Figure 15.50. The user enters a filename in a text field and clicks the View button; the file is then displayed in a text area. Figure 15.50. The program displays a histogram that shows the occurrences of each letter in the file.
|
15.20** | ( Displaying country flag and flag description ) Listing 15.7, ComboBoxDemo.java, gives a program that lets users view a country's flag image and description by selecting the country from a combo box. The description is a string coded in the program. Rewrite the program to read the text description from a file. Suppose that the descriptions are stored in the file description0.txt , and description8.txt under the text directory for the nine countries Canada, China, Denmark, France, Germany, India, Norway, the United Kingdom, and the United States, in this order. |
15.21** | ( Creating a histogram for occurrences of letters ) In Listing 15.11, MultipleWindowsDemo.java, you developed a program that displays a histogram to show the occurrences of each letter in a text area. Reuse the Histogram class created in Listing 15.12 to write a program that will display a histogram on a panel. The histogram should show the occurrences of each letter in a text file, as shown in Figure 15.51. Assume that the letters are not case-sensitive.
Figure 15.51. The program displays a histogram that shows the occurrences of each letter in the file.
|
15.22** | ( Slideshow ) Exercise 14.13 developed a slideshow using images. Rewrite Exercise 14.13 to develop a slideshow using text files. Suppose ten text files named slide0.txt , slide1.txt , and slide9.txt are stored in the text directory downloadable along with the source code in the book. Each slide displays the text from one file. Each slide is shown for a second. The slides are displayed in order. When the last slide finishes, the first slide is redisplayed, and so on. Use a text area to display the slide. |