[英]How can I access to a public final static list declared into an utility class from another class in a Java application?
[英]How to access a public static final member in a Java class from Scala?
我一直在嘗試運行以下 Scala 代碼:
import javax.swing.JFrame
class ScalaClass(title: String) extends JFrame(title: String) {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
// ...
}
object Main {
def main(args: Array[String]) : Unit = {
var sFrame : JFrame = new ScalaClass("Hi");
}
}
JFrame.EXIT_ON_CLOSE
應該等於3
。 但是,當我嘗試在 IntelliJ IDEA 中構建和運行時,我收到此錯誤:
Error:(4, 35) value EXIT_ON_CLOSE is not a member of object javax.swing.JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)
添加import javax.swing.JFrame.EXIT_ON_CLOSE
也不起作用。
有誰知道我做錯了什么?
您需要使用它實際定義的位置javax.swing.WindowConstants
。 當 Java 編譯器看到JFrame.EXIT_ON_CLOSE
它會被重寫為WindowConstants.EXIT_ON_CLOSE
; Scala 編譯器不會這樣做(Kotlin 也不會)。
它曾經在JFrame
中聲明,但在 Java 9 中被刪除; 它是 Java 的源代碼和二進制兼容更改,如鏈接中所述,但與 Scala/Kotlin 的源代碼不兼容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.