2017-05-16 159 views
0

我在Java中使用Vaadin,我正在学习本教程:Vaadin Upload 所以我创建了一个新的Class名称上传器。但是有一些东西不中我的代码工作,我把什么是不工作的** **的文字:Vaadin上传“无法解析符号”错误上传教程

import com.vaadin.server.FileResource; 
import com.vaadin.ui.*; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.OutputStream; 

/** 
* Created by mflamant on 15/05/2017. 
*/ 
public class Uploader { 
    final Embedded image = new Embedded("Uploaded image"); 
    **image.setVisible(false);** 

    class Image implements Upload.Receiver, Upload.SucceededListener{ 
     public File file; 
     public OutputStream receiveUpload(String filename, String mimeType){ 
      FileOutputStream fos = null; 
      try{ 
       file = new File(filename); 
       fos = new FileOutputStream(file); 
      } catch (final java.io.FileNotFoundException e){ 
       e.printStackTrace(); 
       return null; 
      } 
      return fos; 
     } 

     public void uploadSucceeded(Upload.SucceededEvent event){ 
      image.setVisible(true); 
      image.setSource(new FileResource(file)); 
     } 
    }; 

    Image receiver = new Image(); 
    Upload upload = new Upload("Upload image here", receiver); 
    **upload.setButtonCaption("Start Upload");** 
    **upload.SucceededListener(receiver);** 

    Panel panel = new Panel("Image storage"); 
    Layout panelContent = new VerticalLayout(); 
    **panelContent.addComponents(upload, image);** 
    **panel.setContent;** 


} 

我的错误是“无法解析符号”。你能向我解释为什么这些线路不工作?

+0

'不工作'是对您的问题的一个非常模糊的描述。你需要清楚地解释[什么不起作用](https://stackoverflow.com/help/how-to-ask)。是否有编译错误,运行时错误,意外行为? –

+0

对不起,我的IDE中只有红色。用“无法解析符号” –

回答

1

Upload example没有列出应用程序的整个代码。它只包含特定于上传组件本身的代码片段。如果你只是将它们粘贴到你的课堂上,这些代码片段不会工作。

本示例是Vaadin Documentation的一部分,您需要了解在达到此部分时的基础知识。

示例代码旨在用作构建Vaadin组件的方法的一部分。特别的错误是您只能从可执行代码块调用方法,如image.setVisible(false)。你不能将它们粘贴到你的类声明中,这不是一个有效的Java代码。

指南链接到a working code on Github。正如你可以看到它包含了一切必要的初始化:

public class UploadExample extends CustomComponent implements BookExampleBundle { 
private static final long serialVersionUID = -4292553844521293140L; 

public void init (String context) { 
    //... omitted for brevity 
     basic(layout); 
    //... omitted for brevity 
} 

void basic(VerticalLayout layout) { 
    final Image image = new Image("Uploaded Image"); 
    //the rest of the example code goes here 

请注意,这个类单独仍然没有作为一个独立的应用程序。这只是其中一个组件。

所以,你现在可以做什么:

  • 完成Vaadin Tutorial第一。这应该有助于你理解这些概念。
  • 先阅读Introduction部分文档。这将帮助您构建工作应用程序。然后你可以跳转到特定的组件。
  • 克隆Book Examples来自Github的应用程序,然后尝试弄清楚它是如何工作的。
+1

谢谢!我去做 ! –

相关问题