简体   繁体   English

将JLabel / JPanel添加到Scala Swing Panel中时出现类型不匹配错误

[英]Type mismatch error when adding JLabel/JPanel to Scala Swing Panel

I'm trying to incorporate a java native JLabel (or a JPanel) to a scala swing application and I'm stuck with the compile error. 我试图将Java本机JLabel(或JPanel)合并到scala swing应用程序中,但我陷入了编译错误。

The code I'm trying to run is the following. 我尝试运行的代码如下。

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()
  }
}

The compiler gives me the following error. 编译器给我以下错误。

$ 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

Also, here is my build.sbt file: 另外,这是我的build.sbt文件:

name := "MyGui"

version := "1.0"

scalaVersion := "2.11.6"

libraryDependencies += "org.scala-lang.modules" %% "scala-swing" % "1.0.1"

The system is the following. 系统如下。

$ 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

I know that people have had similar problems , but this is different -- I'm using the correct components (JLabel/JPanel instead of Label/Panel). 我知道人们也遇到过类似的问题 ,但这是不同的-我使用的是正确的组件(JLabel / JPanel而不是Label / Panel)。

Scala swing uses a thin wrapper component abstraction where the scala.swing.Component base class wraps Java swing components rather than being a part of their class hierarchy. Scala swing使用瘦包装器组件抽象,其中scala.swing.Component基类包装Java swing组件,而不是将其作为类层次结构的一部分。

This means you must either use Panel/Label from scala swing, or use Component.wrap(javaSwingComponent) to make java swing components it into a scala swing components that can be used together with other scala swing components. 这意味着您必须使用来自scala swing的Panel / Label,或使用Component.wrap(javaSwingComponent)将Java swing组件变成可与其他scala swing组件一起使用的scala swing组件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM