我有一个对话框,可在单击按钮时加载图像。基本上,当单击按钮时,它会打开显示在按钮上的较大版本的图像。对话框打开并显示图像就好了,但是当我移动对话框时,它会留下一条线索然后应用程序崩溃。我认为这与使用SWTResourceManager有关,因为如果我已经将新图像加载到应用程序中而不是已经存在一个,那么我只会遇到此问题。
以下是崩溃时的例外情况
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 :(得分:2)
由于您使用SWTResourceManager
并致电dispose()
,因此定义为:
public static void dispose() {
disposeColors();
disposeImages();
disposeFonts();
disposeCursors();
}
这也会处理所有缓存的Font
。
要防止您看到异常,请不要致电dispose()
,而是致电disposeImages()
。