0

In my J2ME app, I have some forms, and some threads running on background. If in any of these threads I decide to display a message box or notification bar on top of the app, I have the problem of not knowing in which form I am, therefore I don't know which form to show after the messagebox or notification bar is hidden.

Does anyone have any suggestions?

4

2 回答 2

1

A ticker is an object that provides scrolling text across the top of the display. A Ticker is associated with the display, not with the screen. You place a Ticker on a screen using the Screen.setTicker(Ticker t) method, as shown in the code below.

You can associate the same Ticker object with multiple screens, however. The implementation renders the Ticker on some constant portion of the display, in this case at the top of the display. Ticker is not an Item. Its derivation directly from java.lang.Object gives you a clue as to why a Ticker can be tied to the display and not to a screen. It doesn't need to be derived from Item, because it really is not something that is placed in a Form.

import javax.microedition.lcdui.Command; 
import javax.microedition.lcdui.CommandListener; 
import javax.microedition.lcdui.Display; 
import javax.microedition.lcdui.Displayable; 
import javax.microedition.lcdui.Ticker; 
import javax.microedition.lcdui.Form; 
/** 
This class demonstrates use of the Ticker MIDP UI 
component class. 
@see javax.microedition.lcdui.Gauge 
*/ 
public class TickerDemo extends Form 
implements CommandListener 
{ 
private String str = 
"This text keeps scrolling until the demo stops..."; 
private Ticker ticker = new Ticker(str); 
private Command back = new Command("Back", Command.BACK, 1); 
private static Displayable instance; 
/** 
Constructor. 
*/ 
public TickerDemo() 
{ 
super("Ticker demo"); 
instance = this; 
addCommand(back); 
setTicker(ticker); 
setCommandListener(this); 
} 
... 
}

Hope this will help you. Thanks

于 2012-04-11T04:47:17.983 回答
1

You can get current form that is already displaying with "Display.getCurrent()".For example this canvas is a SplashScreen that get current form before displays in the screen:

import javax.microedition.lcdui.Canvas;
/*    */ import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Form;
/*    */ import javax.microedition.lcdui.Graphics;
/*    */ import javax.microedition.lcdui.Image;

public class StaticSplashScreen extends Canvas
        implements Runnable {

    private HelloMIDlet mainMidlet;
    private boolean isSplashOver;
    private long currentTime;
    private long previousTime;
    private Form currentForm;


    public StaticSplashScreen(HelloMIDlet mid) {
        this.mainMidlet = mid;
        currentForm = (Form) this.mainMidlet.getDisplay().getCurrent();
        this.previousTime = System.currentTimeMillis();
        new Thread(this).start();
    }

    protected void paint(Graphics g) {
        g.setColor(255, 255, 255);
        g.fillRect(0, 0, getWidth(), getHeight());
        g.setColor(0, 0, 0);
        g.drawString("In the name of God", 40, 70, 0);
    }

    public void run() {
        while (!this.isSplashOver) {
            this.currentTime = System.currentTimeMillis();

            if (this.currentTime - this.previousTime >= 10000L) {
                this.isSplashOver = true;
            }
        }
        this.mainMidlet.getDisplay().setCurrent(currentForm);
    }
}     

In this midlet you can see two forms with some commands.When you press "help" in each form,method() calls and SplashScreen diplays and after 10 seconds you can see the form that launched it again:

public class HelloMIDlet extends MIDlet implements CommandListener {

...

public void commandAction (Command command, Displayable displayable) {
    ...
if (command == helpCommand) {
method ();
}
    ...
}

public Form getForm () {
if (form == null) {
form = new Form ("Welcome");
form.addCommand (getHelpCommand());
form.setCommandListener (this);
}
return form;
}

public void method () {

if (true) {
    StaticSplashScreen sss = new StaticSplashScreen(this);
    this.getDisplay().setCurrent(sss);
} else {
}
}

public Form getForm1 () {
if (form1 == null) {
form1 = new Form ("form1");
form1.addCommand (getHelpCommand ());
form1.setCommandListener (this);
}
return form1;
}

}
于 2012-04-11T05:56:22.757 回答