AWT用户界面设计——注册框设计

来源:互联网 发布:肾结石 知乎 编辑:程序博客网 时间:2024/06/16 08:35
import java.awt.*;import java.awt.event.*;public class LoginDemo extends Frame implements ActionListener,ItemListener {Label lYourInform,lLogin,lKey;TextField textLogin,textKey;Label lName,lSex,lHobby;TextField textname;Checkbox box1,box2;CheckboxGroup boxgroup;Checkbox check1,check2,check3,check4;Choice ychoice,mchoice,dchoice;Label lBirthday;TextArea areaInfo;Button bEnter,bCancel;String Sex = "",Year="",Month="",Day = "";String Movie = "",Program="",Internet="",Chasegirl="";String strKey;boolean flag = true;public LoginDemo(){lYourInform = new Label("请输入您的个人信息,单击确认");add(lYourInform,BorderLayout.NORTH);//中心面板 panel默认的是FlowLayout布局排列Panel centerPanel = new Panel();Panel panel1 = new Panel();lLogin = new Label("帐号:");panel1.add(lLogin);textLogin = new TextField("",6);panel1.add(textLogin);lKey = new Label("密码:");panel1.add(lKey);textKey = new TextField("",6);textKey.setEchoChar('*');//设置反显字符,保护密码panel1.add(textKey);centerPanel.add(panel1);Panel panel2 = new Panel();lName = new Label("姓名:");textname = new TextField("",6);lSex = new Label("性别:");boxgroup = new CheckboxGroup();box1 = new Checkbox("男",true,boxgroup);box2 = new Checkbox("女",false,boxgroup);panel2.add(lName);panel2.add(textname);panel2.add(lSex);panel2.add(box1);panel2.add(box2);centerPanel.add(panel2);Panel panel3 = new Panel();lBirthday = new Label("生日:");ychoice = new Choice();ychoice.add("1987年");ychoice.add("1988年");ychoice.add("1989年");ychoice.add("1990年");mchoice = new Choice();mchoice.add("1月");mchoice.add("2月");mchoice.add("3月");mchoice.add("4月");mchoice.add("5月");mchoice.add("6月");mchoice.add("7月");mchoice.add("8月");mchoice.add("9月");mchoice.add("10月");mchoice.add("11月");mchoice.add("12月");dchoice = new Choice();dchoice.add("28日");dchoice.add("29日");dchoice.add("30日");dchoice.add("31日");ychoice.addItemListener(this);mchoice.addItemListener(this);dchoice.addItemListener(this);panel3.add(lBirthday);panel3.add(ychoice);panel3.add(mchoice);panel3.add(dchoice);centerPanel.add(panel3);Panel panel4=new Panel();panel4.setLayout(new GridLayout(1,5));lHobby = new Label("爱好:");    check1 = new Checkbox("电影");    check2 = new Checkbox("阅读");    check3 = new Checkbox("编程");    check4 = new Checkbox("旅游");        check1.addItemListener(this);    check2.addItemListener(this);    check3.addItemListener(this);    check4.addItemListener(this);    panel4.add(lHobby);    panel4.add(check1);    panel4.add(check2);    panel4.add(check3);    panel4.add(check4);    centerPanel.add(panel4);        areaInfo = new TextArea("",4,40);    centerPanel.add(areaInfo);        add(centerPanel,BorderLayout.CENTER);        Panel bPanel = new Panel();    bEnter = new Button("Enter");    bEnter.addActionListener(this);    bCancel = new Button("Cancel");    bCancel.addActionListener(this);        bPanel.add(bEnter);    bPanel.add(bCancel);    add(bPanel,BorderLayout.SOUTH);      //  this.pack();    setSize(350,350);    setVisible(true);        addWindowListener(new WindowAdapter(){    //实现关闭窗口  (实现窗口右上角的叉叉)public void windowClosing(WindowEvent e){System.exit(0);}});    }public void actionPerformed(ActionEvent ae){   //实现ActionListener监视器接口的方法actionPerformed(ActionEvent)areaInfo.setText("");if(ae.getSource() == bEnter){if(flag == true ){flag = false;areaInfo.append("Name"+textname.getText()+"\n");areaInfo.append("Gender:"+Sex +"\n");areaInfo.append("Birthady"+Year+Month+Day+"\n");areaInfo.append("Hobby:"+ Movie +Internet+Program+Chasegirl+"\n");lYourInform.setText("enter your password again,then press Enter botton");strKey=textKey.getText();  //取得第一次输入的密码保存在strKey中textKey.setText("");//然后清空密码输入框,等待第2次输入}else {if(textKey.getText().equals(strKey)){lYourInform.setText("Rigister success!~");}else{lYourInform.setText("enter your password again,then press Enter botton");}}}else{System.exit(0);}}public void itemStateChanged(ItemEvent ie){  //实现ItemListener监视器接口的方法itemStateChanged(ItemEvent)if(ie.getItemSelectable() == ychoice){Year = ychoice.getSelectedItem();}if(ie.getItemSelectable() == mchoice ){Month = mchoice.getSelectedItem();}if(ie.getItemSelectable() == dchoice){Day = dchoice.getSelectedItem();}if(box1.getState()) Sex = box1.getLabel();if(box2.getState()) Sex = box2.getLabel();if(check1.getState()) Movie = check1.getLabel();else Movie = "";if(check2.getState()) Program = check2.getLabel();else Program = "";if(check3.getState()) Internet = check3.getLabel();else Internet = "";if(check4.getState()) Chasegirl= check4.getLabel();else Chasegirl = "";}public static void main(String[] args) {new LoginDemo6();}}

参考书籍:《Java软件编程实例教程》  孙燮华