|
|
||||||||||||||||||||||||||||||||||||||
Class UIButtonpublic abstract class UIButton extends UISingleContainer { // Fields public static final int TOGGLE; public static final int TRITOGGLE; // Constructors public UIButton(); public UIButton(IUIComponent comp); public UIButton(IUIComponent comp, int style); // Methods public void doDefaultAction(); public String getDefaultAction(); public int getStyle(); public boolean keyDown(Event e, int key); public boolean keyUp(Event e, int key); public boolean mouseClicked(Event e, int x, int y); public void setHot(boolean on); public void setStyle(int style); } This class implements an abstract button control. The following diagram shows the classes that extend UIButton. UIButton | +--UICheckButton | | | +--UIRadioButton | +--UIPushButton | +--UIExpandButton | +--UIRepeatButton When constructing a button, you typically specify the button's content and style. The UIText, UIGraphic, and UIItem classes provide several options for adding text and graphical images to your button. The following example demonstrates different ways to construct a button. // Construct a push button that displays the String // "Push Button". By default, this text is centered. // Make the button raised and toggled. UIPushButton p1 = new UIPushButton("Push Button", UIPushButton.RAISED | UIButton.TOGGLE); // Construct a check box button that displays the text // stored in a UIText object. Left-align the text and // make the button tritoggled. UICheckButton c1 = new UICheckButton(new UIText("Checkbox", UIStatic.LEFT), UIButton.TRITOGGLE); // Construct a repeating push button that displays the graphical // image stored in the variable myImage. Left-align the image. UIRepeatButton r1 = new UIRepeatButton(new UIGraphic(myImage, UIStatic.LEFT)); // Construct a radio button that displays both the graphical // image stored in the variable myImage and the text // "Radio Button." Put the image above the text. UIRadioButton r2 = new UIRadioButton(new UIItem(myImage, "Radio Button", 0, UIItem.ABOVE)); // Add each button to the container. add(p1); add(c1); add(r1); add(r2); UIButton inherits the setName method to change the text displayed on a button after it is created. The getName method allows you to retrieve the current text. The following example shows how to use these two methods. // Define two UIPushButton variables. UIPushButton p1, p2; // Construct p1 and p2. p1 displays only text, while // p2 displays a graphical image and text. p1 = new UIPushButton("p1 text"); p2 = new UIPushButton(new UIItem(myImage, "p2 text")); add(p1); add(p2); // Now replace the text displayed from p2 with the text from p1. String s = p1.getName(); p2.setName(s); // p2 now displays a graphical image and // the text from p1. Note When you construct a button with a String, the button is automatically hot-tracked. To override this default, use a UIText object instead of a String, as shown in the following example. UIPushButton p1, p2, p3; // Construct a button with a String. // By default, the button is hot-tracked. p1 = new UIPushButton("Hot-tracked text"); // Construct a button with a UIText object. // By default, the button is not hot-tracked. p2 = new UIPushButton(new UIText("Text not hot-tracked")); // To create a hot-tracked button using UIText, // specify the UIStatic.HOTTRACK flag. p3 = new UIPushButton(new UIText("Hot-tracked UIText", UIStatic.HOTTRACK)); add(p1); add(p2); add(p3); For more information about hot-tracking, see the UIText overview. Note The hot-track color is the same color as the button text color. As a result, hot-tracking does not appear to be functional. Typically, when a UIButton object is pressed and released, an action event is generated. (With UIRepeatButton objects, action events are continually generated while the button remains pressed.) The following example shows how to use the action method to trap these events. public boolean action(Event e, Object o) { if (e.target == c) // c is a UICheckButton object. { // isChecked returns true if c is checked, // false if c is unchecked. boolean enabled = c.isChecked(); // p1 and p2 are UIPushButton objects that // will be enabled if c is checked; otherwise, // they will be disabled. p1.setEnabled(enabled); p2.setEnabled(enabled); return true; } return false; } In both UIRadioButton and UICheckButton objects, the behavior of the action method is inconsistent with that of AWT components in certain cases. The inconsistency is in the way the states of the button object are retrieved. For example, if you create a menu with a UIMenuButton, add a UIRadioButton to it as a UIMenuItem, the action method returns different values under AFC and AWT. //Add a radio button as a menu item. UIMenuItem uimi = new UIMenuItem(new UIRadioButton("button as menu item")); The action method returns the following values when AFC is used.
In comparison, the action method returns the following under AWT.
You must query the button to retrieve states when using AFC, if you are currently using the action method to retrieve the states of buttons in your Java code. UIComponent | +--UIContainer | +--UIStateContainer | +--UISingleContainer | +--UIButton ConstructorsUIButtonpublic UIButton(); UIButtonpublic UIButton(IUIComponent comp); UIButtonpublic UIButton(IUIComponent comp, int style); MethodsdoDefaultActionpublic void doDefaultAction(); getDefaultActionpublic String getDefaultAction(); getStylepublic int getStyle(); keyDownpublic boolean keyDown(Event e, int key); keyUppublic boolean keyUp(Event e, int key); mouseClickedpublic boolean mouseClicked(Event e, int x, int y); setHotpublic void setHot(boolean on); setStylepublic void setStyle(int style); Fields
|
© 1998 Microsoft Corporation. All rights reserved. Terms of use. |