我尝试将一些Java代码移植到Scala.
我想在GUI应用程序中显示scala.swing.Applet.
使用Java Swing,我会这样做
val jframe = new JFrame()
jframe.add(APPLET)
jframe.setVisible(true)
APPLET.setFocusCycleRoot(true)
APPLET.init()
APPLET.start()
但是使用Scala Swing的代码
def top = new MainFrame {
contents = APPLET
}
不喜欢的类型:
error: type mismatch;
found : scala.swing.Applet
required: scala.swing.Component
contents = APPLET
当我尝试通过Java对等体添加Applet时,它会像这样失败:
def top = new MainFrame {
contents = new Panel() {
peer.add(new SinglePlayerGame)
}
}
我收到此错误消息:
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)
我该如何解决这个问题?
解决方法:
它不是用Scala编写的,但Subway
是一个混合applet和应用程序的简单示例. JApplet和JFrame都是top-level containers,initContainer()执行公共初始化.
请注意,这两个组件都需要适当注意Initial Threads.