scala.swing.SimpleSwing应用程序的定期重新绘制

scala.swing.SimpleSwing应用程序的定期重新绘制,scala,scala-swing,Scala,Scala Swing,如果我运行这个示例代码 class ExampleGUIElement extends Panel { preferredSize = new Dimension(100, 100) val rand : Random = Random override def paintComponent(g: Graphics2D) = g.drawLine(rand.nextInt(10),rand.nextInt(10),rand.nextInt(90) + 10 ,rand.nextInt

如果我运行这个示例代码

class ExampleGUIElement extends Panel
{
  preferredSize = new Dimension(100, 100)
  val rand : Random = Random
  override def paintComponent(g: Graphics2D) = g.drawLine(rand.nextInt(10),rand.nextInt(10),rand.nextInt(90) + 10 ,rand.nextInt(90) + 10)
}

object GUITest extends SimpleSwingApplication  
{
  def top = new MainFrame
  {
    contents = new ExampleGUIElement
  }
}
它显然只显示了最初绘制的一条线。如果我想要一个周期性的重新绘制,我通常会制作一个定时器,在最外层的JFrame上调用重新绘制。
但是SimpleSwingApplication没有这个方法,至少我找不到它,而调用top.repaint则没有任何作用。那么,如何定期重新绘制整个内容呢?

考虑调用
顶部的
对等方
(请参阅),然后调用
重新绘制

top.peer.repaint()

另外,别忘了调用
top.pack()

我知道这是过时的,但我将您的面板提取到val中,并添加了一个计时器来重新绘制:

def top = new MainFrame
{
    val example = new ExampleGUIElement 
    contents = example
    val timer=new javax.swing.Timer(250, Swing.ActionListener(e =>
    { 
        example.repaint()
    }))
    timer.start()
}

这对我也不管用。我用override def run=GUITest.top.peer.repaint()创建了一个计时器,但它仍然没有更新。