gpt4 book ai didi

Java - java.lang.Error : SWT Resource was not properly disposed

转载 作者:行者123 更新时间:2023-12-05 01:59:35 27 4
gpt4 key购买 nike

我正在重新开始使用 JAVA 编程语言。重新 self 介绍一下,我正在编写一个简单的银行程序,其中包含单元测试以适应语法。我无法编译代码,因为我看到了以前从未见过的错误。

java.lang.Error: SWT Resource was not properly disposed
at org.eclipse.swt.graphics.Resource.initNonDisposeTracking(Resource.java:172)
at org.eclipse.swt.graphics.Resource.<init>(Resource.java:120)
at org.eclipse.swt.graphics.Image.<init>(Image.java:498)
at org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering.createShadow(CTabRendering.java:910)
at org.eclipse.e4.ui.workbench.renderers.swt.CTabRendering.setShadowColor(CTabRendering.java:1054)
at org.eclipse.e4.ui.css.swt.dom.CTabFolderElement.reset(CTabFolderElement.java:136)
at org.eclipse.e4.ui.css.swt.engine.AbstractCSSSWTEngineImpl.reset(AbstractCSSSWTEngineImpl.java:126)
at org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine.setTheme(ThemeEngine.java:453)
at org.eclipse.e4.ui.css.swt.internal.theme.ThemeEngine.setTheme(ThemeEngine.java:434)
at org.eclipse.ui.internal.dialogs.ViewsPreferencePage.performOk(ViewsPreferencePage.java:264)
at org.eclipse.jface.preference.PreferenceDialog$7.run(PreferenceDialog.java:905)
at org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:45)
at org.eclipse.jface.util.SafeRunnable.run(SafeRunnable.java:174)
at org.eclipse.jface.preference.PreferenceDialog.okPressed(PreferenceDialog.java:889)
at org.eclipse.ui.internal.dialogs.FilteredPreferenceDialog.okPressed(FilteredPreferenceDialog.java:461)
at org.eclipse.jface.preference.PreferenceDialog.buttonPressed(PreferenceDialog.java:233)
at org.eclipse.jface.dialogs.Dialog.lambda$0(Dialog.java:619)
at org.eclipse.swt.events.SelectionListener$1.widgetSelected(SelectionListener.java:84)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:252)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
at org.eclipse.jface.window.Window.runEventLoop(Window.java:823)
at org.eclipse.jface.window.Window.open(Window.java:799)
at org.eclipse.ui.internal.OpenPreferencesAction.run(OpenPreferencesAction.java:66)
at org.eclipse.jface.action.Action.runWithEvent(Action.java:474)
at org.eclipse.jface.action.ActionContributionItem.handleWidgetSelection(ActionContributionItem.java:580)
at org.eclipse.jface.action.ActionContributionItem.lambda$4(ActionContributionItem.java:414)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:89)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4209)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1043)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4026)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3626)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run(PartRenderingEngine.java:1157)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(PartRenderingEngine.java:1046)
at org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(E4Workbench.java:155)
at org.eclipse.ui.internal.Workbench.lambda$3(Workbench.java:644)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:338)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:551)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:156)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:152)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:203)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:134)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:104)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:401)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:255)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:653)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:590)
at org.eclipse.equinox.launcher.Main.run(Main.java:1461)

我在 Google 上做了很多研究,但尚未找到解决方案。有谁知道为什么我在错误日志中两次收到此错误?

我正在使用 Eclipse IDE。这是我的代码:

银行帐户.java:

package simplebank;

public class BankAccount {

private double balance;

public BankAccount()
{
balance = 0;
}

public BankAccount(double initialBalance)
{
balance = initialBalance;
}

public double getBalance()
{
return balance;
}

boolean deposit(double amount)
{

if(amount > 0 && balance - amount > 0)
{
balance = balance + amount;
return true;
}
return false;
}

public boolean withdrawl(double amount)
{
if (amount > 0)
{
balance = balance - amount;
return true;
}

return false;
}

public boolean transfer(BankAccount transferTo, double amount)
{

if (amount > 0)
{
if(this.withdrawl(amount))
{
if(transferTo.deposit(amount))
{

}
else // Return bank account to normal
{
this.deposit(amount);
return false;
}
}

return true;
}

return false;
}

}

银行账户测试.java

package simplebank;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;

class BankAccountTest {

BankAccount account1 = null;
BankAccount account2 = null;
@BeforeAll
public void setup() {
account1 = new BankAccount(20.00);
account2 = new BankAccount();
}

@Test
void testGetBalance() {
assert(account1.getBalance() == 20.00);
}

@Test
void testDeposit() {
account1.deposit(20);
assert(account1.getBalance() == 40.00);
}

@Test
void testWithdrawl() {
account1.withdrawl(10);
assert(account1.getBalance() == 30.00);
}

@Test
void testTransfer() {
account1.transfer(account2, 10);
assert(account1.getBalance() == 20.00);
assert(account2.getBalance() == 10.00);
}
}

作为旁注,如果有人对我如何改进这个简单的代码有任何建议,请留下您的建议!我刚刚回到 JAVA,所以我的风格可能不是最好的。

最佳答案

在 Eclipse 4.19 (2021-03) 中,添加了一个用于跟踪适当资源处置的 API,请参阅 https://www.eclipse.org/eclipse/news/4.19/platform_isv.php#resource-disposal-tracking

这就是这些错误的来源。

尝试在您的 eclipse.ini 中将 true 设置为 false-Dorg.eclipse.swt.graphics.Resource.reportNonDisposed=true摆脱他们。不确定它是否有效。

关于Java - java.lang.Error : SWT Resource was not properly disposed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/67732056/

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