我正试图将java本机JLabel (或JPanel)集成到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)。
发布于 2015-04-10 20:18:25
Scala使用了一个瘦包装组件抽象,其中scala.swing.Component基类封装Java组件,而不是它们的类层次结构的一部分。
这意味着您必须使用scala的Panel/Label,或者使用Component.wrap(javaSwingComponent)将java swing组件变成scala组件,这些组件可以与其他scala组件一起使用。
https://stackoverflow.com/questions/29558983
复制相似问题