Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/392.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_Button_Plugins_Swt - Fatal编程技术网

将静态方法传递给按钮工厂(Java)

将静态方法传递给按钮工厂(Java),java,button,plugins,swt,Java,Button,Plugins,Swt,我正在为Eclipse编写一个小插件(使用SWT),它通过几个按钮创建一个视图。为了不产生冗余代码,我想创建某种工厂方法来为我创建按钮。电话可能是这样的: Button button0 = createButton(new Button(parent, SWT.PUSH), "Test DB zurücksetzen", btnHight, btnWidth, new FormAttachment(0, 2), new FormAttachment(0,2)); 到目前为止,我的工厂是这样的:

我正在为Eclipse编写一个小插件(使用SWT),它通过几个按钮创建一个视图。为了不产生冗余代码,我想创建某种工厂方法来为我创建按钮。电话可能是这样的:

Button button0 = createButton(new Button(parent, SWT.PUSH), "Test DB zurücksetzen", btnHight, btnWidth, new FormAttachment(0, 2), new FormAttachment(0,2));
到目前为止,我的工厂是这样的:

private Button createButton(Button buttonToCreate, String buttonText, int height, int width, FormAttachment left, FormAttachment top) {

    buttonToCreate.setText(buttonText);
    FormData formData = new FormData();
    formData.height = height;
    formData.width = width;
    formData.left = left;
    formData.top = top;
    buttonToCreate.setLayoutData(formData);

    buttonToCreate.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            Actions.editPropertys(); 
        }
    });

    return buttonToCreate;

}

我的问题是:如何将要执行的Actions类的方法作为参数传递给工厂?

您可以将其作为方法的
Runnable
参数传递

例如:

private Button createButton(Button buttonToCreate, String buttonText, int height, int width, FormAttachment left, FormAttachment top, Runnable actionOnSelection) {

  // ...
    buttonToCreate.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent e) {
            actionOnSelection.run(); // change here
        }
    });
}
并调用它:

Button button0 = createButton(new Button(parent, SWT.PUSH), "Test DB zurücksetzen", btnHight, btnWidth, new FormAttachment(0, 2), new FormAttachment(0,2), 
                Actions::editPropertys); // other change here

无需将静态方法传递到工厂方法中,因为您只需使用类名即可调用静态方法。@amitpandey我认为OP希望传递静态方法,以便能够根据对
createButton()
的调用调用调用一个不同的方法!非常感谢。