为什么 scala 不能识别 com.vaadin.ui.MenuBar.MenuItem
我正在尝试使用 Scala 创建一个带有 MenuBar 的 Vaadin 窗口。我收到编译器错误,表明无法找到 com.vaadin.ui.MenuBar.MenuItem 导入。我查看了 Vaadin 库 (6.4.8),看起来该类就在那里:
com/vaadin/ui/MenuBar$Command.class
com/vaadin/ui/MenuBar$MenuItem.class
com/vaadin/ui/MenuBar.class
这是来自 MenuBar 源的类结构:
@SuppressWarnings("serial")
@ClientWidget(value = VMenuBar.class, loadStyle = LoadStyle.LAZY)
public class MenuBar extends AbstractComponent {
...
public interface Command extends Serializable { ... }
public class MenuItem implements Serializable { ... }
}
出于演示目的,这里是一个示例 Scala 类:
import com.vaadin.Application
import com.vaadin.ui.Button
import com.vaadin.ui.Window
import com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.Command
import com.vaadin.ui.MenuBar.MenuItem
class MyVaadinApplication extends Application
{
private var window : Window = null
override def init() =
{
window = new Window("My Scala Vaadin Application")
setMainWindow(window)
window.addComponent(new Button("Click Me"))
}
}
这是当我尝试编译它:
/Users/jstanford/Development/NetBeansProjects/TraderDashboard/src/main/scala:-1: info: compiling
Compiling 2 source files to /Users/jstanford/Development/NetBeansProjects/TraderDashboard/target/classes at 1291973683915
[ERROR]MyVaadinApplication.scala:7: error: MenuItem is not a member of com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.MenuItem
^
有人能发现这里的问题吗?
谢谢, 约翰
I'm trying to create a Vaadin window with a MenuBar using Scala. I'm getting a compiler error that indicates that the com.vaadin.ui.MenuBar.MenuItem import can't be found. I've looked at the Vaadin library (6.4.8), and it appears that the class is there:
com/vaadin/ui/MenuBar$Command.class
com/vaadin/ui/MenuBar$MenuItem.class
com/vaadin/ui/MenuBar.class
Here is the class structure from the MenuBar source:
@SuppressWarnings("serial")
@ClientWidget(value = VMenuBar.class, loadStyle = LoadStyle.LAZY)
public class MenuBar extends AbstractComponent {
...
public interface Command extends Serializable { ... }
public class MenuItem implements Serializable { ... }
}
For demo purposes, here's a sample Scala class:
import com.vaadin.Application
import com.vaadin.ui.Button
import com.vaadin.ui.Window
import com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.Command
import com.vaadin.ui.MenuBar.MenuItem
class MyVaadinApplication extends Application
{
private var window : Window = null
override def init() =
{
window = new Window("My Scala Vaadin Application")
setMainWindow(window)
window.addComponent(new Button("Click Me"))
}
}
And here's the resulting error when I try to compile it:
/Users/jstanford/Development/NetBeansProjects/TraderDashboard/src/main/scala:-1: info: compiling
Compiling 2 source files to /Users/jstanford/Development/NetBeansProjects/TraderDashboard/target/classes at 1291973683915
[ERROR]MyVaadinApplication.scala:7: error: MenuItem is not a member of com.vaadin.ui.MenuBar
import com.vaadin.ui.MenuBar.MenuItem
^
Can anyone spot the problem here?
Thanks,
John
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内部类不能像往常一样导入,这适用于 Java。只需指定类型投影,就像对 Button#ClickEvent 类型所做的那样:
Inner classes cannot be imported as usual it is appropriate for Java. Just specify type projections as it's done for Button#ClickEvent type:
一个非常简单的例子展示了发生了什么。
Java:
Scala:
请注意,Inner 与 MenuBar.MenuItem 一样,是一个非静态内部类,因此与 Outer 的一个实例相关联>。但是 Scala 允许我们使用 Outer 实例导入 Inner:
如果我们可以
导入 a.Outer.Inner
编译器如何知道Inner 应该属于 Outer 的哪个实例?希望问题已得到解决。
A very simple example showing what's going on.
Java:
Scala:
Notice that Inner, like MenuBar.MenuItem, is a non-static inner class and as such is associated with one instance of Outer. But Scala allows us to import Inner using a instance of Outer:
If we could just
import a.Outer.Inner
how would the compiler know to which instance of Outer is Inner supposed to belong to?Hope the issue has been cleared.