Java graphic interface API for mobile phone software

Source: Internet
Author: User

In addition to screen-based components, you should also master the use of forms to organize multiple components into one screen. This section discusses the Form class and components that can be placed on the Form.

   1. Form

A form object is a screen that can contain any item, including read-only and editable text boxes, images, date fields, gauge, and grouping components. As mentioned above, any subclass of the Item class can be placed on a form object. The Form class constructor is as follows:

public Form(String title);
public Form(String title, Item[] items);

The first constructor creates an empty form with only one title. The second constructor creates a form containing the title and initial content. For example, the following program creates an empty form with the title "Choose an Item", as shown in figure 1. This is a regular screen.


Figure 1. An empty form

Form form = new Form("Choose an Item");
The Form object does not use any layout manager. Instead, the Form object will arrange its components like a list, usually from the top to the end. Like an option in the list, each item in the form can be edited, such as insert, add, and delete. The methods of the Form class are listed as follows.

To add an image to the end of the form, you can use:

public int append(Image img);
The following method adds an Item subclass object to the end of the form:

public int append(Item item);
The following method adds a string to the end of the form:

public int append(String str);
The following method deletes the items at the given position on the form and changes the size of the form:

public void delete(int itemNum);
You can use the following method to access any items in the form, while the content of the form remains unchanged.

public Item get(int itemNum);
The following method inserts an entry in the form before the specified index:

public void insert(int itemNum, Item item);
The following method replaces the previous one in the form:

public int set(int itemNum, Item item);
Finally, you can use the following size () method to find the current number of items in the form:

public int size( );
GUI components that can be placed on a form include ChoiceGroup, DateField, Gauge, ImageItem, StringItem, and TextField. All these are subclasses of the abstract class Item. Later, we will see how to place these items on the screen. First, let's give an introduction in sequence.

2. Item

The abstract Item class is used as the base class for components that can be placed on forms or alert. All Item objects have a tag (for example, a string is attached to it), which can be accessed using the following method:

public String getLabel( );
public void setLabel(String s);

This abstract class only has these two methods.

3. ChoiceGroup

The ChoiceGroup object describes a set of Optional options placed on the form. Similar to the List class, it also implements the Choice interface and inherits the Item abstract class. This object can specify one or more options. The ChoiceGroup class constructor is as follows:

public ChoiceGroup(String label, int choiceType);
public ChoiceGroup(String label, int choiceType,String[] stringElements, Image[] imageElements);

The first constructor creates an empty ChoiceGroup component and specifies its label and type. Since this class implements the interface Choice, you may think of three options. However, when using a ChoiceGroup component, there are only two options available: EXCLUSIVE and MULTIPLE. The IMPLICIT type does not apply to ChoiceGroup, just like the List component. You do not need to have a menu-like option field in a form (remember that only one option can be selected at a time in the EXCLUSIVE mode; MULTIPLE options can be selected at a time in the MULTIPLE mode ).

The second ChoiceGroup constructor is used to create a new ChoiceGroup object and specify its labels and types. It also has a string array and an image array to set its initial values.

Once you create an empty selection, you can insert, add, or replace the options, just like on a List component. In addition, each option has an integer index value to represent its position in the list. The first option starts from 0, and the last index value is the maximum length of the list minus 1. The ChoiceGroup class provides the following methods to complete these operations.

public int append(String stringElement, Image imageElement);
public void insert(int index, String stringElement, Image imageElement);
public void set(int index, String stringElement, Image imageElement);

Note that an option consists of a string and an optional image. For example, the following code demonstrates how to add several options to the preceding list. Note that the append () method returns an index value (which is specified when this item is created) for future use.

int saveIndex = list.append("save", null);
int deleteIndex = list.append("delete", null);

In addition, you can use the following method to delete any indexes from the quota group:

public void delete(int index);
If you want to retrieve the string or image elements specified by any index, you can use the following method:

public String getString(int index);
public Image getImage(int index);

If you want to set, unset, retrieve the selected index in the current quota group, or query any index to determine whether it is currently selected, you can use the following method:

public int getSelectedIndex( )
public boolean isSelected(int index);
public setSelectedIndex(int index, boolean selected);

Finally, just like operating the List component, you can use a Boolean flag array to set the selection status of the entire consumer group. Note that the getSelectedFlags () method does not return a Boolean array, but modifies an input Boolean array (and as an optimization technique, returns the sequence number of the selected element in integer form ). The length of the array must be at least equal to the number of elements in the list. If the length is longer, the remaining array element values are set to false.

public int getSelectedFlags(boolean[] selectedArray);
public void setSelectedFlags(boolean[] selectedArray);

For a MULTIPLE list, setSelectedFlags () is used to set the status of each element in the list. For an EXCLUSIVE list, only one item in the Boolean array can be true. If no element is true, the first element is selected. If two or more elements are true, the system selects the first element that is true.
The following statement creates a new empty ChoiceGroup object named "Selection" and its type is EXCLUSIVE:

ChoiceGroup choices = new ChoiceGroup("Method of payment", Choice.EXCLUSIVE);
The following code adds several new options to the selection group.

choices.append("Visa", null);
choices.append("Master Card", null);
choices.append("Amex", null);

Similar to the options in a list, you can use insert, add, or delete methods to edit items in a quota group. In addition, each option is referenced by its index value. For example, you can delete the last item in the following sentence:

choices.delete(2);
Note that once a quota group is created and put into use, you cannot use the setCurrent () method to display it like the operation list. A sheet group is a subclass of Item and must be placed on the form. The form object is displayed using the setCurrent () method.

Form form = new Form("Choose one");
form.append(choices);
Display.setCurrent(form);

Figure 2 shows an EXCLUSIVE grouping, and Figure 3 shows a MULTIPLE grouping. Note that the IMPLICIT value is unavailable for the ChoiceGroup class; if used, an IllegalArgumentException exception is displayed.


Figure 2. A mutually exclusive consumer group figure 3. Consumer groups with multiple options
4. DateField

A DateField object is an editable component that describes the date and time information in the calendar that can be displayed on the form object. It can be used to display date or time information, or both. A DateField object can be created using the following constructor:

public DateField(String label, int mode);
public DateField(String label, int mode, TimeZone timeZone);

The first constructor is used to create a DateField object with the specified tag and mode. This mode can be specified by providing a static field (DateField. DATE, DateField. TIME or DateField. DATE_TIME. The DateField. DATE input mode allows you to set DATE information. DateField. TIME is used to set TIME information (hours and minutes). DateField. DATE_TIME is used to set DATE and TIME information.

The DateField object uses the following methods to access the attributes added to the form object (remember that the tag attributes are defined in the abstract class Item ):

public Date getDate( )
public int getInputMode( )
public void setDate(Date date);
public void setInputMode(int mode);

In addition, you can use the toString () method to output a copy of the string-based date or time data.

public String toString( );
The following code creates an object named "Today's date" in the format of DateField. DATE:

DateField date = new DateField("Today’s date", DateField.DATE);
To display a date field, you can first create a Form object and then use its method append () to add a date field.

Form form = new Form("Date Info");
form.append(date);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.