对于 ComboBox,addActionListener -> actionPerformed?
for ComboBox, addActionListener -> actionPerformed?
如果我有
ComboBox box = b;
b.addActionListener(this);
我不应该期待 this.actionPerformed(event)
被调用吗
组合框什么时候操作?
我有一个带有几个组合框的测试框架,似乎可以运行
通常,但从来没有调用过 actionPerformed
。或许是框架
本身需要以某种方式武装?
你的问题不是很清楚,而且你没有给它一个合适的标题。
如果你想将 ActionListener 添加到 ComboBox
,你可以这样做:
ComboBox box = new ComboBox();
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
}
});
这是正确答案。我使用 com.codename1.ui.Dialog 作为
顶级 window。我改用 com.codename1.ui.Form
现在这些动作正在按预期进行。
Dialog(扩展Form)构造的环境中的东西
正在干扰事件机制。也许是设计使然。
据我了解,您只想从 class 中创建一个 ComboBox 来处理动作事件。为此,我建议 class 继承自 ActionListener 并覆盖(使用 @Override 标记)actionPerformed。如果 class 不是从 ActionListener 继承的,仅仅覆盖动作执行是不够的。
public class MyListener extends ActionListener {
@Override
public void actionPerformed (ActionEvent evt){
//code you want to execute when the event happens
}
public void methodCreatingComboBox(){
ComboBox b = new ComboBox();
b.addActionListener(this);
//other stuffs
}
}
那会很有魅力!您可以将同一个 MyListener 实例用于多个事件。
如果我有
ComboBox box = b;
b.addActionListener(this);
我不应该期待 this.actionPerformed(event)
被调用吗
组合框什么时候操作?
我有一个带有几个组合框的测试框架,似乎可以运行
通常,但从来没有调用过 actionPerformed
。或许是框架
本身需要以某种方式武装?
你的问题不是很清楚,而且你没有给它一个合适的标题。
如果你想将 ActionListener 添加到 ComboBox
,你可以这样做:
ComboBox box = new ComboBox();
box.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent evt) {
}
});
这是正确答案。我使用 com.codename1.ui.Dialog 作为 顶级 window。我改用 com.codename1.ui.Form 现在这些动作正在按预期进行。
Dialog(扩展Form)构造的环境中的东西 正在干扰事件机制。也许是设计使然。
据我了解,您只想从 class 中创建一个 ComboBox 来处理动作事件。为此,我建议 class 继承自 ActionListener 并覆盖(使用 @Override 标记)actionPerformed。如果 class 不是从 ActionListener 继承的,仅仅覆盖动作执行是不够的。
public class MyListener extends ActionListener {
@Override
public void actionPerformed (ActionEvent evt){
//code you want to execute when the event happens
}
public void methodCreatingComboBox(){
ComboBox b = new ComboBox();
b.addActionListener(this);
//other stuffs
}
}
那会很有魅力!您可以将同一个 MyListener 实例用于多个事件。