gpt4 book ai didi

java - 使用 WindowBuilder 的自定义 JPanel 导致崩溃

转载 作者:行者123 更新时间:2023-11-30 09:44:33 25 4
gpt4 key购买 nike

我使用 WindowBuilder Pro 设计了一个表单,部分生成代码为:

JPanel panel = new JPanel();

我正在尝试使用 paint 方法制作自己的 JPanel:

private class DisplayPanel extends JPanel {

public DisplayPanel() {
// TODO Auto-generated constructor stub
}
@Override
protected void paintComponent(Graphics g) {
super.paintComponent(g);
//Paint the graph. Note that x is to the right, y is down.
g.setColor(Color.black);
g.drawRect(0,0,this.getWidth()-1,this.getHeight()-1);
}
@Override
public boolean mouseDrag(Event evt, int x, int y) {
System.out.println(x+", "+y);
return false;
}

}

不幸的是,将构造函数更改为:

JPanel panel = new DisplayPanel();

切换到设计 View 时导致错误:

 WindowBuilder encountered unexpected internal error.

This could be caused by a WindowBuilder bug or by a misconfiguration issue, conflict, partial update, etc.

java.lang.NullPointerException: Argument must not be null
Stack trace:

java.lang.NullPointerException: Argument must not be null
at javax.swing.Spring.checkArg(Spring.java:671)
at javax.swing.Spring.width(Spring.java:640)
at javax.swing.SpringLayout.applyDefaults(SpringLayout.java:1074)
at javax.swing.SpringLayout.putConstraints(SpringLayout.java:1105)
at javax.swing.SpringLayout.getConstraints(SpringLayout.java:1140)
at javax.swing.SpringLayout$SpringProxy.getConstraint(SpringLayout.java:829)
at javax.swing.SpringLayout$SpringProxy.isCyclic(SpringLayout.java:853)
at javax.swing.SpringLayout.isCyclic(SpringLayout.java:907)
at javax.swing.Spring$CompoundSpring.isCyclic(Spring.java:460)
at javax.swing.SpringLayout.isCyclic(SpringLayout.java:907)
at javax.swing.SpringLayout.abandonCycles(SpringLayout.java:919)
at javax.swing.SpringLayout.layoutContainer(SpringLayout.java:1209)
at java.awt.Container.layout(Container.java:1421)
at java.awt.Container.doLayout(Container.java:1410)
at java.awt.Container.validateTree(Container.java:1507)
at java.awt.Container.validateTree(Container.java:1513)
at java.awt.Container.validateTree(Container.java:1513)
at java.awt.Container.validateTree(Container.java:1513)
at java.awt.Container.validate(Container.java:1480)
at org.eclipse.wb.internal.swing.model.component.top.WindowTopBoundsSupport.apply(WindowTopBoundsSupport.java:67)
at org.eclipse.wb.core.model.AbstractComponentInfo.refresh_afterCreate(AbstractComponentInfo.java:241)
at org.eclipse.wb.core.model.ObjectInfo.refreshCreate0(ObjectInfo.java:535)
at org.eclipse.wb.core.model.ObjectInfo.access$0(ObjectInfo.java:529)
at org.eclipse.wb.core.model.ObjectInfo$5$1.run(ObjectInfo.java:486)
at org.eclipse.wb.internal.core.utils.execution.ExecutionUtils.runDesignTime(ExecutionUtils.java:139)
at org.eclipse.wb.core.model.ObjectInfo$5.run(ObjectInfo.java:484)
at org.eclipse.wb.internal.swing.utils.SwingUtils$2.run(SwingUtils.java:76)
at org.eclipse.wb.internal.swing.utils.SwingUtils.invokeLaterAndWait(SwingUtils.java:180)
at org.eclipse.wb.internal.swing.utils.SwingUtils.runLaterAndWait(SwingUtils.java:73)
at org.eclipse.wb.internal.swing.model.component.ComponentInfo.doRefresh(ComponentInfo.java:118)
at org.eclipse.wb.core.model.ObjectInfo.refresh(ObjectInfo.java:482)
at org.eclipse.wb.internal.core.editor.DesignPage.internal_refreshGEF(DesignPage.java:559)
at org.eclipse.wb.internal.core.editor.DesignPage.access$9(DesignPage.java:501)
at org.eclipse.wb.internal.core.editor.DesignPage$8$1.run(DesignPage.java:434)
at org.eclipse.swt.widgets.Synchronizer.syncExec(Synchronizer.java:179)
at org.eclipse.ui.internal.UISynchronizer.syncExec(UISynchronizer.java:150)
at org.eclipse.swt.widgets.Display.syncExec(Display.java:4113)
at org.eclipse.wb.internal.core.editor.DesignPage$8.run(DesignPage.java:431)
at org.eclipse.jface.operation.ModalContext.runInCurrentThread(ModalContext.java:464)
at org.eclipse.jface.operation.ModalContext.run(ModalContext.java:372)
at org.eclipse.jface.dialogs.ProgressMonitorDialog.run(ProgressMonitorDialog.java:507)
at org.eclipse.wb.internal.core.editor.DesignPage.internal_refreshGEF_withProgress(DesignPage.java:450)
at org.eclipse.wb.internal.core.editor.DesignPage.internal_refreshGEF(DesignPage.java:400)
at org.eclipse.wb.internal.core.editor.UndoManager.refreshDesignerEditor(UndoManager.java:381)
at org.eclipse.wb.internal.core.editor.UndoManager.activate(UndoManager.java:90)
at org.eclipse.wb.internal.core.editor.DesignPage.handleActiveState_True(DesignPage.java:248)
at org.eclipse.wb.internal.core.editor.DesignPage.handleActiveState(DesignPage.java:226)
at org.eclipse.wb.internal.core.editor.multi.DefaultMultiMode.showPage(DefaultMultiMode.java:125)
at org.eclipse.wb.internal.core.editor.multi.DefaultMultiMode$1.widgetSelected(DefaultMultiMode.java:63)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:228)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1176)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1200)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1185)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1025)
at org.eclipse.swt.custom.CTabFolder.setSelection(CTabFolder.java:3256)
at org.eclipse.swt.custom.CTabFolder.onMouse(CTabFolder.java:2045)
at org.eclipse.swt.custom.CTabFolder$1.handleEvent(CTabFolder.java:323)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1176)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:3493)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3112)
at org.eclipse.ui.internal.Workbench.runEventLoop(Workbench.java:2405)
at org.eclipse.ui.internal.Workbench.runUI(Workbench.java:2369)
at org.eclipse.ui.internal.Workbench.access$4(Workbench.java:2221)
at org.eclipse.ui.internal.Workbench$5.run(Workbench.java:500)
at org.eclipse.core.databinding.observable.Realm.runWithDefault(Realm.java:332)
at org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Workbench.java:493)
at org.eclipse.ui.PlatformUI.createAndRunWorkbench(PlatformUI.java:149)
at org.eclipse.ui.internal.ide.application.IDEApplication.start(IDEApplication.java:113)
at org.eclipse.equinox.internal.app.EclipseAppHandle.run(EclipseAppHandle.java:194)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(EclipseAppLauncher.java:110)
at org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(EclipseAppLauncher.java:79)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:368)
at org.eclipse.core.runtime.adaptor.EclipseStarter.run(EclipseStarter.java:179)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.eclipse.equinox.launcher.Main.invokeFramework(Main.java:559)
at org.eclipse.equinox.launcher.Main.basicRun(Main.java:514)
at org.eclipse.equinox.launcher.Main.run(Main.java:1311)

这是一个错误吗?还是有其他方法可以做到这一点?

我尝试在 gui 设计器中设置子类,右键单击 Morph-subclass,但是当我选择 DisplayPanel 时出现错误:

new DisplayPanel((Mainwin) null) is not valid source for component creation, it references not existing constructor. 

最佳答案

我将 DisplayPanel 从窗口类中的私有(private)类移到了单独的类中。这消除了错误。

关于java - 使用 WindowBuilder 的自定义 JPanel 导致崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7850954/

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