命令在LWUIT中不起作用

时间:2014-05-16 13:53:50

标签: java java-me lwuit

我在lwuit中扩展了Form类,并创建了一个表单类,它有两个命令,Next和Exit。然后我创建了一个运行的midlet来显示表单。这些命令正在显示,但点击它们时没有任何反应。这是我写的代码:

  

MainForm.java

import com.sun.lwuit.*;
import com.sun.lwuit.events.ActionEvent;
import com.sun.lwuit.events.ActionListener;
import com.sun.lwuit.layouts.GridLayout;

public class MainForm extends Form implements ActionListener{
private Label label;
private RadioButton epl, laliga, seria, uefa, bundesliga;
private Command exit, next;
private String leagueName;
private ButtonGroup bg;
private TestMIDlet midlet;

public MainForm(TestMIDlet midlet){
    this.midlet = midlet;
    setTitle("Main Page");
    GridLayout gl = new GridLayout(6,1);
    setLayout(gl);
    label = new Label("Choose a league to proceed");
    epl = new RadioButton("EPL");
    laliga = new RadioButton("La liga");
    seria = new RadioButton("Seria A");
    bundesliga = new RadioButton("Bundesliga");
    uefa = new RadioButton("UEFA Champions League");
    uefa.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "International Clubs";
        }
    });
    bundesliga.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Germany";
        }
    });
    seria.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Italy";
        }
    });
    laliga.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "Spain";
        }
    });
    epl.addActionListener(new ActionListener() {
        public void actionPerformed(ActionEvent actionEvent) {
            leagueName = "England";
        }
    });
    bg = new ButtonGroup();
    bg.add(epl);
    bg.add(laliga);
    bg.add(seria);
    bg.add(bundesliga);
    bg.add(uefa);
    next = new Command("Next",2);
    exit = new Command("Exit", 2);
    addComponent(label);
    addComponent(epl);
    addComponent(laliga);
    addComponent(seria);
    addComponent(bundesliga);
    addComponent(uefa);
    addCommand(exit);
    addCommand(next);
}

public void actionPerformed(ActionEvent evt) {
    Command c = evt.getCommand();
    if (c == exit){
        midlet.destroyApp(false);
        midlet.notifyDestroyed();
    }
    else if (c == next){
            System.out.println(leagueName);
    }
}

}

2 个答案:

答案 0 :(得分:1)

我揭示了你的整个计划,我找到了适合你的解决方案。在此处您实施了ActionListener,但尚未将CommandListener添加到Form。这是单击它们时未调用命令的原因。请遵循以下规范并在那里使用。

this.addCommandListener(this);

现在一切都在您的代码中完美无缺。如果您遇到任何其他问题,请告诉我。

答案 1 :(得分:0)

您无法与使用等号的对象进行比较。您必须使用Object.equals(anotherObject)方法。

actionPerformed方法替换为:

public void actionPerformed(ActionEvent evt) {
 Command c = evt.getCommand();
 if (c.equals(exit)){
  midlet.destroyApp(false);
  midlet.notifyDestroyed();
 }
 else if (c.equals(next)) {
  System.out.println(leagueName);
 }
}

编辑: 另外,为了抓住Command,您需要致电setCommandListener(this);addCommandListener(this);