21

我有一个在 GUI 设计器的帮助下创建的项目。这是主要形式的代码。

public class MainForm {
    MainForm() {
        directLineOkButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               //some action
                }
            }
        });
        crossLineOkButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               //some action
        });
        clearButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            //some action
        });
        cancelButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
            //some action
        });
        saveButton.addActionListener(new ActionListener() {
           public void actionPerformed(ActionEvent arg0) {
          //some action
        });
    }

    public JPanel getMainPanel() {
        return mainPanel;
    }

    private void createUIComponents() {
        drawingPanel = new DrawingPanel();
    }

    private JPanel mainPanel;
    private JComboBox directDirectionCombobox;
    private JButton directLineOkButton;
    private JButton crossLineOkButton;
    private JComboBox crossLineComboBox;
    private JTextField crossLineSizeValue;
    private JButton clearButton;
    private JLabel directLineLabel;
    private JPanel directLinePanel;
    private JLabel crossLineLabel;
    private JPanel crossLinePanel;
    private JPanel okClearButtonPanel;
    private JTextField directLineSizeValue;
    private JButton saveButton;
    private JPanel drawingPanel;
    private JButton cancelButton;
}

它工作正常。Jar 文件生成正常,这里是 pom.xml 的代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>DOC</groupId>
    <artifactId>DOC</artifactId>
    <version>1.0</version>
    <dependencies>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.9</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>2.3.2</version>
                <configuration>
                    <source>1.6</source>
                    <target>1.6</target>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-jar-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <addClasspath>true</addClasspath>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                </configuration>
            </plugin>

            <plugin>
                <artifactId>maven-assembly-plugin</artifactId>
                <configuration>
                    <archive>
                        <manifest>
                            <mainClass>Main</mainClass>
                        </manifest>
                    </archive>
                    <descriptorRefs>
                        <descriptorRef>jar-with-dependencies</descriptorRef>
                    </descriptorRefs>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

但是当我尝试运行 jar 文件时出现了错误。

Exception in thread "main" java.lang.NullPointerException
at MainForm.<init>(MainForm.java:14)
at Main.main(Main.java:13)

它表明错误出现在创建 directLineOkButton 侦听器的行。我像这里所说的那样创建侦听器:http: //www.jetbrains.com/idea/training/demos/GUI_Designer/GUI_Designer.html 在构造函数中使用 cmd+o。这是表单的xml代码:

<?xml version="1.0" encoding="UTF-8"?>
<form xmlns="http://www.intellij.com/uidesigner/form/" version="1" bind-to-class="MainForm">
  <grid id="27dc6" binding="mainPanel" layout-manager="FormLayout">
    <rowspec value="center:23px:noGrow"/>
    <rowspec value="top:3dlu:noGrow"/>
    <rowspec value="center:47px:noGrow"/>
    <rowspec value="top:3dlu:noGrow"/>
    <rowspec value="center:max(d;4px):noGrow"/>
    <rowspec value="top:3dlu:noGrow"/>
    <rowspec value="center:max(d;4px):noGrow"/>
    <rowspec value="top:7dlu:noGrow"/>
    <rowspec value="center:25px:noGrow"/>
    <rowspec value="top:174dlu:noGrow"/>
    <rowspec value="center:max(d;4px):noGrow"/>
    <colspec value="fill:d:noGrow"/>
    <colspec value="left:4dlu:noGrow"/>
    <colspec value="fill:452px:noGrow"/>
    <constraints>
      <xy x="20" y="20" width="797" height="453"/>
    </constraints>
    <properties/>
    <border type="none"/>
    <children>
      <component id="3b663" class="javax.swing.JLabel" binding="directLineLabel">
        <constraints>
          <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties>
          <text value="Direct Line"/>
        </properties>
      </component>
      <grid id="499e0" binding="directLinePanel" layout-manager="FormLayout">
        <rowspec value="center:d:grow"/>
        <colspec value="fill:98px:noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:80px:grow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <constraints>
          <grid row="1" column="0" row-span="2" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties/>
        <border type="none"/>
        <children>
          <component id="c383d" class="javax.swing.JComboBox" binding="directDirectionCombobox">
            <constraints>
              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <enabled value="true"/>
              <model>
                <item value="Вверх"/>
                <item value="Вниз"/>
                <item value="Вправо"/>
                <item value="Влево"/>
              </model>
            </properties>
          </component>
          <component id="17aa1" class="javax.swing.JTextField" binding="directLineSizeValue">
            <constraints>
              <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
                <preferred-size width="150" height="-1"/>
              </grid>
              <forms defaultalign-horz="false"/>
            </constraints>
            <properties/>
          </component>
          <component id="44fc7" class="javax.swing.JButton" binding="directLineOkButton">
            <constraints>
              <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <text value="OK"/>
            </properties>
          </component>
        </children>
      </grid>
      <component id="5a571" class="javax.swing.JLabel" binding="crossLineLabel">
        <constraints>
          <grid row="4" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="0" anchor="8" fill="0" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties>
          <text value="Cross Line"/>
        </properties>
      </component>
      <grid id="77f1a" binding="crossLinePanel" layout-manager="FormLayout">
        <rowspec value="center:d:grow"/>
        <colspec value="fill:98px:noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:80px:grow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <constraints>
          <grid row="6" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties/>
        <border type="none"/>
        <children>
          <component id="32368" class="javax.swing.JComboBox" binding="crossLineComboBox">
            <constraints>
              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="2" anchor="8" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <model>
                <item value="Вверх-вправо"/>
                <item value="Вверх-влево"/>
                <item value="Вниз-вправо"/>
                <item value="Вниз-влево"/>
              </model>
            </properties>
          </component>
          <component id="dbf23" class="javax.swing.JTextField" binding="crossLineSizeValue">
            <constraints>
              <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="6" anchor="8" fill="1" indent="0" use-parent-layout="false">
                <preferred-size width="150" height="-1"/>
              </grid>
              <forms defaultalign-horz="false"/>
            </constraints>
            <properties/>
          </component>
          <component id="c5c8a" class="javax.swing.JButton" binding="crossLineOkButton">
            <constraints>
              <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <text value="OK"/>
            </properties>
          </component>
        </children>
      </grid>
      <grid id="53bbc" binding="okClearButtonPanel" layout-manager="FormLayout">
        <rowspec value="center:d:noGrow"/>
        <colspec value="fill:d:noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <colspec value="left:4dlu:noGrow"/>
        <colspec value="fill:max(d;4px):noGrow"/>
        <constraints>
          <grid row="8" column="0" row-span="1" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties>
          <enabled value="false"/>
        </properties>
        <border type="none"/>
        <children>
          <component id="41ba7" class="javax.swing.JButton" binding="saveButton">
            <constraints>
              <grid row="0" column="0" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <text value="Сохранить"/>
            </properties>
          </component>
          <component id="a6bf6" class="javax.swing.JButton" binding="clearButton">
            <constraints>
              <grid row="0" column="2" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <text value="Очистить"/>
            </properties>
          </component>
          <component id="40f1c" class="javax.swing.JButton" binding="cancelButton">
            <constraints>
              <grid row="0" column="4" row-span="1" col-span="1" vsize-policy="0" hsize-policy="3" anchor="0" fill="1" indent="0" use-parent-layout="false"/>
              <forms/>
            </constraints>
            <properties>
              <text value="Отменить"/>
            </properties>
          </component>
        </children>
      </grid>
      <grid id="2e94e" binding="drawingPanel" custom-create="true" layout-manager="FormLayout">
        <rowspec value="center:d:grow"/>
        <colspec value="fill:d:grow"/>
        <constraints>
          <grid row="0" column="2" row-span="11" col-span="1" vsize-policy="3" hsize-policy="3" anchor="0" fill="3" indent="0" use-parent-layout="false"/>
          <forms/>
        </constraints>
        <properties>
          <background color="-1"/>
        </properties>
        <border type="none"/>
        <children/>
      </grid>
    </children>
  </grid>
</form>
4

5 回答 5

29

您必须包括ideauidesigner-maven-plugin

<build>
    <plugins>
        <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>ideauidesigner-maven-plugin</artifactId>
            <version>1.0-beta-1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>javac2</goal>
                    </goals>
                </execution>
            </executions>

            <configuration>
                <fork>true</fork>
                <debug>true</debug>
                <failOnError>true</failOnError>
            </configuration>
        </plugin>
    </plugins>
</build>

并且可以选择 - 如果您使用GridLayoutManageror JGoodies- 还包括forms_rt作为依赖项:

    <dependency>
        <groupId>com.intellij</groupId>
        <artifactId>forms_rt</artifactId>
        <version>7.0.3</version>
    </dependency>
于 2014-09-16T19:44:35.850 回答
9

我在用 maven 构建项目时遇到了同样的问题。解决我的问题的方法是在设置(在项目|设置|编辑器|GUI设计器中找到)中的“二进制类文件”->“Java源代码”中更改“生成GUI到:”的值。

于 2017-03-02T11:48:26.740 回答
2

我自己找到了决定。

http://glxn.net/2010/08/17/making-a-swing-project-using-intellij-idea-and-gui-builder-with-maven-include-executable-jar
你需要特殊的插件来制作maven与 intelliji idea gui 设计师合作的项目。

于 2013-09-28T13:07:38.637 回答
1

GUI 设计器的问题在于它没有可以使用的 maven 简单插件,因为 Intellij 使用 ant 对目标 .class 文件进行后处理。如果你和我一样不喜欢用 intellij 代码轰炸你的 .java,你可以使用 maven ant 插件:

         <plugin>
            <artifactId>maven-antrun-plugin</artifactId>
            <version>1.7</version>
            <dependencies>
                <dependency>
                    <groupId>com.sun</groupId>
                    <artifactId>tools</artifactId>
                    <version>1.7.0</version>
                    <scope>system</scope>
                    <systemPath>${java.home}/../lib/tools.jar</systemPath>
                </dependency>
            </dependencies>
            <executions>
                <execution>
                    <phase>compile</phase>
                    <configuration>
                        <tasks>
                            <path id="j2cp">
                                <fileset dir="${project.basedir}/src/main/resources/antlibs" includes="*.jar"/>
                            </path>
                            <path id="j2sp">
                                <pathelement location="${project.basedir}/src/main/java"/>
                            </path>

                            <taskdef name="javac2" classpathref="j2cp" classname="com.intellij.ant.Javac2"/>
                            <javac2 destdir="${project.basedir}/target/classes"> 
                                <src refid="j2sp"/>
                            </javac2>
                        </tasks>
                    </configuration>
                    <goals>
                        <goal>run</goal>
                    </goals>
                </execution>
            </executions>
        </plugin>

但是您需要将 javac2 jar 放入资源/antlibs 中,在我的项目中我使用这些:

  • asm-5.0.3.jar
  • asm-all-7.0.1.jar
  • javac2.jar
  • jdom.jar

您可以在 ${Intellij.home}/libs 中找到这些库

如果您使用特定的 IDE 布局,也许您需要其他特定的 jars,如果您错过了某些东西,ant 插件将是特定的

于 2021-01-25T23:25:43.497 回答
0

在您的动态初始化程序(带有 {{}} 的部分)中,您正在访问directLineOkButton

directLineOkButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {
               //some action
                }
            }
        });

但是,该字段尚未分配,因此您会收到 NullPointerException。

于 2013-09-27T12:23:19.063 回答