Page 770 - Introduction to Programming with Java: A Problem Solving Approach
P. 770

                736 Chapter 17 GUI Programming—Component Layout, Additional GUI Components
allows the user to adjust the font size of the window’s text. The scroll bar is part of what is known as a scroll pane. The scroll bar allows the user to scroll up and down and view the contents of the entire window.
If you’d like to see Figure 17.19’s program in its entirety, view the ReaderMenu.java file on the book’s Web site. Figure 17.20 shows a portion of that program—the portion that creates the menu bar, menus, and menu items. And the following statement shows the portion of the program that creates the scroll pane. More specifically, the following statement creates a scroll pane for a text area component and then adds the scroll pane to the window.
add(new JScrollPane(textArea));
 private JMenuBar mBar; // the menu bar
private JMenu menu1, menu2;
// the two menus
private JMenuItem mi1, mi2, mi3, mi4; // the four menu items
􏰀
menu1 = new JMenu("Brightness");
menu2 = new JMenu("Size");
mi1 = new JMenuItem("Lighter background");
mi2 = new JMenuItem("Darker background");
mi3 = new JMenuItem("Larger font");
mi4 = new JMenuItem("Smaller font");
mi1.addActionListeneAr(pneaw gBroightPneDsFsLisEtenehr(a))n; cer mi2.addActionListener(new BrightnessListener()); mi3.addActionListener(new SizeListener()); mi4.addActionListener(new SizeListener());
menu1.add(mi1);
menu1.add(mi2);
menu2.add(mi3);
menu2.add(mi4);
mBar = new JMenuBar();
mBar.add(menu1);
mBar.add(menu2);
setJMenuBar(mBar);
Figure 17.20 Code that creates menu bar, menus, and menu items for Figure 17.19’s program
Sliders
For another learn-on-your-own example, we recommend that you look up the JSlider class on Sun’s Web site. The JSlider class allows you to add a slider component to a window. A slider allows the user to select a value from a range of values. To select a value, the user drags a “thumb” along a bar of values. See Figure 17.21. It mimics a lunar eclipse by covering a white circle (the moon) with a gray circle (the earth’s shadow). When the user drag’s the slider’s thumb right, the shadow moves right. When the user drag’s the slider’s thumb left, the shadow moves left. The slider uses an event handler to adjust the shadow’s position.








































































   768   769   770   771   772