我一直在尝试创建一个Option对话框,不仅限于两个或三个选项(Option.YesNo或Option.YesNoCancel),但我一直无法找到使用任何东西的方法,但这些内置 - 在选项中。具体来说,以下内容拒绝接受我可以为optionType添加的任何内容:
object Choices extends Enumeration {
type Choice = Value
val red, yellow, green, blue = Value
}
val options = List("Red", "Yellow", "Green", "Blue")
label.text = showOptions(null,
"What is your favorite color?",
"Color preference",
optionType = Choices.Value,
entries = options,
initial = 2) match {
case Choice.red => "Red"
case Choice.yellow => "Yellow"
case Choice.green => "Green"
case Choice.blue => "Blue"
case _ => "Some other color"
}
答案 0 :(得分:1)
是的,这是Scala-Swing中众多设计错误之一。您可以编写自己的showOptions
方法:
import swing._
import Swing._
import javax.swing.{UIManager, JOptionPane, Icon}
def showOptions[A <: Enumeration](
parent: Component = null,
message: Any,
title: String = UIManager.getString("OptionPane.titleText"),
messageType: Dialog.Message.Value = Dialog.Message.Question,
icon: Icon = EmptyIcon,
entries: A,
initial: A#Value): Option[A#Value] = {
val r = JOptionPane.showOptionDialog(
if (parent == null) null else parent.peer, message, title, 0,
messageType.id, Swing.wrapIcon(icon),
entries.values.toArray[AnyRef], initial)
if (r < 0) None else Some(entries(r))
}
val res = showOptions(message = "Color", entries = Choices, initial = Choices.green)
如果您想传入字符串,请更改为entries: Seq[Any]
,initial: Int
,在通话中使用entries(initial)
,然后返回r
Int
答案 1 :(得分:0)
optionType
不适用于传入Scala类型,实际上我不确定它在此API中的用途。但你可以设置optionType = Options.YesNoCancel
,我认为这应该有效。