我如何以编程方式 'set' 对话框的所有参数,例如 'dialog title'、'ok text' 等,而不将字符串文字作为参数?

How can I programmatically 'set' all parameters of a Dialog, such as 'dialog title', 'ok text', etc., without having string literals as parameters?

这更像是一个与编程风格相关的问题,而不是与功能相关的问题。

我看到的创建 CodenameOne 对话框的所有示例都是这样的:

.
.
import com.codename1.ui.Display;
import com.codename1.ui.Form;
import com.codename1.ui.Dialog;
import com.codename1.ui.Button;
.
.
// Create a button
Button myButton = new Button("Click Me");

// Create an action listener for the button
myButton.addActionListener((e) -> Dialog.show("Dialog title", "Dialog text", "OK", null));
.
.
.      

上面的Dialog.show()命令里面有4个参数。但是,如果可能的话,我希望删除它们,并 'set' 它们,按照以下样式:

Dialog myDialog = new Dialog();       // Empty parameter list
myDialog.setTitle("Dialog Title");    // 1st parameter
myDialog.set???   // How can I 'set' "Dialog text" (2nd parameter) like I just did with Dialog Title?
myDialog.set???   // How can I 'set' "OK" (3rd parameter) like I just did with Dialog Title?
myDialog.set???   // How can I 'set' null (4th parameter) like I just did with Dialog Title?

// ...and then just do something like ...

myButton.addActionListener((e) -> myDialog.show());   // empty parameter list

我怎样才能 'set' 对话框文本、确定按钮文本等,就像我可以使用 dialog.setTitle() 一样?每个其他参数是否都有相应的 setter?

如有任何帮助,我们将不胜感激。

对于现有 函数或方法,例如Dialog.show(),您不能在java 中执行此操作。

您想要的构造在其他语言中是可能的,但它不是 java 的一部分。

方法调用参考:http://docs.oracle.com/javase/specs/jls/se8/html/jls-15.html#jls-15.12

您必须始终使用 列表 参数,例如 (arg1, arg2, arg3)

除了setTitle()之外,show (String title, String text, String okText, String cancelText)中使用的参数没有设置器。您可以在official docs中搜索,除了方法show外,没有其他对textokTextcancelText的引用。您必须为方法 show 的重载放置所有参数,这就是 API 的制作方式。

如果你想做额外的工作来获得更多的 setter,你可以像@James H 建议的那样做(我不知道为什么我一开始没有想到):创建一个派生的 class.我很确定 show () 的参数没有任何对应的字段,因此在派生的 class(例如 OkCancelDialog)中,您可以添加字段 textokTextcancelText,为这些字段创建默认构造函数和参数化构造函数以及 getters/setters,以及使用这些字段的 show 方法。然后你只需要改变 DialogOkCancelDialog.

public class OkCancelDialog extends Dialog {
    private String text=null, okText=null, cancelText=null;

    public OkCancelDialog (String text, String okText, String cancelText){
        super ();
        this.text = text;
        this.okText = okText;
        this.cancelText = cancelText;
    }

    public void setText (String text) { this.text = text; }
    public void setOkText (String okText) { this.okText = okText; }
    public void setCancelText (String cancelText) {this.cancelText = cancelText; } 

    public boolean show () { 
        return super.show (this.title, this.text, this.okText, this.cancelText); 
    }       
}

show 是一种静态辅助方法,部分灵感来自 Swing 的 JOptionPaneDialog 的核心功能是 Form/Container.

例如:

Dialog dlg = new Dialog("My Dialog");
dlg.setLayout(new BorderLayout());
dlg.add(BorderLayout.CENTER, new SpanLabel("This is the body of the dialog, I can add anything I want here..."));
dlg.add(BorderLayout.SOUTH, new Button(new Command("OK"));

请注意,使用 Command 会隐式处理 Dialog,但您可以编写一个在 dlg 上调用 dispose() 的侦听器。

您还可以使用接受 Container 作为 Dialog.

主体的静态 show 方法变体之一