2012-12-21 53 views
0

我有一個對話框,單擊按鈕時會加載圖像。本質上,當按鈕被點擊時,它會打開按鈕上顯示的更大版本的圖像。對話框會打開並顯示圖像,但是當我移動對話框時,它會留下一條痕跡,然後應用程序崩潰。我認爲這與使用SWTResourceManager有關,因爲我只有這個問題,如果我已經加載了一個新的圖像到應用程序,而不是已經存在的一個。Java SWT拖動對話框導致應用程序崩潰

這是由於異常時崩潰

java.lang.IllegalArgumentException: Argument not valid 
at org.eclipse.swt.SWT.error(SWT.java:4263) 
at org.eclipse.swt.SWT.error(SWT.java:4197) 
at org.eclipse.swt.SWT.error(SWT.java:4168) 
at org.eclipse.swt.graphics.GC.setFont(GC.java:4405) 
at org.eclipse.swt.widgets.Composite.WM_PAINT(Composite.java:1514) 
at org.eclipse.swt.widgets.Control.windowProc(Control.java:4585) 
at org.eclipse.swt.widgets.Canvas.windowProc(Canvas.java:341) 
at org.eclipse.swt.widgets.Display.windowProc(Display.java:4985) 
at org.eclipse.swt.internal.win32.OS.DispatchMessageW(Native Method) 
at org.eclipse.swt.internal.win32.OS.DispatchMessage(OS.java:2531) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3752) 
at hm_Forms.Dialog_Animal_Photo.open(Dialog_Animal_Photo.java:43) 
at hm_Forms.Frm_Animal$2.widgetSelected(Frm_Animal.java:145) 
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754) 
at hm_Forms.Frm_Animal.open(Frm_Animal.java:71) 
at hm_Composites.Comp_Animal_List$3$1.widgetSelected(Comp_Animal_List.java:118) 
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:240) 
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84) 
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1053) 
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4165) 
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3754) 
at hm_Forms.Frm_Main.open(Frm_Main.java:76) 
at hm_Forms.Frm_Main.main(Frm_Main.java:60) 

---代號爲保存圖像到應用程序---

btnSave.addSelectionListener(new SelectionAdapter() { 
     @Override 
     public void widgetSelected(SelectionEvent e) { 
      String path = txtPhotoPath.getText(); 
      if (CC_Files.fileExists(path)) { 
       ArrayList<String> picTypes = new ArrayList<String>(); 
       picTypes.add(".jpg"); 
       picTypes.add(".png"); 
       picTypes.add(".gif"); 
       int t = 0; 

       for(int i = 0; i < picTypes.size(); i++){ 
        String s = picTypes.get(i); 
        if(path.contains(s.toUpperCase())){ 
         t++; 
        } 
        if(path.contains(s.toLowerCase())){ 
         t++; 
        } 
       } 
       if (t > 0) { 
        SWTResourceManager.dispose(); 
        Image image = (Image) SWTResourceManager.getImage(path); 
        ImageData imgData = image.getImageData(); 
        ImageLoader imageLoader = new ImageLoader(); 
        imageLoader.data = new ImageData[] { imgData }; 
        imageLoader.save(Variables.getStrResources() 
          + "Pics\\" + a.getHerd_id() + "a.jpg", 
          SWT.IMAGE_JPEG); 

        int intH = image.getBounds().height; 
        int intW = image.getBounds().width; 
        int h = (150 * intH)/intW; 
        int w = 150; 
        if (h > 150){ 
         h = 150; 
         w = (150 * intW)/intH; 
        } 
        imgData = imgData.scaledTo(w, h); 
        imageLoader.data = new ImageData[] { imgData }; 
        imageLoader.save(Variables.getStrResources() 
          + "Pics\\" + a.getHerd_id() + ".jpg", 
          SWT.IMAGE_JPEG); 
        image.dispose(); 

        try { 
         Frm_Animal.setAnimalEditSC(Frm_Animal 
           .createAnimalComp(a)); 
        } catch (Exception e1) { 
         // TODO Auto-generated catch block 
         e1.printStackTrace(); 
        } 
       } 
      } 
     } 
    }); 

---代號爲Dialog_Animal_Photo ---

package hm_Forms; 

import hm.Animal; 
import hm.Variables; 

import org.eclipse.swt.SWT; 
import org.eclipse.swt.custom.CLabel; 
import org.eclipse.swt.graphics.Image; 
import org.eclipse.swt.layout.GridData; 
import org.eclipse.swt.layout.GridLayout; 
import org.eclipse.swt.widgets.Dialog; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Shell; 
import org.eclipse.wb.swt.SWTResourceManager; 

import CC_Library.CC_Files; 

public class Dialog_Animal_Photo extends Dialog { 

protected Object result; 
protected Shell shell; 

/** 
* Create the dialog. 
* @param parent 
* @param style 
*/ 
public Dialog_Animal_Photo(Shell parent, int style, Animal a) { 
    super(parent, SWT.DIALOG_TRIM); 
    setText(a.getTag()); 
} 

/** 
* Open the dialog. 
* @return the result 
*/ 
public Object open(Animal a) { 
    createContents(a); 
    shell.open(); 
    shell.layout(); 
    Display display = getParent().getDisplay(); 
    while (!shell.isDisposed()) { 
     if (!display.readAndDispatch()) { 
      display.sleep(); 
     } 
    } 
    return result; 
} 

/** 
* Create contents of the dialog. 
*/ 
private void createContents(Animal a) { 
    shell = new Shell(getParent(), SWT.SHELL_TRIM | SWT.BORDER |  SWT.APPLICATION_MODAL); 

    shell.setText(getText()); 
    shell.setLayout(new GridLayout(1, false)); 

    CLabel lblPic = new CLabel(shell, SWT.NONE); 
    lblPic.setLayoutData(new GridData(SWT.CENTER, SWT.FILL, true, true, 1, 1)); 
    lblPic.setText(""); 

    Image image = null; 

    String strPic = Variables.getStrResources() + "Pics\\" + a.getHerd_id() 
      + "a.jpg"; 
    //SWTResourceManager.dispose(); 
    if (CC_Files.fileExists(strPic)) { 
     image = (Image) SWTResourceManager.getImage(strPic); 
    } else { 
     image = (Image) SWTResourceManager.getImage(Variables 
       .getStrResources() + "black_cow.png"); 
    } 
    //shell.setSize(500,500); 
    shell.setSize(image.getBounds().width + 25,image.getBounds().height + 50); 
    lblPic.setImage(image); 
} 
} 

---代碼爲打開對話框的按鈕---

Dialog_Animal_Photo dap = new Dialog_Animal_Photo(shell, SWT.NONE, a); 
      dap.open(a); 
+0

你有什麼異常嗎? – Baz

+0

我給這個問題增加了異常。 – Talon06

+0

堆棧跟蹤中提到的F​​rm_Main代碼在哪裏? –

回答

2

由於您使用SWTResourceManager和呼叫dispose(),其定義爲:

public static void dispose() { 
    disposeColors(); 
    disposeImages(); 
    disposeFonts(); 
    disposeCursors(); 
} 

這將處理所有緩存Font S以及。

爲防止出現異常,請不要撥打dispose(),而應撥打disposeImages()