How to use a scala.swing.Applet in a Scala Swing application?
I try to port some Java code to Scala.
I want to display a scala.swing.Applet
in a GUI application.
With Java Swing I would do
val jframe = new JFrame()
jframe.add(APPLET)
jframe.setVisible(true)
APPLET.setFocusCycleRoot(true)
APPLET.开发者_StackOverflow中文版init()
APPLET.start()
But code using Scala Swing
def top = new MainFrame {
contents = APPLET
}
doesn't like the type:
error: type mismatch;
found : scala.swing.Applet
required: scala.swing.Component
contents = APPLET
When I try to add the Applet via the Java peers, it fails like this:
def top = new MainFrame {
contents = new Panel() {
peer.add(new SinglePlayerGame)
}
}
I get this error message:
Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: scala.swing.Applet cannot be cast to javax.swing.JComponent
at scala.swing.Container$Wrapper$$anon$1.componentAdded(Container.scala:43)
at java.awt.Container.processContainerEvent(Container.java:2071)
at java.awt.Container.processEvent(Container.java:2042)
at java.awt.Component.dispatchEventImpl(Component.java:4629)
at java.awt.Container.dispatchEventImpl(Container.java:2103)
at java.awt.Component.dispatchEvent(Component.java:4455)
at java.awt.Container.addImpl(Container.java:1081)
at java.awt.Container.add(Container.java:373)
How am I supposed to solve that problem?
It's not written in Scala, but Subway
is a simple example of a hybrid applet and application. Both JApplet
and JFrame
are top-level containers, and initContainer()
performs the common initialization.
Note that both components require proper attention to Initial Threads.
精彩评论