2

我一直在尝试创建一个不限于两个或三个选项(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"
        }
4

2 回答 2

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

于 2013-12-07T00:21:53.330 回答
0

optionType不是为了传入 Scala 类型,实际上我不确定它在这个 API 中的目的是什么。但是你可以设置optionType = Options.YesNoCancel,我认为这应该有效。

于 2013-12-06T19:19:58.440 回答