我有这个课程扩展FlowPanel
我正在尝试向其中添加标签:
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();
}
}
但我收到一条错误消息:
type mismatch;
found : java.awt.Label
required: scala.swing.Component
contents += new Label()
^
但例如如果我改变new Label()
to new Button()
,一切正常。其实我无法添加Label
对于任何类型的容器,总会存在一些错误。
我已经尝试寻找答案一个小时了,但没有成功。
我认为该消息告诉您需要一个 SWING 组件,该组件java.awt.Label
不是(看看你的进口)。 SWING 标签是javax.swing.JLabel http://docs.oracle.com/javase/7/docs/api/javax/swing/JLabel.html,因此按如下方式修复导入应该可以解决您的问题:
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();
}
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)