J2ME Learning Notes (5) ----- MIDlets GUI (Graphic) Programming HUANGHUI22 [Original]

zhaozj2021-02-16  104

GUI (graphic) programming in MIDlets

1. Overview MIDP defines an API for the MIDP application running in the MIDP container, this API itself is built on the CLDC API. The Java class design of the MIDP User Interface API is not based on the Java Abstract Window Toolkit (AWT) class, but is specifically designed for mobile phones. This type of equipment has only limited screen sizes and keyboard performance. When programmers write graphics applications with MIDP, they can only use the MIDP or CLDC API.

2. MIDP GUI Class 1) All MIDP GUI classes are part of the javax.microedition.lcdui package. 2) The basic abstract graphics of the MIDP user interface is the screen, and the Screen class encapsulates the graphics and users of the device. You can only display one screen each time your application. 3) The MIDP API has "High-Level" and "low-level" UI classes. Advanced UI ----- For example, Form, List, TextBox, TextField, Alert, and Ticker have a device adaptation feature, which supports images, text, text domain, and radio buttons. Low-level UI -----, for example, the Canvas class allows the operator to draw any painting.

3. The main class 1 in the MIDP GUI 1) Graphics class ----- provides a Graphics object for painting 2D geometry objects. The javax.microedition.lcdui.graphics class is not created in the MIDLET, which provides a PAINT () method in the MIDP GUI programming in the MIDP GUI programming, using this parameter. Implement the functionality of the GRAPHICS class in the MIDlet. 2) DisplayAble class ----- is an abstract class, Displayable object handles the MIDlet's GUI output. It has two derived class javax.microedition.lcdui.canvas and javax.microedition.lcdui.screen. 3) CANVAS class ----- CANVAS class allows the operator to draw any painting, with the Graphics class is a low-level UI. The method provided by the Graphics class class is always used in the derived class of the Canvas class. 4) Screen class ----- is a high-level UI, Form, List, TextBox, a Alert class is its derived class.

4. Below is a piece of code for the Graphics class:

Import javax.microedition.midlet. *; import javax.microedition.lcdui. *;

public class MidpGraphics extends MIDlet implements CommandListener {Display display; testCanvas canvas; public MidpGraphics () {display = Display.getDisplay (this); canvas = new testCanvas ();} public void startApp () throws MIDletStateChangeException {display.setCurrent (canvas) ;} public void pauseApp () {} public void destroyApp (boolean unconditional) throws MIDletStateChangeException {} public void commandAction (Command c, Displayable d) {}} class testCanvas extends Canvas {public void paint (Graphics g) {// create the font Object font font = font.getfont (font.face_system, font.style_bold, font.size_medium); // Set font object g.setfont (font); // Painting text g.drawstring ("Hello MIDP", getWidth ) / 2, getHeight () / 2, graphics.hcenter|graphics.top); try {// Painting Image Image image = image.createImage ("MyImage.png"); g.drawImage (image, 0,0, graphics.hcenter | graphics.top);} catch (exception e) {}}} ------- -------------------- Run the effect is displaying text "Hello MIDP" on the center of the screen. 5. Use MIDlet to program different graphical user interface instance code for SaveMymoney bank app: 1) Task statement: The first screen display contains BALANCE ENQUIRY (current balance), Fixed Deposit Enquiry, Check Status ENQUIRY Status) Menu Menu When the user selects Balance Enquiry Should display the progress indicator When the user selects Fixed Deposit Enquiry Should display the progress indicator When the user selects Check Status Enquiry Show the TEXTBOX to enter the check number When the user is inserted, the progress should be displayed after the user Indicator 2. The code is as follows: // Import LCDUI and MIDlet package import javax.microedition.lcdui. *; Import javax.microedition.midlet. *;

// Define the MIDLET PUBLIC CLASS MIDPGUI EXTENDS MIDPGUI class for the extended MIDPGUI class, which creates a Display Display DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY DISPLAY; / / Define Form Form = New Form ("Container Form");

// Screen class derived // definition list (main menu) list menu; // Defines text box textbox input;

// Item class (Gauge class represents a bar chart on the screen) Gauge Gauge = New Gauge ("Your Enquiry Is Being Processed", False, 100, 30);

// Define the command static final command okcommand = new command ("ok", command.ok, 1); static final command.back, 0); static final command.back, 0); static final command exitcommand = New Command ("exit", command.stop, 2); string currentMenu; / / Defines the string variable to identify the current form name.

public MidpGui () {} / ** * The startApp () starts the MIDlet, creates a list of items and * Uses the EXIT command * / public void startApp () throws MIDletStateChangeException {// get the object display display = Display.getDisplay ( This); // Create an initial menu and add UNU = New List ("Enquiries", choice.implicit; menu.append ("current balance", null); menu.Append ("fixed deposit", null); menu .append ("check status", null; // joining the exit command to the form menu.addcommand (EXITCOMMAND); Menu.setCommandListener (this); // Call the mainMenu method, set the initial screen mainmenu (); // Add meter Components to form form.Append (gauge);} // mainmenu method void mainmenu () {Display.setCurrent (MENU); currentMenu = "main";} public void pauseapp () {form = null; display = null; menu = null; input = null; gauge = null;} // invoked when the MIDlet is revoked public void destroyApp (boolean unconditional) throws MIDletStateChangeException {notifyDestroyed ();} // show TextBox components public void showTextBox () {input = new TextBox ("Enter th E CHECK NUMBER: "," "", 20, textfield.any; input.addcommand; input.addcommand (OkCommand); Input.setCommandListener (this); Input.setString (""); Display.SetCurrent (Input) CurrentMenu = "Input";} // Display progress indicator screen public void showform () {form.addcommand (backcommand); form.setCommandListener (this); Display.setCurrent (Form); currentMenu = "form";

// Command activation Public Void CommandAction (Command C, Displayable D) {string label = c.getlabel (); if ("exit") {Try {design (true);} catch (exception e) { }}} Else IF ("BACK")) {if (currentMenu.equals ("infut") || currentMenu.equals ("form")) {// go back to menu mainmenu ();}} else {IF ("OK")) {showform ();} else {list down = (list) display.getCurrent (); switch (down.getSelectedIndIndex ()) {cas 0: showform (); break; Case 1: Showform (); Break; Case 2: ShowTextBox (); Break;}}} }} Note: Instructions for all classes and methods can view J2ME DOC. Path is x: / WTK104 / DOCS / API

转载请注明原文地址:https://www.9cbs.com/read-10565.html

New Post(0)