Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/381.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将函数名传递给函数_Java - Fatal编程技术网

Java 将函数名传递给函数

Java 将函数名传递给函数,java,Java,希望我的措辞正确。所以,我正在开发一个有UI和按钮的程序。我有几件事需要用按钮来完成。当按下按钮时,这些将由一个函数处理 按钮示例: private void columnOneButtonActionPerformed(java.awt.event.ActionEvent evt) { buttonDisable(0); }

希望我的措辞正确。所以,我正在开发一个有UI和按钮的程序。我有几件事需要用按钮来完成。当按下按钮时,这些将由一个函数处理

按钮示例:

private void columnOneButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    buttonDisable(0);
}                                               

private void columnTwoButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                
    buttonDisable(1);
} 
以及功能:

private void buttonDisable(Integer index){
    // deleted code where the index does stuff, that works fine and didn't want to show it

    this.columnOneButton.setEnabled(false);

}
显然,这只适用于columnOneButton,因此为了修复它,我使用了如下索引:

private void buttonDisable(Integer index){
// deleted code where the index does stuff, that works fine and didn't want to show it

        if(index == 0){
        this.columnOneButton.setEnabled(false);
        } else if(index == 1){
        this.columnTwoButton.setEnabled(false);
        } // .... there are more than two buttons
}
虽然这完成了任务,但它是错误的,我想通过传入第二个参数来实现同样的目标,我的想法如下:

private void buttonDisable(Integer index, String buttonName){
// deleted code where the index does stuff, that works fine and didn't want to show it

this.buttonName.setEnabled(false);

}
这显然不起作用,但我希望它起作用,也不知道如何起作用

谢谢大家!

您可以使用:

public void actionPerformed(ActionEvent e) {
    buttonDisable(1, (JButton) e.getSource());
}

private void buttonDisable(Integer index, JButton button){
    button.setEnabled(false);
}
您可以使用:

public void actionPerformed(ActionEvent e) {
    buttonDisable(1, (JButton) e.getSource());
}

private void buttonDisable(Integer index, JButton button){
    button.setEnabled(false);
}
请参阅并仅使用一个处理程序方法请参阅并仅使用一个处理程序方法