问题描述
- Java的GroupLayout问题,设置组件的问题 30C
- 代码显示没有错误,但是运行的时候老是在控制台显示错误,错误提示如下《:Exception in thread ""AWT-EventQueue-0"" java.lang.IllegalStateException: javax.swing.JLabel[000x0invalidalignmentX=0.0alignmentY=0.0border=flags=8388608maximumSize=minimumSize=preferredSize=defaultIcon=disabledIcon=horizontalAlignment=LEADINGhorizontalTextPosition=TRAILINGiconTextGap=4labelFor=text=账号登录名:verticalAlignment=CENTERverticalTextPosition=CENTER] is not attached to a vertical group》
当然这个是我认为比较有依据的一句话
源代码如下:
public class AddAccount extends JPanel{/** * */private static final long serialVersionUID = 1L;//public static AddAccount addAccount; JLabel lblIdlblLoginNamelblPasswordlblRealNamelblType;//设置地址,登录名,登录密码,真实姓名, JLabel lblIdMesslblNameMesslblPassMesslblRealMess;// JTextField txtIdtxtNametxtRealName; JPasswordField txtPassword; JComboBox cboType; JButton btnOkbtnReset;public AddAccount(){ initComponent();}public void initComponent(){ this.setPreferredSize(new Dimension(6000));//设置初始大小 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(lblIdGroupLayout.PREFERRED_SIZE100GroupLayout.PREFERRED_SIZE) .addComponent(lblLoginNameGroupLayout.PREFERRED_SIZE100GroupLayout.PREFERRED_SIZE)//登录账号 .addComponent(lblPassword)//登录密码 .addComponent(lblRealName)//真实姓名 .addComponent(lblType) ); hgroup.addGroup(group.createParallelGroup(Alignment.CENTER)// .addComponent(txtIdGroupLayout.PREFERRED_SIZE100GroupLayout.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); }
解决方案
希望 各位帮帮忙,这种情况出现好几次了,我总是找不到解决的办法,拜托拜托
解决方案二:
group.setVerticalGroup(hgroup); 改成 group.setVerticalGroup(vgroup);
解决方案三:
Android中的RelativeLayout中组件的排放问题
时间: 2024-10-18 05:54:03