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

时间:2011-10-21 14:31:32

标签: java swing java-2d windowbuilder

我使用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();
切换到设计视图时

导致错误:

 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. 

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:0)

有同样的问题。 好像JTree给我造成了这个错误。 我将JTree从静态更改为"而不是静态"错误消失了。

当我试图将它改回静态错误时仍然没有... (没有做出其他改动!)

我认为这只是一个窗口构建器错误。