简体   繁体   English

Netbeans Swing GUI Builder 不适用于 java-module 项目和 Maven

[英]Netbeans Swing GUI Builder not working with java-module project and Maven

I want to use Netbeans' Swing GUI builder together with a Maven project and java modules.我想将 Netbeans 的 Swing GUI 构建器与 Maven 项目和 java 模块一起使用。 It seems like the GUI Builder can not find the compiled classes if they are compiled as part of a java module.如果将它们编译为 java 模块的一部分,则 GUI Builder 似乎找不到已编译的类。 It gives the error "Error in loading component (...) Cannot load component class (...)" (See the whole error in the screenshot at the end of the example)它给出了错误“加载组件时出错(...)无法加载组件类(...)”(请参阅​​示例末尾屏幕截图中的整个错误)

I have pinned it down to a minimal example, using Netbeans 11:我已经使用 Netbeans 11 将其固定为一个最小的示例:

  • File > New Project > Java with Maven > Java Application (use default suggestions and click Finish) File > New Project > Java with Maven > Java Application(使用默认建议并单击完成)

  • In the project tree under "Source Packages", right click and add a new JFrame, and a JPanel.在“Source Packages”下的项目树中,右键单击并添加一个新的 JFrame 和一个 JPanel。

  • Compile the project (Run > Build project), so that you can use the classes with GUI Builder编译项目(运行 > 构建项目),以便您可以在 GUI Builder 中使用这些类

  • Using the GUI Builder, add an instance of the JPanel to the JFrame, by opening the JFrame in design mode, and dragging the JPanel to the JFrame.使用 GUI Builder,通过在设计模式下打开 JFrame 并将 JPanel 拖到 JFrame,将 JPanel 的实例添加到 JFrame。

  • Update the project to using java modules, by adding the file src/main/java/module-info.java :通过添加文件src/main/java/module-info.java将项目更新为使用 java 模块:

module MavenGuiTest {
    requires java.desktop;
    requires java.logging;
}

  • Run "Build project" again.再次运行“构建项目”。 Netbeans then detects that it is now a java module project and shows a dialog: Netbeans 然后检测到它现在是一个 java 模块项目并显示一个对话框: 在此处输入图片说明

  • Click Yes, and the following will be added to pom.xml:点击Yes,在pom.xml中添加如下内容:

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.1</version>
            </plugin>
        </plugins>
    </build>
  • Run "Build project" again.再次运行“构建项目”。 Now the project is compiled.现在项目编译完成。

  • Close the JFrame if it's open in the editor.如果 JFrame 在编辑器中打开,请关闭它。

  • Open it again, and switch to the "Design" tab.再次打开它,然后切换到“设计”选项卡。
  • Now, you get an error:现在,您收到一个错误:

在此处输入图片说明

When clicking Show Exceptions, you can see that the GUI Builder cannot find the compiled class of the JPanel:点击Show Exceptions时,可以看到GUI Builder找不到JPanel的编译类:

在此处输入图片说明

Here is the full exception text:以下是完整的异常文本:

java.lang.ClassNotFoundException: com.mycompany.mavenproject1.NewJPanel
    at org.netbeans.modules.form.project.ProjectClassLoader.findClass(ProjectClassLoader.java:163)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at org.netbeans.modules.form.project.FormClassLoader.findClass(FormClassLoader.java:58)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:588)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:398)
    at org.netbeans.modules.form.project.ClassPathUtils.loadClass(ClassPathUtils.java:89)
    at org.netbeans.modules.form.FormUtils.loadClass(FormUtils.java:1552)
    at org.netbeans.modules.form.PersistenceObjectRegistry.loadClass(PersistenceObjectRegistry.java:73)
    at org.netbeans.modules.form.GandalfPersistenceManager.restoreComponent(GandalfPersistenceManager.java:673)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadComponent(GandalfPersistenceManager.java:949)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:484)
    at org.netbeans.modules.form.GandalfPersistenceManager.loadForm(GandalfPersistenceManager.java:260)
    at org.netbeans.modules.form.FormEditor$2.run(FormEditor.java:327)
    at org.netbeans.modules.form.FormLAF$2.run(FormLAF.java:268)
    at org.netbeans.modules.openide.util.NbMutexEventProvider$Event.doEventAccess(NbMutexEventProvider.java:115)
    at org.netbeans.modules.openide.util.NbMutexEventProvider$Event.readAccess(NbMutexEventProvider.java:75)
    at org.netbeans.modules.openide.util.LazyMutexImplementation.readAccess(LazyMutexImplementation.java:71)
    at org.openide.util.Mutex.readAccess(Mutex.java:225)
    at org.netbeans.modules.form.FormLAF.executeWithLookAndFeel(FormLAF.java:251)
    at org.netbeans.modules.form.FormEditor.loadFormData(FormEditor.java:324)
    at org.netbeans.modules.nbform.FormEditorSupport.loadOpeningForm(FormEditorSupport.java:436)
    at org.netbeans.modules.nbform.FormDesignerTC.loadForm(FormDesignerTC.java:256)
    at org.netbeans.modules.nbform.FormDesignerTC.access$300(FormDesignerTC.java:64)
    at org.netbeans.modules.nbform.FormDesignerTC$PreLoadTask$1.run(FormDesignerTC.java:245)
    at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:313)
    at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:770)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:721)
    at java.desktop/java.awt.EventQueue$4.run(EventQueue.java:715)
    at java.base/java.security.AccessController.doPrivileged(Native Method)
    at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:85)
    at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:740)
    at org.netbeans.core.TimableEventQueue.dispatchEvent(TimableEventQueue.java:136)
    at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:203)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
    at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
    at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)

I cannot figure out how to fix it, and I have not found any workaround.我不知道如何修复它,我也没有找到任何解决方法。

This is just a warning not an error.这只是警告而不是错误。 You can run this project even this occurs.即使发生这种情况,您也可以运行此项目。 This warning showing when these things are happen.此警告显示何时发生这些事情。

  1. When you switch one version to another version.当您将一个版本切换到另一个版本时。
  2. You have unused or corrupted library in your project.您的项目中有未使用或损坏的库。 Try to remove that kind of libraries.尝试删除那种库。
  3. You used to have a custom GUI component and you modified that class and new IDE does not recognize it anymore.您曾经有一个自定义 GUI 组件,并且您修改了该类,而新的 IDE 不再识别它。 There for just press Allow editing button and perform clean and build (the broom and hammer icon).只需按下允许编辑按钮并执行清理和构建(扫帚和锤子图标)。 After build complete, Close netbeans and reopen it.构建完成后,关闭netbeans并重新打开它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM