好的'JOptionPane包含了大量的静态方法.有许多组合,但要更改某些选项(如按钮),您仍然必须指定其他可选参数 - 通常是默认值(如空图标).这不会导致易于阅读的代码.
此外,方法不是特别一致(int返回对应于***_OPTION常量或按钮索引?)因此需要大量文档来消除歧义:它不易于学习,记忆或写作.
对我来说,创建一个'Builder'包装器似乎很自然.它可能看起来像这样:
String[] buttonText = { "Looks good", "It sucks" };
Object selection = new OptionPaneBuilder("What do you think?")
.question()
.message(messageComponent)
.resizable(true)
.showOptionDialog(parent, buttonText);
return buttonText[0].equals(selection);
Run Code Online (Sandbox Code Playgroud)
最后的方法调用可能是:
// returns int (or enum?)
.showConfirmDialog(parent, JOptionPane.YES_NO_CANCEL_OPTION)
// returns JOptionPane
.build()
// etc...
Run Code Online (Sandbox Code Playgroud)
我很乐意去写它 - 但是我没有找到任何现有的东西让我想知道:我是疯了(这是一个坏主意/有更好的方法)......或者只是无能为力?:-)
那么......有没有人知道任何可以达到这样的东西?
我的感觉是坚持使用JOptionPane对话框,以便UI代表获得字体和系统图标等正确的外观和感觉; 但我想如果替代方案在这方面取得成功,他们也会没事的.
有一个名为oxbow TaskDialogs 的API .它包括一些体面的标准任务对话框 - 布局,你也可以设计自己的.在此API中,您可以自由地使用静态方法创建标准对话框,以及使用其构建器构建所需的对话框.
构建器模式示例:
int choice =
TaskDialogs
.build( owner, "What do you think?",
"You have to choose, either this dialog looks good or it just sucks!" )
.title( "Make a decision!" )
.choice( 0,
new CommandLink( "Looks good", "With good I mean it looks reaaaally awesome!" ),
new CommandLink( "It sucks", "I cant see it anymore, take it away!" ) );
Run Code Online (Sandbox Code Playgroud)