将标签添加到Scala Swing面板时出现类型不匹配错误 [英] Type mismatch error when adding Label to Scala Swing Panel

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

问题描述

我让这个类扩展了FlowPanel,我正在尝试在其中添加标签:

I have this Class extending FlowPanel and I'm trying to add Labels into it:

import java.awt.{Label, Color}
import scala.swing._
import scala.util.Random    

class MyPanel extends FlowPanel{
  val dimension = new Dimension(600,400)
  maximumSize = dimension
  minimumSize = dimension
  preferredSize = dimension
  foreground = Color.white
  background = Color.LIGHT_GRAY

  def drowLabels(size: Int) = {
    for(i <- 0 until size){
      contents += new Label()
      revalidate();
      repaint();
    }
  }

但是我收到一条错误消息:

But I get an error message:

type mismatch;
found   : java.awt.Label
required: scala.swing.Component
    contents += new Label()
                ^

但是例如,如果我将new Label()更改为new Button(),则一切正常.实际上,我无法将Label添加到任何类型的容器中,总会出现一些错误.

But for example if I change new Label() to new Button(), everything works fine. Actually I can't add Label to any kind of container, there are always some errors.

一个小时以来,我一直在寻找答案,但是没有成功.

I have been trying to find answer for an hour, but without succeed.

推荐答案

我认为该消息告诉您,期望使用SWING组件而不是java.awt.Label(请查看您的导入). SWING标签为 javax.swing.JLabel ,因此按以下方式修复导入应该可以解决您的问题:

I think the message is telling you that a SWING component is expected which java.awt.Label is not (look at your imports). The SWING label is javax.swing.JLabel, so fixing the imports as follows should solve your problem:

import java.awt.Color
import javax.swing.JLabel
import scala.swing._
import scala.util.Random

class MyPanel extends FlowPanel {
    ...
    def drowLabels(size: Int) = {
        for(i <- 0 until size){
            contents += new JLabel()
            revalidate();
            repaint();
         }
    }

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

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