我有 2 个组合框。 上面的组合框动态地更改第二个组合框中的项目。
ActionPerformed 事件如何知道该事件是因为上面的组合框更改了项目而触发的?
我尝试过 我将操作命令设置为两个组合框。 (上、下)。当上部组合框更改下部组合框中的项目时,我得到的命令较低,因为它改变了它......
编辑:我更改了描述
最佳答案
为此,您应该使用actionCommand:
//after object creation:
upperCombo.setActionCommand("upper");
lowerCombo.setActionCommand("lower");
然后在你的actionPerformed()中:
String command = event.getActionCommand();
if("upper".equals(command)){
//Do something
} else if("lower".equals(command)){
//Do something
}
这能解决您的问题吗,还是我误解了您的意思?
编辑:
像这样的事情怎么样:
String command = event.getActionCommand();
if("upper".equals(command)){
lowerCombo.removeActionListener(this);
//Do your changes
lowerCombo.addActionListener(this);
} else if("lower".equals(command)){
//Do something
}
编辑(再次;)):
class YourClass{
private ActionListener lowerActionListener = new java.awt.event.ActionListener() {
void actionPerformed(java.awt.event.ActionEvent evt) {
//doSomething
}
};
//...
// maybe in the constructor, after object-creation
lowerCB.addActionListener(lowerActionListener);
//...
// in th actionPerfomed() method
lowerCB.removeActionListener(lowerActionListener);
}
关于java - 过滤组合框上 ActionPerformed 的源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23662930/