JAVA 添加第二个按钮及其 actionListener 后,动作会执行两次
JAVA After adding a seccond button and its actionListener, actions get executed twice
向第二个按钮添加侦听器后,第一个创建的按钮执行两次相同的操作:
public class ControladorTablaMaterial implements ActionListener {
private VistaTablaMaterial vistaTablaMaterial;
private JPanel jContentPane = null;
private JScrollPane scrollPane = null;
private JTable tablaMaterial;
private JButton mostrarElementoButton;
private JButton eliminarElementoButton;
private ModeloTablaMaterial modeloTablaMaterial;
public ControladorTablaMaterial(ArrayList<Material> coleccionMaterial, ActionListener listener) {
String[] cabecera = {"Material", "Titulo"};
this.vistaTablaMaterial = new VistaTablaMaterial(cabecera, coleccionMaterial);
setupVistaTablaMAterial(listener);
}
private void setupVistaTablaMAterial(ActionListener listener) {
this.scrollPane = vistaTablaMaterial.getScrollPane();
this.tablaMaterial = vistaTablaMaterial.getTablaMaterial();
this.modeloTablaMaterial = vistaTablaMaterial.getModeloTablaMaterial();
this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.initListeners(listener);
}
private void initListeners (ActionListener listener) {
getMostrarElementoButton().addActionListener(listener);
getEliminarElementoButton().addActionListener(listener);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
如果我删除以下行,一切正常:
getEliminarElementoButton().addAtionListener(listener);
但当然我也需要那个按钮被收听。
在侦听器 class 中,在 actionPerformed(actionEvent e) 方法中,我使用以下代码来区分两个按钮:
if (e.getSource().equals(this.getControladorTablaMaterial().getMostrarElementoButton())) {
除了这个框架之外,这似乎工作正常。任何猜测?
题外话:为什么代码缩进在 Whosebug 的编辑器上无法正常工作?
问题出在这些行中:
this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
你得到的是同一个按钮。
向第二个按钮添加侦听器后,第一个创建的按钮执行两次相同的操作:
public class ControladorTablaMaterial implements ActionListener {
private VistaTablaMaterial vistaTablaMaterial;
private JPanel jContentPane = null;
private JScrollPane scrollPane = null;
private JTable tablaMaterial;
private JButton mostrarElementoButton;
private JButton eliminarElementoButton;
private ModeloTablaMaterial modeloTablaMaterial;
public ControladorTablaMaterial(ArrayList<Material> coleccionMaterial, ActionListener listener) {
String[] cabecera = {"Material", "Titulo"};
this.vistaTablaMaterial = new VistaTablaMaterial(cabecera, coleccionMaterial);
setupVistaTablaMAterial(listener);
}
private void setupVistaTablaMAterial(ActionListener listener) {
this.scrollPane = vistaTablaMaterial.getScrollPane();
this.tablaMaterial = vistaTablaMaterial.getTablaMaterial();
this.modeloTablaMaterial = vistaTablaMaterial.getModeloTablaMaterial();
this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.initListeners(listener);
}
private void initListeners (ActionListener listener) {
getMostrarElementoButton().addActionListener(listener);
getEliminarElementoButton().addActionListener(listener);
}
@Override
public void actionPerformed(ActionEvent e) {
}
}
如果我删除以下行,一切正常:
getEliminarElementoButton().addAtionListener(listener);
但当然我也需要那个按钮被收听。
在侦听器 class 中,在 actionPerformed(actionEvent e) 方法中,我使用以下代码来区分两个按钮:
if (e.getSource().equals(this.getControladorTablaMaterial().getMostrarElementoButton())) {
除了这个框架之外,这似乎工作正常。任何猜测?
题外话:为什么代码缩进在 Whosebug 的编辑器上无法正常工作?
问题出在这些行中:
this.mostrarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
this.eliminarElementoButton = vistaTablaMaterial.getMostrarElementoButton();
你得到的是同一个按钮。