在Scala Swing中收听RadioButton上的事件

在Scala Swing中收听RadioButton上的事件,swing,events,scala,radio-button,jradiobutton,Swing,Events,Scala,Radio Button,Jradiobutton,如何在scala中收听单选按钮上的事件?我有下面的代码,但由于某种原因,没有执行反应。这是一个对话框,我希望收听radiobutton选择,并相应地更改对话框窗口的标题 val dirFileSelector = { List( new RadioButton("Directory"){ name = "dir" }, new RadioButton("File"){ name = "file" } ) } val button

如何在scala中收听单选按钮上的事件?我有下面的代码,但由于某种原因,没有执行反应。这是一个对话框,我希望收听radiobutton选择,并相应地更改对话框窗口的标题

val dirFileSelector = {
  List(
    new RadioButton("Directory"){
      name = "dir"

    },
    new RadioButton("File"){
      name = "file"
    }
  )
}

val buttonGroup = new ButtonGroup
dirFileSelector map { button=>
  listenTo(button)
  buttonGroup.buttons.add(button) 
}

contents = new BorderPanel{

  add(new BoxPanel(Orientation.Horizontal) {contents ++= dirFileSelector}, BorderPanel.Position.North)
}

reactions += {
  case SelectionChanged(buttonSelect) => {
    println("buttonSelect selection changed")
    buttonSelect.name match {
      case "dir" => title = "Add Directory"
      case "file" => title = "Add File"
    }
  }

}

据我所知,单选按钮不会发出SelectionChanged事件。然而,它们确实会发出辐射

这是一个简单的工作示例,可以获得您想要的效果:

import swing._
import swing.event._

object app extends SimpleSwingApplication {
  val dirFileSelector = List(
    new RadioButton() {
      name = "dir"
      text = "Directory"

    },
    new RadioButton() {
      name = "file"
      text = "File"
    }
  )

  new ButtonGroup(dirFileSelector: _*)

  def top = new MainFrame {
    title = "Test"
    contents = new BoxPanel(Orientation.Horizontal) {
      contents ++= dirFileSelector
    }
    dirFileSelector.foreach(listenTo(_))
    reactions += {
      case ButtonClicked(button) => {
        button.name match {
          case "dir" => title = "Add Directory"
          case "file" => title = "Add File"
        }
      }
    }
  }
}

感谢您的支持,以及关于一次添加所有内容的见解。