2015-05-01 74 views
0

當我顯示現有的圖像時,它顯示正確的大小(400px寬);而當我導入新圖像時,它顯示的是原始大小(即非常大或很小)。如何讓導入的圖像的行爲方式與顯示現有圖像時的方式相同?GWT - 檢索時圖像顯示大;但現有的圖像正確顯示

我使用的代碼是:

// A panel where the thumbnails of upload images will be shown 
      final HorizontalPanel existingPanelImages = new HorizontalPanel(); 
      final Label fileName = new Label(); 
      final PreloadedImage image = new PreloadedImage(); 

      // Load the image in the document and in the case of success attach it to the viewer 
      // Attach a replacement image to the pictures viewer 
      final OnLoadPreloadedImageHandler showNewImage = new OnLoadPreloadedImageHandler() { 
       public void onLoad(PreloadedImage existingImage) { 
        image.setWidth("400px"); 
        existingPanelImages.clear(); 
        existingPanelImages.add(existingImage); 
        flexTable.setWidget(1, 2, existingPanelImages); 
        existingPanelImages.setWidth("400px"); 
        existingPanelImages.setBorderWidth(1); 
        existingPanelImages.setStyleName("gwt-TextBox"); 
        flexTable.getFlexCellFormatter().setRowSpan(1, 2, 7); 
        flexTable.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_TOP); 
        flexTable.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); 
       } 
      }; 

      final IUploader.OnFinishUploaderHandler onReplaceFinishUploaderHandler = new IUploader.OnFinishUploaderHandler() { 
       @SuppressWarnings("deprecation") 
       public void onFinish(IUploader uploader) { 
        if (uploader.getStatus() == Status.SUCCESS) { 
         new PreloadedImage(uploader.fileUrl(), showNewImage); 

         //The server sends useful information to the client by default 
         UploadedInfo info = uploader.getServerInfo(); 
//           System.out.println("File name " + info.name); 
//           System.out.println("File content-type " + info.ctype); 
//           System.out.println("File size " + info.size);    

         // You can send any customised message and parse it 
//           System.out.println("Server message " + info.message); 
         //Store path to image; 
         imagePath = info.message; 

         if (info.name != null) { 
          fileName.setText(info.name); 
         } 
        } 
       } 
      }; 


      // Attach the image viewer to the document so we can get the Pack Holiday image. 
      //TODO Delete temporary image when finished. 
      flexTable.setWidget(1, 2, existingPanelImages); 
      existingPanelImages.setWidth("400px"); 
      existingPanelImages.setBorderWidth(1); 
      existingPanelImages.clear(); 
      existingPanelImages.setStyleName("gwt-TextBox"); 
      flexTable.getFlexCellFormatter().setRowSpan(1, 2, 7); 
      flexTable.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_TOP); 
      flexTable.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); 

      //Display the current image. 
      String imageDataString = packHoliday.getEventPicture(); 
      final Image existingImage = new Image(imageDataString); 
      existingImage.setWidth("400px"); 
      existingImage.setStyleName("gwt-TextBox"); 
      flexTable.setWidget(1, 2, existingImage); 
      flexTable.getFlexCellFormatter().setRowSpan(1, 2, 7); 
      flexTable.getCellFormatter().setVerticalAlignment(1, 2, HasVerticalAlignment.ALIGN_TOP); 
      flexTable.getCellFormatter().setHorizontalAlignment(1, 2, HasHorizontalAlignment.ALIGN_CENTER); 

      // Create a new uploader panel and attach it to a document 
      final SingleUploader existingdefaultUploader = new SingleUploader(); 
      existingdefaultUploader.setAutoSubmit(true); 
      existingdefaultUploader.setValidExtensions("jpg", "jpeg", "gif"); 
      existingdefaultUploader.setEnabled(true); 
      existingdefaultUploader.avoidRepeatFiles(false); 
      existingdefaultUploader.setStyleName("gwt-TextBox"); 

      // Add a finish handler which will load the image once the upload finishes 
      existingdefaultUploader.addOnFinishUploadHandler(onReplaceFinishUploaderHandler); 
      existingdefaultUploader.getFileInput().getWidget().setStyleName("customButton"); 
      existingdefaultUploader.getFileInput().getWidget().setSize("100px", "20px"); 

      flexTable.setWidget(7, 2, existingdefaultUploader); 
      flexTable.getCellFormatter().setVerticalAlignment(7, 2, HasVerticalAlignment.ALIGN_MIDDLE); 
      flexTable.getCellFormatter().setHorizontalAlignment(7, 2, HasHorizontalAlignment.ALIGN_LEFT); 

任何幫助極大的讚賞。

public void onLoad(PreloadedImage existingImage) { 

以下:

問候,

格林

回答

0

大量的試驗和錯誤,我發現我需要添加到後

existingImage.setWidth("100%"); 

的問候,

Glyn