这更像是一个与编程风格相关的问题,而不是与功能相关的问题。
我看到的所有创建 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个参数。但是,如果可能的话,我希望按照以下样式删除它们并“设置”它们:
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
我如何才能像使用 dialog.setTitle()
一样“设置”对话框文本、确定按钮文本等?每个其他参数是否都有相应的 setter ?
如有任何帮助,我们将不胜感激。
最佳答案
除了 setTitle()
之外,show (String title, String text, String okText, String cancelText)
中使用的参数没有 setter 。您可以在official docs中搜索,除了方法show
,没有其他对text
、okText
或cancelText
的引用。您必须为方法 show
的重载放置所有参数,这就是 API 的制作方式。
如果你想做额外的工作来获得更多的 setter,你可以像@James H 建议的那样(我不知道为什么我一开始没有想到):创建一个派生类。我很确定 show ()
的参数没有任何对应的字段,所以在派生类中(例如 OkCancelDialog
)你可以添加字段 text
、okText
和 cancelText
,创建默认构造函数和参数化构造函数以及这些字段的 getter/setter,以及 show
使用这些字段的方法。然后,您只需将 Dialog
更改为 OkCancelDialog
。
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);
}
}
关于java - 如何以编程方式 'set' 对话框的所有参数,例如 'dialog title' 、 'ok text' 等,而无需将字符串文字作为参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38778943/