2

2 つまたは 3 つの選択肢 (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 の多くの設計バグの 1 つです。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)rInt

于 2013-12-07T00:21:53.330 に答える
0

optionTypeScala 型を渡すためのものではありません。実際、この API での目的がわかりません。しかし、設定するだけoptionType = Options.YesNoCancelでうまくいくはずです。

于 2013-12-06T19:19:58.440 に答える