Java window 生成器:运行时错误

Java window builder: runtime error

我使用 Eclipse 的 WindowBuilder 插件创建了非常简单的 Gui。我正在使用 Swing(也许是个问题?) 我有很多运行时错误:

Exception in thread "AWT-EventQueue-0" java.lang.NoClassDefFoundError: com/jgoodies/common/base/Preconditions
    at com.jgoodies.forms.layout.FormSpec.<init>(FormSpec.java:179)
    at com.jgoodies.forms.layout.ColumnSpec.<init>(ColumnSpec.java:147)
    at com.jgoodies.forms.layout.FormSpecs.<clinit>(FormSpecs.java:62)
    at com.jgoodies.forms.layout.LayoutMap.createRoot(LayoutMap.java:569)
    at com.jgoodies.forms.layout.LayoutMap.getRoot(LayoutMap.java:217)
    at com.jgoodies.forms.layout.ColumnSpec.decode(ColumnSpec.java:199)
    at it.myweb.project.GUI.TestGUI.initialize(TestGUI.java:50)
    at it.myweb.project.GUI.TestGUI.<init>(TestGUI.java:39)
    at it.myweb.project.GUI.TestGUI.run(TestGUI.java:26)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access0(EventQueue.java:97)
    at java.awt.EventQueue.run(EventQueue.java:709)
    at java.awt.EventQueue.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
Caused by: java.lang.ClassNotFoundException: com.jgoodies.common.base.Preconditions
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 23 more

代码:

import java.awt.EventQueue;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import javax.swing.JTextField;

import com.jgoodies.forms.layout.ColumnSpec;
import com.jgoodies.forms.layout.FormLayout;
import com.jgoodies.forms.layout.RowSpec;

public class TestGUI {

    private JFrame frame;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ChatGUI window = new ChatGUI();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public ChatGUI() {
        initialize();
    }

    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frame = new JFrame();
        frame.setBounds(100, 100, 1006, 737);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(new FormLayout(new ColumnSpec[] {
                ColumnSpec.decode("default:grow"),
                ColumnSpec.decode("right:max(50dlu;default)"),},
            new RowSpec[] {
                RowSpec.decode("fill:default:grow"),
                RowSpec.decode("bottom:max(30dlu;default)"),}));

        JTextArea textArea = new JTextArea();
        frame.getContentPane().add(textArea, "1, 1, 2, 1, fill, fill");

        textField = new JTextField();
        frame.getContentPane().add(textField, "1, 2, fill, center");
        textField.setColumns(10);

        JButton btnNewButton = new JButton("Invia");
        frame.getContentPane().add(btnNewButton, "2, 2, center, center");
    }
}

请注意,jgoodies 包含在引用的库中。

jgoodies jar 应该包含在您的应用程序的运行时类路径中。你提到它包含在引用的库中,这大概意味着它在编译类路径上,但这还不够。要在运行时类路径中包含 jar,请使用 -cp switch when 运行 java application with command line.

确保项目同时包含 jgoodies-common-x.x.x.jar 和 jgoodies-forms-x.x.x.jar

如果您创建一个新的 window 项目并向 UI 添加一个 jgoodies 组件,您将获得 jar 文件,然后您可以将其复制到您的原始项目中并引用它。

如果你的项目是maven,最好添加jgoodies作为maven依赖:

<dependency>
    <groupId>com.jgoodies</groupId>
    <artifactId>jgoodies-forms</artifactId>
    <version>1.8.0</version>
</dependency>

这解决了所有问题。