将 JLabel/JPanel 添加到 Scala Swing 面板时出现类型不匹配错误
Type mismatch error when adding JLabel/JPanel to Scala Swing Panel
我正在尝试将 java 本机 JLabel(或 JPanel)合并到 scala swing 应用程序中,但我遇到了编译错误。
我正在尝试 运行 的代码如下。
package com.david
import javax.swing.JPanel
import scala.swing._
object Hello extends SimpleSwingApplication {
def top = new MainFrame {
title = "First Swing App"
contents = new JPanel()
}
}
编译器给我以下错误。
$ sbt compile
[info] Set current project to MyGui (in build file:/home/david/programming/scala/MyGui/)
[info] Compiling 1 Scala source to /home/david/programming/scala/MyGui/target/scala-2.11/classes...
[error] /home/david/programming/scala/MyGui/src/main/scala/com/david/Hello.scala:12: type mismatch;
[error] found : javax.swing.JPanel
[error] required: scala.swing.Component
[error] contents = new JPanel()
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Apr 10, 2015 12:13:11 PM
此外,这是我的 build.sbt
文件:
name := "MyGui"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "1.0.1"
系统如下。
$ java -version
java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
$ uname -a
Linux strela 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
我知道人们有 had similar problems,但这是不同的 -- 我使用的是正确的组件(JLabel/JPanel 而不是 Label/Panel)。
Scala swing 使用薄包装组件抽象,其中 scala.swing.Component
基础 class 包装 Java swing 组件而不是其 class 层次结构的一部分。
这意味着您必须使用 scala swing 中的 Panel/Label,或者使用 Component.wrap(javaSwingComponent)
将 java swing 组件变成可以与其他 scala 一起使用的 scala swing 组件摆动组件。
我正在尝试将 java 本机 JLabel(或 JPanel)合并到 scala swing 应用程序中,但我遇到了编译错误。
我正在尝试 运行 的代码如下。
package com.david
import javax.swing.JPanel
import scala.swing._
object Hello extends SimpleSwingApplication {
def top = new MainFrame {
title = "First Swing App"
contents = new JPanel()
}
}
编译器给我以下错误。
$ sbt compile
[info] Set current project to MyGui (in build file:/home/david/programming/scala/MyGui/)
[info] Compiling 1 Scala source to /home/david/programming/scala/MyGui/target/scala-2.11/classes...
[error] /home/david/programming/scala/MyGui/src/main/scala/com/david/Hello.scala:12: type mismatch;
[error] found : javax.swing.JPanel
[error] required: scala.swing.Component
[error] contents = new JPanel()
[error] ^
[error] one error found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Apr 10, 2015 12:13:11 PM
此外,这是我的 build.sbt
文件:
name := "MyGui"
version := "1.0"
scalaVersion := "2.11.6"
libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "1.0.1"
系统如下。
$ java -version
java version "1.7.0_76"
Java(TM) SE Runtime Environment (build 1.7.0_76-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.76-b04, mixed mode)
$ uname -a
Linux strela 3.13.0-48-generic #80-Ubuntu SMP Thu Mar 12 11:16:15 UTC 2015 x86_64 x86_64 x86_64 GNU/Linux
我知道人们有 had similar problems,但这是不同的 -- 我使用的是正确的组件(JLabel/JPanel 而不是 Label/Panel)。
Scala swing 使用薄包装组件抽象,其中 scala.swing.Component
基础 class 包装 Java swing 组件而不是其 class 层次结构的一部分。
这意味着您必须使用 scala swing 中的 Panel/Label,或者使用 Component.wrap(javaSwingComponent)
将 java swing 组件变成可以与其他 scala 一起使用的 scala swing 组件摆动组件。