无法向 JFrame 添加任何组件 - IllegalArgumentException

Can't add any component to JFrame - IllegalArgumentException

当我尝试在构造函数中向 JFrame 添加任何内容时出现 IllegalArgumentException。

文档中有类似的代码: https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html

为什么我的非常简单的代码不起作用?它是在 Netbeans 10 中创建的。

编辑: 我还尝试添加带有位置的标签和 sieze (setBounds) 它没有帮助。我用 setBounds 方法更改了代码。

首先,JavaTestApp.java中的主要class:

package javatestapp;

public class JavaTestApp {
    public static void main(String[] args) {
        TestForm mainFrame = new TestForm();

        mainFrame.setLocation(300, 150);
        mainFrame.setVisible(true);

        mainFrame.toFront();
        mainFrame.repaint();
    }

}

第二个文件是:

package javatestapp;

import javax.swing.JLabel;

public class TestForm extends javax.swing.JFrame {

    /**
     * Creates new form TestForm
     */
    public TestForm() {
        initComponents();

        JLabel label = new JLabel("Test label");
        label.setBounds(10,10,100,25);        

        getContentPane().add(label);
    }

    // GENERATED CODE BELOW
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        pack();
    }// </editor-fold>                        

    public static void main(String args[]) {
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(TestForm.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new TestForm().setVisible(true);
            }
        });
    }

Stacktrace,关于@VGR 请求:

Exception in thread "main" java.lang.IllegalArgumentException
    at org.netbeans.lib.awtextra.AbsoluteLayout.addLayoutComponent(Unknown Source) 
    at java.awt.Container.addImpl(Container.java:1120)
    at java.awt.Container.add(Container.java:410)
    at javatestapp.TestForm.<init>(TestForm.java:16)
    at javatestapp.JavaTestApp.main(JavaTestApp.java:5) 
BUILD STOPPED (total time: 9 seconds)

Link 到 weetransfer 上传的项目: Project (scanned via my Bitdefender antivir)

I also chosen absolute layout

这就是你的问题。你没有正确使用它。

    getContentPane().add(label);

您不能在不指定适当约束的情况下将标签添加到框架中。我从未使用过 AbsoluteLayout(因为我相信适当的布局管理)但我猜你需要指定约束,如 x、y、宽度、高度。

布局管理器无法猜测您要将组件放置在何处,因此您需要指定所有信息。这就是您应该使用布局管理器的原因。然后布局管理器会根据布局管理器的规则定位组件。稍微练习一下就容易多了。