When I try to add anything to JFrame in constructor and I have IllegalArgumentException.
Similar code is working in documentation: https://docs.oracle.com/javase/tutorial/uiswing/layout/none.html
Why my very simple code doesn't work? It is created in Netbeans 10.
Edit: I also tried add label with position and sieze (setBounds) it doesn't help. I changed the code with setBounds method.
First, main class in JavaTestApp.java:
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();
}
}
Second file is:
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, regarding to @VGR request:
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 to project uploaded on weetransfer: Project (scanned via my Bitdefender antivir)