2 qq 34814970 qq_34814970 于 2016.05.05 21:54 提问

Java的GroupLayout问题,设置组件的问题

代码显示没有错误,但是运行的时候老是在控制台显示错误,错误提示如下《: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=账号登录名:,verticalAlignment=CENTER,verticalTextPosition=CENTER] is not attached to a vertical group》
当然这个是我认为比较有依据的一句话
图片说明
源代码如下:
public class AddAccount extends JPanel{

/**
 * 
 */
private static final long serialVersionUID = 1L;

//public static AddAccount addAccount;
   JLabel lblId,lblLoginName,lblPassword,lblRealName,lblType;//设置地址,登录名,登录密码,真实姓名,
    JLabel lblIdMess,lblNameMess,lblPassMess,lblRealMess;//
    JTextField txtId,txtName,txtRealName;
    JPasswordField txtPassword;
    JComboBox cboType;
    JButton btnOk,btnReset;
public AddAccount(){
    initComponent();

}
public void initComponent(){
    this.setPreferredSize(new Dimension(600,0));//设置初始大小
    lblId=new JLabel("账号编号:");
    lblLoginName=new JLabel("账号登录名:");
    lblPassword=new JLabel(" 登录密码 :");
    lblRealName=new JLabel(" 真实姓名 :");
    lblType=new JLabel(" 账号类型 :");
    //设置JLabel
    txtId=new JTextField();
    txtName=new JTextField();
    txtRealName=new JTextField();
    //设置JTextField()方法
    txtPassword=new JPasswordField();

     String [] items =new String[]{"管理员","业务员","操作员"};//创建账号类型
     cboType = new JComboBox(items);
     btnOk=new JButton("确定");
     btnReset=new JButton("重置");
     lblIdMess=new JLabel("编号由系统生成,不用修改");
     lblNameMess=new JLabel("账号长度在5到20位之间");
     lblPassMess=new JLabel("密码长度必须在5到20位之间");
     lblRealMess=new JLabel("姓名长度必须在1到20位之间");
     //F/GroupLayout创建文本框的横竖位置
     GroupLayout group=new GroupLayout(this);

     GroupLayout.SequentialGroup hgroup=group.createSequentialGroup();
     hgroup.addGroup(group.createParallelGroup(Alignment.LEADING)
             .addComponent(lblId,GroupLayout.PREFERRED_SIZE,100,GroupLayout.PREFERRED_SIZE)
             .addComponent(lblLoginName,GroupLayout.PREFERRED_SIZE,100,GroupLayout.PREFERRED_SIZE)//登录账号
             .addComponent(lblPassword)//登录密码
             .addComponent(lblRealName)//真实姓名
             .addComponent(lblType)
             );
     hgroup.addGroup(group.createParallelGroup(Alignment.CENTER)//
             .addComponent(txtId,GroupLayout.PREFERRED_SIZE,100,GroupLayout.PREFERRED_SIZE)
             .addComponent(txtName)
             .addComponent(txtPassword)
             .addComponent(txtRealName)
             .addComponent(cboType)
             .addGroup(group.createParallelGroup())
             .addComponent(btnOk)
             .addComponent(btnReset)
             );
     hgroup.addGroup(group.createSequentialGroup()
             .addComponent(lblIdMess)
             .addComponent(lblNameMess)
             .addComponent(lblPassMess)
             .addComponent(lblRealMess));
     //
     GroupLayout.SequentialGroup vgroup=group.createSequentialGroup();

     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(lblId)
             .addComponent(txtId)
             .addComponent(lblIdMess)
             );
     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(lblLoginName)
             .addComponent(txtName)
             .addComponent(lblNameMess)
             );

     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(lblPassword)
             .addComponent(txtPassword)
             .addComponent(lblPassMess)
             );
     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(lblRealName)
             .addComponent(txtRealName)
            // .addComponent(lblRealMess)
             );
     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(lblType)
             .addComponent(cboType)
             );
     vgroup.addGroup(group.createParallelGroup(Alignment.CENTER)
             .addComponent(btnOk)
             .addComponent(btnReset)
             );
     group.setHorizontalGroup(hgroup);
     group.setVerticalGroup(hgroup);
     this.setLayout(group);
     }

3个回答

qq_34814970
qq_34814970   2016.05.05 21:57
已采纳

希望 各位帮帮忙,这种情况出现好几次了,我总是找不到解决的办法,拜托拜托

havedream_one
havedream_one   2016.05.06 10:10

group.setVerticalGroup(hgroup); 改成 group.setVerticalGroup(vgroup);

qq_34814970
qq_34814970 你好,虽然这个小错误是真实的不该出现,但是我改过来了,它还是错误的唉,,,,
一年多之前 回复
CSDNXIAON
CSDNXIAON   2016.05.06 15:42

Android中的RelativeLayout中组件的排放问题
----------------------同志你好,我是CSDN问答机器人小N,奉组织之命为你提供参考答案,编程尚未成功,同志仍需努力!

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!