2012-01-27 40 views
0

我找不出为什么它没有收到操作事件。我按下箭头并选择按钮,仍然没有输出到控制台。当我按下选择按钮或箭头按钮时,ActionListener未收到任何操作执行事件

import com.sun.lwuit.events.*; 
public class LWUITAPP extends javax.microedition.midlet.MIDlet implements ActionListener, CommandListener { 

     Form form = new Form(); 
     form.show(); 
     form.addComponent(list); 

     list.setModel(model); 
} 



public void pauseApp() { 
} 

public void destroyApp(boolean unconditional) { 
} 

public void actionPerformed(ActionEvent evt) { 
    System.out.println ("hii!"); 
    System.out.println(evt.getKeyEvent()); 
} 

public void commandAction(Command c, Displayable d) { 

} 
} 
+0

我觉得你在'公共课'之后立即失去了一些东西;无论是你还是你最后都有一个无关的'}'。 – 2012-01-27 19:22:25

回答

1

你忘了把keyListener设置为Form。您必须将此addKeyListener/addGameKeyListener附加到Form。这应该工作。

+2

你的意思是addKeyListener/addGameKeyListener或addCommandListener ... – 2012-01-29 05:53:20

0

好的:我会尽量保持它SSCCE。

为了拥有注册事件,必须添加行...

Form form = new Form(); 
> form.addCommandListener(this) 
form.show() 

...为了聆听通过actionPerformed方法事件。

作为我的证明,请看一下LWUIT API的这个页面。 https://lwuit.java.net/javadocs/com/sun/lwuit/Form.html#addCommandListener(com.sun.lwuit.events.ActionListener)

有趣的是,实现了addCommandListener()方法来代替Swing应用程序通常使用addActionListener()的位置。