我在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);
现在,一切都可以在您的代码中完美运行。让我知道您是否还有其他问题。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句