菜鸟用swing做登陆界面遇到java.lang.IllegalStateException解决不了了,求大神指点啊...
import java.awt.*;import java.awt.event.*;
import javax.swing.*;
public class Myframe extends JFrame
{
public Myframe()
{
super("Find");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//String laf = UIManager.getCrossPlatformLookAndFeelClassName();
String laf = UIManager.getSystemLookAndFeelClassName();
try {
UIManager.setLookAndFeel(laf);
} catch (UnsupportedLookAndFeelException exc) {
System.err.println("Warning: UnsupportedLookAndFeel: " + laf);
} catch (Exception exc) {
System.err.println("Error loading " + laf + ": " + exc);
}
JLabel label1 = new JLabel("用户名");
JLabel label2 = new JLabel("密码");
JTextField textField1 = new JTextField();
JTextField textField2 = new JTextField();
JCheckBox teacherCheckBox = new JCheckBox("教师");
JCheckBox studentCheckBox = new JCheckBox("学生");
JButton dengluButton = new JButton("登录");
JButton cancelButton = new JButton("退出");
Container c = getContentPane();
GroupLayout layout = new GroupLayout(c);
c.setLayout(layout);
//自动设定组件、组之间的间隙
layout.setAutoCreateGaps(true);
layout.setAutoCreateContainerGaps(true);
//LEADING -- 左对齐 BASELINE -- 底部对齐 CENTER -- 中心对齐
GroupLayout.ParallelGroup hpg1 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg1.addComponent(label1);
hpg1.addComponent(label2);
GroupLayout.ParallelGroup hpg2 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg2.addComponent(textField1);
hpg2.addComponent(textField2);
GroupLayout.ParallelGroup hpg3 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg3.addComponent(teacherCheckBox);
hpg3.addComponent(dengluButton);
GroupLayout.ParallelGroup hpg4 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg4.addComponent(studentCheckBox);
hpg4.addComponent(cancelButton);
GroupLayout.SequentialGroup hpg5 = layout.createSequentialGroup();
hpg5.addGroup(hpg3).addGroup(hpg4);
GroupLayout.ParallelGroup hpg6 = layout.createParallelGroup(GroupLayout.Alignment.LEADING);
hpg6.addGroup(hpg2);
hpg6.addGroup(hpg5);
//水平
layout.setHorizontalGroup(layout.createSequentialGroup()
.addGroup(hpg1).addGroup(hpg6));
//设定两个Button在水平方向一样宽
layout.linkSize(SwingConstants.HORIZONTAL,new Component[] { dengluButton, cancelButton });
//layout.linkSize(SwingConstants.HORIZONTAL,new Component[] {teacherCheckBox,studentCheckBox });
GroupLayout.ParallelGroup vpg1 = layout.createParallelGroup(GroupLayout.Alignment.BASELINE);
vpg1.addGroup(hpg1);
vpg1.addGroup(hpg2);
//垂直
layout.setVerticalGroup(layout.createSequentialGroup()
.addGroup(vpg1).addGroup(hpg5));
setLocation(200,200);
pack();
setVisible(true);
}
public static void main(String[] args)
{
new Myframe();
}
}
错误提示:
Exception in thread "main" java.lang.IllegalStateException: javax.swing.JTextField[,0,0,0x0,invalid,layout=javax.swing.plaf.basic.BasicTextUI$UpdateHandler,alignmentX=0.0,alignmentY=0.0,border=com.sun.java.swing.plaf.windows.XPStyle$XPFillBorder@16ee3ec,flags=296,maximumSize=,minimumSize=,preferredSize=,caretColor=javax.swing.plaf.ColorUIResource[r=0,g=0,b=0],disabledTextColor=javax.swing.plaf.ColorUIResource[r=172,g=168,b=153],editable=true,margin=javax.swing.plaf.InsetsUIResource[top=2,left=2,bottom=2,right=2],selectedTextColor=javax.swing.plaf.ColorUIResource[r=255,g=255,b=255],selectionColor=javax.swing.plaf.ColorUIResource[r=49,g=106,b=197],columns=0,columnWidth=0,command=,horizontalAlignment=LEADING] is not attached to a vertical group
at javax.swing.GroupLayout.checkComponents(Unknown Source)
at javax.swing.GroupLayout.prepare(Unknown Source)
at javax.swing.GroupLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at javax.swing.JRootPane$RootLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at javax.swing.JComponent.getPreferredSize(Unknown Source)
at java.awt.BorderLayout.preferredLayoutSize(Unknown Source)
at java.awt.Container.preferredSize(Unknown Source)
at java.awt.Container.getPreferredSize(Unknown Source)
at java.awt.Window.pack(Unknown Source)
at Myframe.<init>(Myframe.java:79)
at Myframe.main(Myframe.java:85)