开发者

Scala applets - SimpleApplet demo

The ScalaDoc for the applet class is pretty thin on details on how yo开发者_Go百科u actually override the ui piece and add components. It says "Clients should implement the ui field. See the SimpleApplet demo for an example."

  1. Where is this SimpleApplet demo?
  2. Barring that, does anyone have some simple source code of using the Scala Applet class, rather than the JApplet class directly?

Thanks


The more recent ScalaDoc may be slightly more helpful (in particular, the new version of ScalaDoc allows you to show/hide concrete members so you can focus on what you must implement).

It should be noted that you don't have to define an object named ui that extends UI. What the ScalaDoc says is both more accurate and more flexible -- "implement the ui field". Because of the Uniform Access Principle, you're free to implement the ui field as a val or an object (similarly, you can use a val or var to implement a def). The only constraints (as reflected in the ScalaDoc as val ui : UI) are that

  1. the ui has to be a UI, and
  2. the reference to the ui has to be immutable

For example:

class MainApplet extends Applet {
  val ui = new MainUI(Color.WHITE)

  class MainUI(backgroundColor: Color) extends UI {
     val mainPanel = new BoxPanel(Orientation.Vertical) {
        // different sort of swing components
        contents.append(new Button("HI"))
     }
     mainPanel.background = backgroundColor // no need for ugly _=
     contents = mainPanel

     def init(): Unit = {}
   }
}


Finally found some source that shows what you need to do:

http://scala-forum.org/read.php?4,701,701

import swing._
import java.awt.Color

class MainApplet extends Applet {

  object ui extends UI {
     val mainPanel = new BoxPanel(Orientation.Vertical) {
     // different sort of swing components
     contents.append(new Button("HI"))
     }
     mainPanel.background = Color.WHITE
     contents = mainPanel

     def init():Unit = {}
  }
}

In other words you define an object named ui that extends UI. I never would have thought of that. That ScalaDoc needs some serious work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜