I know this have been asked in someway state or form and I looked at others, but I can't find why I am getting this error message. I'm not too well studied with GUIs and this is one of my first GUI projects I am working with. Error Code: Exception in thread
"AWT-EventQueue-0" java.lang.IllegalStateException: javax.swing.JLabel[,0,0,0x0,invalid,alignmentX=0.0,alignmentY=0.0,border=,flags=8388608,maximumSize=,minimumSize=,preferredSize=,defaultIcon=,disabledIcon=,horizontalAlignment=LEADING,horizontalTextPosition=TRAILING,iconTextGap=4,labelFor=,text=Label 1,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a horizontal group
at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.layoutContainer(Unknown Source)
at java.awt.Container.layout(Unknown Source)
at java.awt.Container.doLayout(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validateTree(Unknown Source)
at java.awt.Container.validate(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$500(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.awt.EventQueue$3.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.awt.EventQueue$4.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)
Code:
package GUI;
import java.awt.Component;
import javax.swing.*;
import static javax.swing.GroupLayout.Alignment.*;
import java.awt.*;
public class GUI extends JFrame {
public GUI() {
JButton b1 = new JButton("1"),b2=new JButton("2"),b3=new JButton("3"),b4=new JButton("4"),b5=new JButton("5"),b6=new JButton("6");
JLabel l1= new JLabel("Label 1"),l2= new JLabel("Label 2"),l3=new JLabel("Label 3"),l4=new JLabel("Label 4"),l5=new JLabel("Label 5"),l6=new JLabel("Label 6");
JPanel pane = new JPanel();
setTitle("Hello World");
setSize(500,500);
setVisible(true);
GroupLayout layout = new GroupLayout(pane);
pane.setLayout(layout);
getContentPane().add(pane);
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b1)
.addComponent(b2)
.addComponent(b3)
)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(l1)
.addComponent(l3)
.addComponent(l5)
)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(l2)
.addComponent(l4)
.addComponent(l6)
)
.addGroup(layout.createParallelGroup(LEADING)
.addComponent(b4)
.addComponent(b5)
.addComponent(b6)
));
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b1)
.addComponent(l1)
.addComponent(l2)
.addComponent(b4)
)
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b2)
.addComponent(l3)
.addComponent(l4)
.addComponent(b5)
)
.addGroup(layout.createParallelGroup(BASELINE)
.addComponent(b3)
.addComponent(l5)
.addComponent(l6)
.addComponent(b6)
)
);
}
}