0

I'm trying to make my very firs j2me program, so please bear with me...

My question: how can I pass a value of string variable to "commandAction"? I mean something like this:

public void test() {
    String myVariable = "hello";
    option1 = new Command("Option 1", Command.EXIT, 2);
    textBox.addCommand(option1);
    textBox.setCommandListener(this);
    Display.getDisplay(this).setCurrent(textBox);
}

public void commandAction(Command cmd, Displayable displayable) {
    if (cmd == option1) {
        print myVariable; // HOW TO MAKE THIS WORK?
}

How can I pass value of "myVariable" from "test" to "commandAction"? Does j2me have some "global variables" or how can I do this?

4

1 回答 1

1

使用实例变量:

private String myVariable;

public void test() {
    myVariable = "hello";
    option1 = new Command("Option 1", Command.EXIT, 2);
    textBox.addCommand(option1);
    textBox.setCommandListener(this);
    Display.getDisplay(this).setCurrent(textBox);
}

public void commandAction(Command cmd, Displayable displayable) {
    if (cmd == option1) {
        print myVariable; 
    }
}
于 2013-04-28T20:37:38.650 回答