Scala swing GUI编译器指出我的反应块中的某些情况是不可访问的?
这是从大量代码中摘录的,为了简洁起见,删除了不相关的代码。我已经单独测试了下面的代码(作为新项目中的单个类),并验证了同样的问题仍然存在。所以我知道这个问题是特定于这里包含的代码的Scala swing GUI编译器指出我的反应块中的某些情况是不可访问的?,swing,scala,user-interface,Swing,Scala,User Interface,这是从大量代码中摘录的,为了简洁起见,删除了不相关的代码。我已经单独测试了下面的代码(作为新项目中的单个类),并验证了同样的问题仍然存在。所以我知道这个问题是特定于这里包含的代码的 object HumanGUI extends SimpleGUIApplication with Logs { PropertyConfigurator.configure("log4j.properties") def top = new MainFrame { title = "BJ GUI
object HumanGUI extends SimpleGUIApplication with Logs {
PropertyConfigurator.configure("log4j.properties")
def top = new MainFrame {
title = "BJ GUI"
val PlayPanel = new BoxPanel(Orientation.Vertical) {
val hitButton = new Button("Hit")
val stayButton = new Button("Stay")
val doubleButton = new Button("Double")
val quitButton = new Button("Quit")
contents += hitButton
contents += stayButton
contents += doubleButton
contents += quitButton
listenTo(hitButton, stayButton, doubleButton, quitButton)
reactions += {
case ButtonClicked(hitButton) =>
debug("Clicked Hit!")
case ButtonClicked(stayButton) =>
debug("Clicked Stay!")
case ButtonClicked(doubleButton) =>
debug("Clicked Double!")
case ButtonClicked(quitButton) =>
debug("Clicked Quit!")
}
contents = new BoxPanel(Orientation.Vertical) {
contents += playPanel
}
}
具体来说,编译器告诉我,此块中的最后3个案例无法访问:
listenTo(hitButton, stayButton, doubleButton, quitButton)
reactions += {
case ButtonClicked(hitButton) =>
debug("Clicked Hit!")
case ButtonClicked(stayButton) =>
debug("Clicked Stay!")
case ButtonClicked(doubleButton) =>
debug("Clicked Double!")
case ButtonClicked(quitButton) =>
debug("Clicked Quit!")
}
为什么会出现这种情况?当模式匹配时,所有小写变量都将绑定到该情况下匹配的变量,如果要匹配模式匹配之外的变量,则必须使用`(回勾):
谢谢我已经试过了,但是编译器的错误并没有消失。看到你的帖子后,我返回并保存了编译器抱怨的文件,“错误”消失了。它编译得很好,工作正常。
listenTo(hitButton, stayButton, doubleButton, quitButton)
reactions += {
case ButtonClicked(`hitButton`) =>
debug("Clicked Hit!")
case ButtonClicked(`stayButton`) =>
debug("Clicked Stay!")
case ButtonClicked(`doubleButton`) =>
debug("Clicked Double!")
case ButtonClicked(`quitButton`) =>
debug("Clicked Quit!")
}