我想根据JComboBox更改按钮的功能。
例如,当我选择单数和复数名词时,按钮的行为应该更改为此项,而当我从JComboBox按钮中选择另一项时,按钮的行为应该相应地更改。
发布于 2015-07-16 17:27:06
您可以将监听程序添加到组合框中:
//In your class
comboList.addActionListener(this);然后,在ActionPerformed中,您可以更改所需功能的按钮的侦听器:
//In your class
JButton btn1 = new JButton("Button1");
--------------------------------------------------------
// Your actionPerformed for combo box listener
public void actionPerformed(ActionEvent e) {
JComboBox cb = (JComboBox)e.getSource();
String itemName = (String)cb.getSelectedItem();
if(itemName.equals("str"){
//checking if you are not adding listener twice
if(btn1.getActionListeners().length > 0){
//remove all the existing listener, iterate and remove if more than one
btn1.removeActionListener(existingListener);
}
btn1.addActionListener(new ButtonListener1());
}
if(itemName.equals("str2"){
//follow same process as for above if
}
}https://stackoverflow.com/questions/31449725
复制相似问题