java - 命令在 LWUIT 中不起作用

标签 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);
    }
}

}

最佳答案

我审查了您的整个程序,并找到了适合您的解决方案。请参阅此处,您实现了 ActionListener,但尚未将 CommandListener 添加到 Form 中。这就是单击命令时未调用命令的原因。按照下面的代码并在那里使用它。

this.addCommandListener(this);

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

关于java - 命令在 LWUIT 中不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23697004/

相关文章:

java - 人脸识别中的主成分分析-python/java

java - H2 数据库 URL 错误

java - 为什么要避免 System.exit()?

java-me - 设置 J2me 应用程序权限的替代方法

java - 如何使用LWUIT主题

java - Spring WebFlux switchIfEmpty 返回不同的类型

java - 如何使用 Apache FOP 1.0 从包含日语字符的 XML 创建 PCL 文档?

java-me - 在循环 java me 中下载图像

multithreading - Lwiit J2ME 中的等待屏幕

java-me - 使用 LWUIT 的饼图、条形图和图表