将JLabel/JPanel添加到Scala Swing Panel中时出现类型不匹配错误 [英] Type mismatch error when adding JLabel/JPanel to Scala Swing Panel

查看:175
本文介绍了将JLabel/JPanel添加到Scala Swing Panel中时出现类型不匹配错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将Java本机JLabel(或JPanel)合并到scala swing应用程序中,但我陷入了编译错误.

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.

我要运行的代码如下.

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

我知道人们有也有类似的问题,但这是不同的-我使用的是正确的组件(JLabel/JPanel而不是Label/Panel).

I know that people have had similar problems, but this is different -- I'm using the correct components (JLabel/JPanel instead of Label/Panel).

推荐答案

Scala swing使用精简包装器组件抽象,其中scala.swing.Component基类包装Java swing组件,而不是其类层次结构的一部分.

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的Panel/Label,或使用Component.wrap(javaSwingComponent)将Java swing组件变成可与其他scala swing组件一起使用的scala 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.

这篇关于将JLabel/JPanel添加到Scala Swing Panel中时出现类型不匹配错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆