为什么 scala 不能识别 com.vaadin.ui.MenuBar.MenuItem

发布于 2024-10-06 20:10:33 字数 1540 浏览 2 评论 0原文

我正在尝试使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

无畏 2024-10-13 20:10:33

内部类不能像往常一样导入,这适用于 Java。只需指定类型投影,就像对 Button#ClickEvent 类型所做的那样:

import com.vaadin._
import ui._
import Button._

class MyScalaVaadinApplication extends Application {
  val window = new Window("My Vaadin Application")

  def init {
    window.addComponent(new Button("Click Me", new ClickListener {
      def buttonClick(clickEvent: Button#ClickEvent): Unit = window.showNotification("Just a message")
    }))

    setMainWindow(window)
  }
}

Inner classes cannot be imported as usual it is appropriate for Java. Just specify type projections as it's done for Button#ClickEvent type:

import com.vaadin._
import ui._
import Button._

class MyScalaVaadinApplication extends Application {
  val window = new Window("My Vaadin Application")

  def init {
    window.addComponent(new Button("Click Me", new ClickListener {
      def buttonClick(clickEvent: Button#ClickEvent): Unit = window.showNotification("Just a message")
    }))

    setMainWindow(window)
  }
}
山色无中 2024-10-13 20:10:33

一个非常简单的例子展示了发生了什么。

Java:

package a;
public class Outer { public class Inner {} }

Scala:

import a.Outer
import a.Outer.Inner // causes *Inner is not a member of a.Outer*

请注意,InnerMenuBar.MenuItem 一样,是一个非静态内部类,因此与 Outer 的一个实例相关联>。但是 Scala 允许我们使用 Outer 实例导入 Inner

import a.Outer
import a.Outer.Inner

object dummy {
  val o = new Outer
  val o2 = new Outer
  import o.Inner
  val i = new Inner
  val i2 = new o2.Inner
}

如果我们可以导入 a.Outer.Inner 编译器如何知道Inner 应该属于 Outer 的哪个实例?

希望问题已得到解决。

A very simple example showing what's going on.

Java:

package a;
public class Outer { public class Inner {} }

Scala:

import a.Outer
import a.Outer.Inner // causes *Inner is not a member of a.Outer*

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:

import a.Outer
import a.Outer.Inner

object dummy {
  val o = new Outer
  val o2 = new Outer
  import o.Inner
  val i = new Inner
  val i2 = new o2.Inner
}

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文