A simplest scalafx example can't be compiled

本秂侑毒 提交于 2019-12-24 03:26:11

问题


I'm trying the scalafx with a simplest example, but it can't be compiled and the error is like:

Error:scalac: bad symbolic reference to javafx.stage.PopupWindow.AnchorLocation encountered in class file 'StageIncludes.class'. Cannot access type AnchorLocation in object javafx.stage.PopupWindow. The current classpath may be missing a definition for javafx.stage.PopupWindow.AnchorLocation, or StageIncludes.class may have been compiled against a version that's incompatible with the one found on the current classpath.

I googled and found someone say we need to add the jfxrt.jar to classpath, but which is still not working.

My code:

build.sbt

name := "ColaBlog"

version := "0.1.0"

scalaVersion := "2.11.0"

libraryDependencies ++= List(
  "org.scalafx" % "scalafx_2.11" % "8.0.0-R4"
)

unmanagedJars in Compile += Attributed.blank(
  file(scala.util.Properties.javaHome) / "lib" / "jfxrt.jar")

fork in run := true

App.scala

import scalafx.Includes._
import scalafx.application.JFXApp
import scalafx.scene.Scene
import scalafx.scene.paint.Color
import scalafx.scene.shape.Rectangle

object App extends JFXApp {

  stage = new JFXApp.PrimaryStage {
    title = "Hello World"
    width = 600
    height = 450
    scene = new Scene {
      fill = Color.LIGHTGREEN
      content = Set(new Rectangle {
        x = 25
        y = 40
        width = 100
        height = 100
        fill <== when(hover) choose Color.GREEN otherwise Color.RED
      })
    }
  }
}

That's all.


回答1:


This works correctly on the Java 8 JVM. The likely problem is that you are running on a Java 7 install.

By these release notes ScalaFX has two parallel releases:

  • v.1.0.0 - supports JavaFX 2 and Java 7 (SFX-1)
  • v.8.0.0 - supports JavaFX 8 and Java 8 (SFX-8)

Either update Java, or use the other release.




回答2:


I know this question is a little dated, but downloading from:

https://jdk8.java.net/download.html

worked for me.

Make sure to change your project settings to use this JDK.



来源:https://stackoverflow.com/questions/23447259/a-simplest-scalafx-example-cant-be-compiled

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!