gpt4 book ai didi

Java SWT 拖动对话框导致应用程序崩溃

转载 作者:行者123 更新时间:2023-12-02 07:22:47 25 4
gpt4 key购买 nike

我有一个对话框,单击按钮时会加载图像。本质上,当单击按钮时,它会打开按钮上显示的图像的更大版本。对话框打开并显示图像效果很好,但是当我移动对话框时,它会留下痕迹,然后应用程序崩溃。我认为这与使用 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);

最佳答案

由于您正在使用 SWTResourceManager 并调用 dispose(),其定义为:

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

这也会处理所有缓存的Font

为了防止出现异常,请不要调用 dispose(),而是调用 disposeImages()

关于Java SWT 拖动对话框导致应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13993019/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com