Graphical user interface programming is very important in program development because this is a bridge interacting between procedures and users. It's hard to imagine that an app doesn't have a user interface. In J2SE we can develop graphical user interfaces through the AWT / SWING, or you can use a near future SWT. When the J2ME expert organizes the graphical user interface in the MIDP, considering the performance of the device is limited, AWT / SWING is mainly designed for desktop applications, and eventually does not use AWT / SWING as a graphical user interface library of MIDP. The smart javax.microedition.lcdui package was redesigned. This chapter will mainly tell the architecture of the MIDP graphical user interface, and the role of each class and a brief introduction to use.
This article will be introduced in accordance with MIDP 2.0 specification, we must clearly know that there is a low-level graphical user interface and advanced graphical user interface in the MIDP graphical user interface. The same event handling mechanism also has a high low.
First we demonstrate the architecture of the graphical user interface in MIDP2.0 by chart.
From the structure we can see that DisplayAble is a superclass for all user interface components. The two branches from DISPLAYABLE are Screen and the other is Canvas. The former is the superclass of the advanced graphical user interface, the latter is the superclass of the low-level graphical user interface. First let's see the advanced graphical user interface, Screen's four subclasses are TextBox, List, Alert, and Form. One of the three is a component of a predefined type, and their content is basically defined, such as TextBox is a multi-line input box. The last FORM class is open type or a container type. FORM is responsible for managing Item (ITEM we talk), Form is planned to lay out according to Item information. Below we describe the functionality of these four advanced graphical user interface components
Textbox is a component that provides multiple line data to the user, and we can also limit the user's input. The constructor is as follows (String Title, String Text, Int Maxsize, Int Constraints)
List is a component that provides the user selection option, and the List implements the Choice interface. Usually it is available to make menus and list information such as phone book. The constructor is List (String Title, Int Listtype) list (String Title, Int Listtype, String "StringElements, Image [] Imageelements
Alert is used to display information to the user within a period of time, or it can be used as a confirmation box. Structure Alert (String Title) Alert (String Title, String AlertText, Image AlertImage, AlertType AlertType)
The Form is used as a container, which is to collect user input information by adding a corresponding Item, such as adding TextField. The constructor is form (String Title) form (String Title, Item [] Items)
Finally, one of us must introduce is Display, which is a representative of the device screen, only one DISPLAY reference in our program, is obtained by method Display.getDisplay (MIDlet MIDlet). Through it, we get some information about the current device screen, such as color, but most importantly we show users different screens through Display's setCurrent () method.
Below we must introduce the Item class, which is the parent class that can be added to the component on the FORM container, let's take a look at the Item inheritance structure
In MIDP2.0, there are 8 subclasses of Item, and their functions are different, and they will no longer be introduced here. Simple explanation only for classs that are commonly used. The TextField class is usually used, which can only provide a single line of input. The DateField class provides the user enters the date. ChoicGroup also implements the Choice interface, we can use it as a radio and multiple selection. CustomItem is provided to the user in MIDP 2.0, in fact it is a user interface between advanced and low, we can call it enhanced graphical user interface because it can be added to the Form, and can be implemented A series of abstract methods to draw themselves. For example, you can implement a table component. The Canvas class is the parent class of all lower-level user components, and we draw screens by implementing the Paint (Graphics G) method of the Canvas class, mainly using the various methods provided by the Graphics class. A Gamecanvas class is provided in MIDP 2.0, which is designed for game development. About the Canvas class can be said to be very detailed, but here is just to introduce the architecture so no more.
Next, let's take a look at the event handling mechanism in the MIDP graphical user interface. In the advanced graphical user interface, we must understand three listener and a Command, where the principle is the callback mechanism, and the callback is widely used in graphical interface programming. Its principle is that the underlying function calls the high-level function. In Java, we define an interface, such as CommandListener, which define a method such as CommandAction (Command CMD, Displayable Displayable). When the user triggers the event, such as the user presses a Command, then the command () method defined in the CommandListener will be called to handle the relevant operation. The principles of other two listener, ItemStateListener and ItemMandListener are the same as the principles of CommandListener. When using advanced events, the steps we follow are to implement the interface - registration listener.
Low-level event processing is more complicated, and we need to determine the user's input to perform the corresponding processing. For keyboard events, the following three methods are used to receive the user's input KeyPressed (int keycode) KeyreleAsed (int keycode) for touch screen events, we can accept user input events by way of the following methods. Pointerdragged (int X, int y) PointerReleaseD (int X, int y) For more descriptions about low-level events, please refer to this site for other articles, here no longer explain.
Summary: This article is not a detailed analysis of a problem, but through the introduction to the architecture, you will grasp the MIDP graphical user interface. In fact, our learning is the process of organizing and digesting foreign knowledge in the mind, often consciously summarizing and abstracting learning is very helpful.