Learning notes (Foundation 4)

Source: Internet
Author: User

-- See the program (memory. Java ):
Package fancy. test;
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class memory extends MIDlet implements commandlistener
{
Private display;
Private form props;
Private command exitcommand = new command ("exit", command. Exit, 1 );
Public memory ()
{
Display = display. getdisplay (this );
}

Public void Startapp ()
{
Props = new form ("runtime information ");
Long Total = runtime. getruntime (). totalmemory ();
Long free = runtime. getruntime (). freememory ();
Props. append ("total memory:" + total + "/N ");
Props. append ("free memory:" + free + "/N ");
Props. addcommand (exitcommand );
Props. setcommandlistener (this );
Display. setcurrent (props );
}
Public void commandaction (command C, displayable S)
{
If (C = exitcommand)
{
Destroyapp (false );
Yydestroyed ();
}
}

Public void destroyapp (Boolean unconditional)
{
}
Public void pauseapp ()
{
Display. setcurrent (null );
Props = NULL;
}
}
This program uses the totalmemory () method of the runtime class and the freememory () method.
In
The runtime class no longer has the function to execute external programs, which is obvious.
List belongs to the javax. microedition. lcdui package. It is the same as form and also belongs to the container type object. Objects of the container type include textbox and alert. We will also introduce the usage of these two classes below. This section describes the usage of list. See the following Program (formlist. Java ):
Package fancy. test;
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class formlist extends MIDlet implements commandlistener
{
Private display;
Private list;
Private command exitcommand = new command ("exit", command. Exit, 1 );

Public formlist ()
{
Display = display. getdisplay (this );
}
Public void Startapp ()
{
List = new list ("choose URL", choice. Exclusive );
List. append ("www.pku.edu.cn", null );
List. append ("www.yahoo.com", null );
List. append ("fancyrainbow@263.net", null );
List. addcommand (exitcommand );
List. setcommandlistener (this );
Display. setcurrent (list );
}
Public void commandaction (command C, displayable S)
{
If (C = exitcommand)
{
Destroyapp (false );
Yydestroyed ();
}
}
Public void destroyapp (Boolean unconditional)
{
}
Public void pauseapp ()
{
Display. setcurrent (null );
List = NULL;
}
}
Please note that the Startapp () method is internal:
List = new list ("choose URL", choice. Exclusive );
List. append ("www.pku.edu.cn", null );
List. append ("www.yahoo.com", null );
List. append ("fancyrainbow@263.net", null );
List. addcommand (exitcommand );
List. setcommandlistener (this );
Display. setcurrent (list );
The logical process is as follows: first, call the constructor to instantiate a list object (list). The list object is actually
Represents a selection list. The first parameter of the list constructor is the name of the List, and the second parameter is the form of the list. Choice. Exclusive indicates that the list can only be selected. If it is choice. Multiple, it indicates that this selection list can be selected multiple times. The append () method of the List class has two parameters. The first parameter is the description of the selected item, and the second parameter is an image object, which represents the small icon before each selected item. The second parameter can be a null value,
First Parameter
Number is required. We can also use the addcommand () method to register commands in the list, or use
Setcomman
The dlistener () method specifies the command listener, which is the same as the form. At the end of the Startapp () method,
Use the setcurrent () method of the display object to set the list object to the current Display object on the screen.
Shows the Running Effect of formlist. Java:

We have used the command object before. The Event System of j2s is special. You must first define a series of commands and then register them in the container objects, such as form, alert, list, And Textbox, and then set the command listener, compile the commandaction () method. When the system sends a command, the commandaction () method is used for overall handling. The following program demonstrates how to define multiple commands and how to compile the commandaction () method.
.
Package fancy. test;
Import javax. microedition. MIDlet .*;
Import javax. microedition. lcdui .*;
Public class cmd extends MIDlet implements commandlistener
{
Private display;
Private form props;
Private command backcommand = new command ("back", command. Back, 2 );
Private command cancelcommand = new command ("cancel", command. Cancel,
1 );
Private command exitcommand = new command ("exit", command. Exit, 1 );
Private command helpcommand = new command ("help", command. Help, 1 );
Private command itemcommand = new command ("item", command. Item, 1 );
Private command okcommand = new command ("OK", command. OK, 1 );
Private command screencommand = new command ("screen", command. screen,

1 );
Private command stopcommand = new command ("stop", command. Stop, 1 );

Public cmd ()
{
Display = display. getdisplay (this );
}
Public void Startapp ()
{
Props = new form ("Hello World ");
Props. append ("Hello world! /N ");
Props. addcommand (backcommand );
Props. addcommand (cancelcommand );
Props. addcommand (exitcommand );
Props. addcommand (helpcommand );
Props. addcommand (itemcommand );
Props. addcommand (okcommand );
Props. addcommand (screencommand );
Props. addcommand (stopcommand );
Props. setcommandlistener (this );
Display. setcurrent (props );
}
Public void showscreen (string cmd)
{
Form = new form ("show cmd ");
Form. append (CMD );
Form. addcommand (exitcommand );
Form. setcommandlistener (this );
Display. setcurrent (form );
}
Public void commandaction (command C, displayable S)
{
If (C = exitcommand)
{
Destroyapp (false );
Yydestroyed ();
}
Else if (C = helpcommand)

{
Showscreen ("help ");
}
Else if (C = backcommand)
{
Showscreen ("back ");
}
Else if (C = cancelcommand)
{
Showscreen ("cancel ");
}
Else if (C = itemcommand)
{
Showscreen ("item ");
}
Else if (C = okcommand)
{
Showscreen ("OK ");
}
Else if (C = screencommand)
{
Showscreen ("screen ");

}
If (C = stopcommand)
{
Showscreen ("stop ");
}

}
Public void destroyapp (Boolean unconditional)
{
}
Public void pauseapp ()
{
Display. setcurrent (null );
Props = NULL;
}
}
In the above Program (CMD. Java), eight commands are defined. If the commandaction () method receives these eight commands, most of them call the showscreen () method and output these commands. The showscreen () method generates a new container object (form) as the current screen, and displays the intercepted commands on the screen.
The Running Effect of CMD. Java is shown in figure 2 (when the "Hello World" text appears on the screen, you need to press the exit key to display the command menu. You can execute the commands in sequence ).

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.