类的JComboBox需要一个类型参数
问题描述:
我想在我的听众得到的JComboBox实例的值:类的JComboBox需要一个类型参数
object NoteListener extends ActionListener {
def actionPerformed(e:ActionEvent):Unit = {
println("Source: " + e.getSource.asInstanceOf[JComboBox].getValue)
}
}
而且我得到这个错误:当我试图传递任何参数
[error] .../test.scala:30: class JComboBox takes type parameters
[error] println("Source: " + e.getSource.asInstanceOf[JComboBox].getValue)
:
[error] .../test.scala:30: ']' expected but '(' found.
[error] println("Source: " + e.getSource.asInstanceOf[JComboBox(Array)].getValue)
这是一个错误还是我的无知?
答
的情况下你的类型参数是不正确的:
e.getSource.asInstanceOf[JComboBox(Array)].getValue)
应该
e.getSource.asInstanceOf[JComboBox[Array]].getValue)
注[阵列。这是您在Scala中指定类型参数的方式。
答
不知道,但应该给你的JComboBox
e.getSource.peer
我不知道scala,但可能它与Java 7中的JComboBox(以及其他Swing类)的更改有关,也就是说它们已被制作为泛型(即具有类型参数)。 –
我很傻。我用“()”而不是“[]”。它应该是asInstanceOf [JComboBox [Array [java.lang.Object]]] – ciembor