简体   繁体   中英

How to access a public static final member in a Java class from Scala?

I've been trying to run the following piece of Scala code:

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 should be equal to 3 . However, when I try to build and run in IntelliJ IDEA I recieve this error:

Error:(4, 35) value EXIT_ON_CLOSE is not a member of object javax.swing.JFrame setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)

Adding import javax.swing.JFrame.EXIT_ON_CLOSE does not work either.

Does anyone know what I'm doing wrong?

You need to use the place it's actually defined in, javax.swing.WindowConstants . When the Java compiler sees JFrame.EXIT_ON_CLOSE it's rewritten into WindowConstants.EXIT_ON_CLOSE ; the Scala compiler doesn't do that (neither does Kotlin).

It used to be declared in JFrame but was removed in Java 9 ; it's a source- and binary-compatible change for Java, as described in the link, but not source-compatible for Scala/Kotlin.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM