3

私は非常に単純な scala スイング アプリを持っており、Swing アプリがトリガーされた場所からコマンド ライン アプリに値を返したいと考えています。値「b」を選択した場合、ボタンが押されるとすぐに GUI が値「b」を返すようにします。アプリが正しいユーザー入力を待機するようにするにはどうすればよいですか?また、呼び出し元のアプリに値を返すにはどうすればよいですか?

import swing._
import event.{ButtonClicked}
object GuiDemo extends App {        

  object GUI extends SimpleSwingApplication {
    val button = new Button {
      text = "Go!"
    }

    val comboBox = new ComboBox(List("a", "b", "c"))
    def top = new MainFrame {
      contents = new FlowPanel {
        contents += comboBox
        contents += button
      }
    }

    listenTo(button)

    reactions += {
      case ButtonClicked(`button`) => { 
        val selection = comboBox.item
        button.enabled_= (false)
      } 
    }    
  }  

  println("Before starting the GUI")
  GUI.main(args)  
  val myValue = GUI.comboBox.item
  println("""Now I need to make a complicated transformation in scala 
  with the selected value: """ + myValue.map(_.toUpper) ) 
  // how do I get the selected value from the GUI?
}

ありがとう!

編集: 現時点では、瓶にパッケージ化していません。コンパイルしてscalaで実行するだけです...

「GUI」から選択した値を「GuiDemo」scala アプリに返して、scala でさらに処理を行う必要があります。

だから質問は本当にです:

GUI 部分が終了するのを待ってから、選択した値を に返す (引き渡す) 方法GuiDemo

4

3 に答える 3

0

親が指定されていない基本的な JOptionPane の例を次に示します。その結果、JFrame を閉じる必要があります。

import swing._

object InputDialogExample extends SimpleSwingApplication {

  val ui = new BorderPanel {
    //content
  }

  def top = new MainFrame {
    title = "Main Frame"
    contents = ui
  }

  println("Before starting the GUI")

  val choices = List( "alpha", "beta", "gamma" )
  // the first arg is null which results in an empty dialog floating around after the choice is made ( not ideal ).
  val selection = Dialog.showInput( null, null, "Choose an option.", Dialog.Message.Plain, null, choices, choices( 0 ) )

  if( !selection.isEmpty )
    println("Now i have the selected value from the gui: " + selection.head )
}

コメントとあなたのコードで提示した「scala-swing-newbie」リンクからの空のメインフレームに基づいています。オプションが選択されていない場合、選択は [なし] になります。

私は Scala に非常に慣れていないので、それが可能かどうか、または MainFrame を親として「キャスト」して追加のダイアログが生成されないようにする方法がわかりません。

于 2013-06-25T00:01:25.800 に答える