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

orSt :

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?

Alexey Romanov :

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.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=416045&siteId=1
Recommended