我扩展了 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/