|
|
|||||||||||
Using Static, Button, Menu, and Choice ControlsThis article provides details on how to use the static, button, menu, choice, and list user interface (UI) controls. Static ControlsStatic controls aren't actually controls at allby definition, they don't accept or respond to user input. But they are an important user interface component and are used as elements of most other user interface controls. For example, the text in a menu control or in a list box is a UIText object, which is a static control. The following diagram shows the class hierarchy of static controls.
There are three basic types of static controls:
The following is an illustration of various AFC static controls.
There are a total of eight static controls in this illustration: four static text controls (the label on the left and the three commas), one static graphic control (the folder), and three varieties of static item controls (the print buttons). The following example is the source code for a panel that contains these controls. class StaticPanel extends UIPanel { // Constructor public StaticPanel() { Toolkit toolkit = getToolkit(); Image hotprint = toolkit.getImage("HotPrint.gif"); Image hotfavorites = toolkit.getImage("HotFavorites.gif"); add(new UIText("Examples of static controls:")); add(new UIGraphic(hotfavorites)); add(new UIText(",")); add(new UIItem(hotprint, "Print")); add(new UIText(",")); add(new UIItem(hotprint, "Print", 0, UIItem.ABOVE)); add(new UIText(",")); add(new UIItem(hotprint, "Print", 0, UIItem.ONLEFT)); } } Button ControlsAFC offers four basic types of buttons:
Radio buttons are extensions of check boxesthe main difference between them is their appearance. Repeat buttons appear and operate like push buttons. However, instead of firing a single action event when pressed, repeat buttons continue to fire action events until released. The following diagram shows the class hierarchy of button controls.
The following illustration shows the appearance of three types of AFC buttons. The buttons shown here are all text-only buttonsAFC also supports buttons displaying images and buttons displaying text and images.
Here's the code for a class that is a panel that contains the buttons shown in the previous illustration. class ButtonsPanel extends UIPanel { private UIPushButton pushbutton; private UIRadioButton radiobutton; private UICheckButton checkbutton; // Constructor public ButtonsPanel() { // Create buttons and add to ButtonPanel add(pushbutton = new UIPushButton("Push Button", UIPushButton.RAISED)); add(radiobutton = new UIRadioButton("Radio Button")); add(checkbutton = new UICheckButton("Check Box")); } } Note that the push button is created with the UIPushButton.RAISED style. If you don't specify the raised style, the push button appears only as text and won't look like a button until it's under the mouse cursor. The following table summarizes the styles that you can use when you create buttons:
To create a button with an image instead of text, supply a UIGraphic object to the button's constructor. To create a button with both text and an image, supply the constructor a UIItem object. Menu ControlsLike other AFC controls, the AFC menu controls are discrete, stand-alone controls that can be used anywhere in Java programs. There are three basic classes used to create menus:
The following diagram shows the class hierarchy of menu controls.
The following illustration shows the appearance of a menu control before and after activation.
The menu includes a check box menu item, a disabled menu item, menu separators, and a cascading menu list. Here's the code for a class that is a panel that contains the menu shown in the previous illustration. class MenusPanel extends UIPanel { // Constructor public MenusPanel() { // Create a menu list and add some menu items UIMenuList mainmenulist = new UIMenuList(); mainmenulist.add("Menu Item 1"); mainmenulist.add("Menu Item 2"); mainmenulist.add(new UILine()); mainmenulist.add(new UICheckButton("Check Box Menu Item")); mainmenulist.add(new UILine()); UIText disableditem = new UIText("Disabled Menu Item"); disableditem.setEnabled(false); mainmenulist.add(disableditem); mainmenulist.add(new UILine()); // Create a sub menu list and add to main menu UIMenuList submenulist = new UIMenuList(); submenulist.add("Sub Menu Item 1"); submenulist.add("Sub Menu Item 2"); mainmenulist.add(new UIMenuItem("Sub Menu Item", submenulist)); // Create a menu button for main menu and add to panel UIMenuButton menubutton = new UIMenuButton("Menu Button", mainmenulist); add (menubutton); } } To create a menu separator, add a UILine object to a menu list. To create cascading menus, create a secondary menu list and add it as a menu item to the primary menu list. You can also create menu items with images by using static graphic (UIGraphic) and static item (UIItem) objects as menu items. Menu Controls Event HandlingMenu controls fire events on the items they contain, not on the actual menu control object. Consider the following code fragment. UIMenuList menulist = new UIMenuList(); UIText item = new UIText("Apples"); menulist.add(item); When the user selects the "Apples" menu item, the menu control fires an action event on the UIText object specified by the item parameter. You can also add textual menu items to menu lists by specifying a string as shown in the following example. UIMenuList menulist = new UIMenuList(); UIText text = (UIText) menulist.add("Oranges"); In this case, the UIMenuList.add method creates a UIText object from the specified string. When the user selects the "Oranges" menu item, the menu control fires an action event on the UIText object specified by the text parameter. Choice and List ControlsChoice and list controls enable users to choose one or more items from a list of available choices. Choice controls (UIChoice) are similar to drop-down list boxes in Win32they display only the currently selected item until they are activated and present a list of available choices. List controls (UIList) display available choices along with the currently selected item. You can also create list controls using the UIList.MULTISELECT selection mode to enable users to select multiple items from the list. The following diagram shows the class hierarchy of choice and list controls.
The following illustration shows the appearance of two choice controls and a list control. The first choice control is shown before activation and the second one is shown after activation.
Here's the code for a class that is a panel that contains a choice control and a list control. class ChoicePanel extends UIPanel { // Constructor public ChoicePanel() { UIChoice choice = new UIChoice(); choice.addString("Choice A"); choice.addString("Choice B"); choice.addString("Choice C"); add (choice); UIList list = new UIList(); list.add("List Item A"); list.add("List Item B"); list.add("List Item C"); add(list); } } Note To create a scroll bar with a list control, you must add the list control to a scroll viewer control (UIScrollViewer). Choice and List Control Event HandlingLike menu controls, choice and list controls don't fire events directly on the choice or list control objectsthey fire events on the objects contained by the controls. Choice controls fire an action event when a user chooses one of the items available in the drop-down list. List controls don't fire events unless a selection is made with a double-click mouse action.
|
© 1998 Microsoft Corporation. All rights reserved. Terms of use. |