0

ボタンの数と構成が可変のアプリケーションを構築していますが、アプリケーションはボタンのクリックイベントをリッスンして処理する必要があります。

可変数のボタンと構成の要件があるため、スコープ内listenToで動的に作成されたボタンを呼び出す必要があると感じています。BoxPanelしかし、ボタンのイベントは受信されないことがわかりました。

ただし、作成したボタンを覚えていて、listenToの範囲外で呼び出すとBoxPanel、そのイベントが受信されます。

listenToBoxPanelのスコープで作業しないのはなぜですか?とにかく、そのような使用法でコンパイラエラーは見られませんでした。

以下は私のサンプルコードです:

import scala.swing._
import scala.swing.event._

object TouchSelectUI extends SimpleSwingApplication {
  val touchMap = Vector(
    Vector("a", "b", "c"),
    Vector("d", "e", "g")
    // more may be defined in the future
  )
  val keyDemension = new Dimension(40, 25)
  def top = new MainFrame {
    title = "Touch Select Experiment"
    val input = new TextArea {
      columns = 80
      text = ""
    }
    var aButtonRemebered = new Button()
    contents = new FlowPanel {
      require(touchMap.size > 0, {println("The touch key definition is empty!")})
      for (c <- 0 until touchMap(0).size) {
    contents += new BoxPanel(Orientation.Vertical) {
      for (r <- 0 until touchMap.size) {
        val key = new Button {
        text = touchMap(r)(c)
          }
        contents += key
        aButtonRemebered = key
        listenTo(key) // listenTo here does not work.
      }
    }
      }
    contents += input 
    }

    //listenTo(aButtonRemebered) // Here listTo would work to deliver the event to the following reaction!
    reactions += {
      case ButtonClicked(b) => {
    println(s"Button clicked!")
    val c = b.text
    input.text = input.text + c
      }
    }
  }
}

あなたの助けをどうもありがとう!

ゆう

4

1 に答える 1

1

これはどういう意味ですか?

object TouchSelectUI extends SimpleSwingApplication {

  //..

  def top = new MainFrame {

    val main = this

    //..

    contents = new FlowPanel {
      //..
      for (c <- 0 until touchMap(0).size) {
        contents += new BoxPanel(Orientation.Vertical) {
          for (r <- 0 until touchMap.size) {
            val key = new Button
            //..
            main listenTo key
          }
        }
      }
    }
  }
}

編集

これの代わりに、 LuigiPlingeが良い提案をしています:

  def top = new MainFrame {

    val main = this

これを行うこともできます:

  def top = new MainFrame { main =>

また、彼は次のリンクも提供しました。

于 2013-02-16T09:34:17.200 に答える