我是Scala的新手,以前从未使用过构建工具,并且在获取scalafx quickstart page上的HelloWorld示例进行编译时遇到了一些麻烦。

我以前尝试通过创建新的Scala项目并在构建路径库的选项中将scala IDE 4.7.0 Release作为外部jar添加到scalafx.jar version 8.0.102-R11来运行代码。但是,当我右键单击该项目并选择“运行方式”时,没有选择将其作为Scala应用程序运行,并且尝试在新的运行配置中使用对象名称似乎无效。我决定尝试将命令行与SBT一起使用。

我正在运行Windows 10,并且安装了Java 8以及JDK 12.0.2。我在下载文件夹中的一个名为scalahelloworld的文件夹中创建了一个项目,并运行以下命令:


  sbt new scala / hello-world.g8


这正常创建了hello world项目,并且编译和运行时没有问题。但是,当我尝试按照scalafx快速入门指南上的说明进行操作时,请添加以下行


  libraryDependencies + =“ org.scalafx” %%“ scalafx”%“ 8.0.144-R12”


build.sbt,并在我的主对象中使用其示例代码,它给了我一些编译错误:

C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld>sbt
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
Java HotSpot(TM) 64-Bit Server VM warning: Ignoring option MaxPermSize; support was removed in 8.0
[info] Loading project definition from C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\project
[info] Loading settings for project scalahelloworld from build.sbt ...
[info] Set current project to hello-world (in build file:/C:/Users/hugo.barroca/Downloads/SBT%20Project/scalahelloworld/)
[info] sbt server started at local:sbt-server-3c8f646db648b9d2b646
sbt:hello-world> compile
[info] Compiling 1 Scala source to C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\target\scala-2.12\classes ...
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:15:3: Symbol 'type javafx.event.EventTarget' is missing from the classpath.
[error] This symbol is required by 'class scalafx.stage.Window'.
[error] Make sure that type EventTarget is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Window.class' was compiled against an incompatible version of javafx.event.
[error]   stage = new PrimaryStage {
[error]   ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:17:17: Symbol 'term javafx.css' is missing from the classpath.
[error] This symbol is required by 'trait scalafx.css.Styleable'.
[error] Make sure that term css is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Styleable.class' was compiled against an incompatible version of javafx.
[error]     scene = new Scene {
[error]                 ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:17:5: Symbol 'type javafx.scene.Scene' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.Scene'.
[error] Make sure that type Scene is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Scene.class' was compiled against an incompatible version of javafx.scene.
[error]     scene = new Scene {
[error]     ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:22:15: Symbol 'type javafx.scene.text.Text' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.text.Text'.
[error] Make sure that type Text is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'Text.class' was compiled against an incompatible version of javafx.scene.text.
[error]           new Text {
[error]               ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:25:24: Symbol 'term javafx.scene.paint' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.paint.LinearGradient'.
[error] Make sure that term paint is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'LinearGradient.class' was compiled against an incompatible version of javafx.scene.
[error]             fill = new LinearGradient(
[error]                        ^
[error] C:\Users\hugo.barroca\Downloads\SBT Project\scalahelloworld\src\main\scala\Main.scala:36:26: Symbol 'type javafx.scene.effect.DropShadow' is missing from the classpath.
[error] This symbol is required by 'class scalafx.scene.effect.DropShadow'.
[error] Make sure that type DropShadow is in your classpath and check for conflicting dependencies with `-Ylog-classpath`.
[error] A full rebuild may help if 'DropShadow.class' was compiled against an incompatible version of javafx.scene.effect.
[error]             effect = new DropShadow {
[error]                          ^
[error] 6 errors found
[error] (Compile / compileIncremental) Compilation failed
sbt:hello-world[error] Total time: 4 s, completed 23/08/2019, 13:19:55
>
sbt:hello-world>


我遇到了一些SO页面,这些页面使我相信问题可能是either with my version of Java or JDK或类路径。但是,到目前为止,我还无法解决。我尝试过重新安装Java,JDK,STB,并尝试过不同版本的ScalaFX jar,特别是8.0.12版本。我还尝试在this post之后安装openjfx。到目前为止,还没有任何变化。

我唯一的代码是示例文件的代码:

package hello

import scalafx.application.JFXApp
import scalafx.application.JFXApp.PrimaryStage
import scalafx.geometry.Insets
import scalafx.scene.Scene
import scalafx.scene.effect.DropShadow
import scalafx.scene.layout.HBox
import scalafx.scene.paint.Color._
import scalafx.scene.paint.{Stops, LinearGradient}
import scalafx.scene.text.Text

object Main extends JFXApp {

  stage = new PrimaryStage {
    title = "ScalaFX Hello World"
    scene = new Scene {
      fill = Black
      content = new HBox {
        padding = Insets(20)
        children = Seq(
          new Text {
            text = "Hello "
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(PaleGreen, SeaGreen))
          },
          new Text {
            text = "World!!!"
            style = "-fx-font-size: 48pt"
            fill = new LinearGradient(
              endX = 0,
              stops = Stops(Cyan, DodgerBlue)
            )
            effect = new DropShadow {
              color = DodgerBlue
              radius = 25
              spread = 0.25
            }
          }
        )
      }
    }
  }
}


我将如何解决这个特定问题,以便代码正确编译并显示HelloWorld scalafx窗口?

最佳答案

一点点挖掘表明您很可能需要为JavaFX二进制文件添加特定于操作系统的依赖项。将以下内容添加到build.sbt的底部

lazy val osName = System.getProperty("os.name") match {
  case n if n.startsWith("Linux") => "linux"
  case n if n.startsWith("Mac") => "mac"
  case n if n.startsWith("Windows") => "win"
  case _ => throw new Exception("Unknown platform!")
}

// Add JavaFX dependencies
lazy val javaFXModules = Seq("base", "controls", "fxml", "graphics", "media", "swing", "web")
libraryDependencies ++= javaFXModules.map( m=>
  "org.openjfx" % s"javafx-$m" % "11" classifier osName
)


就像在示例项目中一样:https://github.com/scalafx/scalafx-hello-world/blob/master/build.sbt

对于某些JDK发行版(例如Ubuntu上的OpenJDK发行版),这也可能是一个问题。如果我没记错的话,有两种不同的解决方法。一种是将JavaFX添加到您的依赖项中:

libraryDependencies += "org.openjfx" % "javafx" % "12.0.2" pomOnly()


如果我没记错的话,这可能是最好的方法。如果该工作通过您的软件包管理器添加,在Ubuntu中将是:

sudo apt install openjdk-8-jdk openjfx


https://mvnrepository.com/artifact/org.openjfx/javafxhttps://askubuntu.com/questions/1091157/javafx-missing-ubuntu-18-04可能有用。

另外,正如askubuntu指出的那样,您可能要确保将其设置为在项目中使用Java 8运行。

07-24 13:42