重新分发Intellij IDE GUI应用程序

时间:2014-08-11 13:09:44

标签: java swing intellij-idea gradle

我正在Intellij IDEA 13.1.4社区版中创建新应用

我可以在IDE中运行我的应用程序,但是,当我创建jar时,执行后我得到错误:

java.awt.IllegalComponentStateException: contentPane cannot be set to null.
at javax.swing.JRootPane.setContentPane(JRootPane.java:620)
at javax.swing.JDialog.setContentPane(JDialog.java:1045)
at MainDialog.<init>(MainDialog.java:39)
at MainDialog.createAndShowUI(MainDialog.java:193)
at MainDialog.access$1200(MainDialog.java:13)
at MainDialog$13.run(MainDialog.java:184)
at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
at java.awt.EventQueue.access$200(EventQueue.java:103)
at java.awt.EventQueue$3.run(EventQueue.java:694)
at java.awt.EventQueue$3.run(EventQueue.java:692)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)

我试图在google / stackoverflow上搜索

并在最后一篇文章中附上了链接

以下是我的MainDialog.java示例代码

    public static void main(String[] args) {

    java.awt.EventQueue.invokeLater(new Runnable() {
        public void run() {
            createAndShowUI();
        }
    });
}

private static void createAndShowUI() {
    System.out.println("Start");
    JFrame frame = new JFrame("rConnect");
    try {
        frame.setContentPane(new MainDialog().getContentPane());
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    } catch (IllegalComponentStateException e) {
        e.printStackTrace();
    } finally {
        frame.pack();
        frame.setVisible(true);
    }
    System.out.println("Finish");
}

这是我的build.gradle

apply plugin: 'java'
apply plugin: 'application'

mainClassName = 'com.test.rconnect'
version = '0.1'
sourceCompatibility = 1.7
repositories {
    mavenCentral()
}



jar {

    baseName = 'rConnect'
    from files(sourceSets.main.output.classesDir)
    from files(sourceSets.main.output.resourcesDir)
    from { configurations.compile.collect { zipTree(it) } }

    manifest {
        attributes 'Implementation-Title': 'rConnect'
        attributes 'Implementation-Version': version
        attributes 'Main-Class': 'MainDialog'
    }
}

sourceSets {
    main {
        java {
            srcDir 'src/main/java'
        }
        resources {
            srcDir 'src/main/resources'
        }
    }
}
dependencies {
    testCompile group: 'junit', name: 'junit', version: '4.11'
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'org.apache.commons:commons-lang3:3.3.2'
    compile 'org.apache.commons:commons-io:1.3.2'
    compile 'commons-logging:commons-logging-api:1.1'
    compile 'org.ini4j:ini4j:0.5.2'

}

我也遇到了IDE的奇怪行为

当我清洁&gt;运行应用程序,运行没有问题。

当我做jar时>运行应用程序,它崩溃同样的错误,直到我做清理&gt;运行

有任何帮助吗? 谢谢。

1 个答案:

答案 0 :(得分:7)

我遇到了同样的问题,我在第一个链接中找到了答案:

How to launch swing app with jetbrains layouts in Netbeans

http://www.jetbrains.com/idea/webhelp/gui-designer.html

因此,在GUI Designer中使用Java源文件而不是Binary类文件,并将此依赖项添加到gradle脚本中:

compile 'com.intellij:forms_rt:6.0.5'