薯子
2016-05-05 13:54
采纳率: 100%
浏览 2.6k
已采纳

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);
     }
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

  • 薯子 2016-05-05 13:57
    最佳回答

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

    已采纳该答案
    评论
    解决 无用
    打赏 举报
查看更多回答(1条)

相关推荐 更多相似问题