将选择框Choice,列表框List等相关信息读取到文本框TextArea中

编写一个Application程序输入学生的有关信息,用Checkbox表示学生是否注册,用CheckboxGroup表示学生的性别,用List表示学生的年级,用Choice表示学生的系别。程序还包括一个按钮,用户单击按钮时,程序读取当前组件中的选择并显示在一个TextArea中。


public class ApplicationTest extends Frame implements ActionListener {

    Frame f=new Frame("学生的相关信息"); 
    Panel pn,pc,p1,p2,p3;
    Label lb1,lb2,lb3;
    Choice department;
    List grade;
    TextArea ta; 
    Button ok;
    CheckboxGroup cbg;
    Checkbox male,female;  //单选按钮,男或女
    Checkbox digister;     //复选框,是否注册
    
   public ApplicationTest() {
    // TODO Auto-generated constructor stub
            // TODO Auto-generated method stub
            pn=new Panel();
            pc=new Panel();
            p1=new Panel();
            p2=new Panel();
            p3=new Panel();
            lb1=new Label("系别:");
            department =new Choice();
            department.addItem("信息系");
            department.addItem("工业系");
            department.addItem("外语系");
                       
            lb2=new Label("年级:");
            grade=new List(3,false);
            grade.add("大一");
            grade.add("大二");
            grade.add("大三");
            grade.add("大四");
            
            lb3=new Label("性别:");
            cbg=new CheckboxGroup();
            male=new Checkbox("男", cbg,false);
            female=new Checkbox("女", cbg,false);
            digister =new Checkbox("注册", false);
            
            ok=new Button("确定");
            ok.addActionListener(this);
            ta=new TextArea();
            
            f.setLayout(new BorderLayout());
            f.add("North", pn);
            FlowLayout flll=new FlowLayout();
            p1.setLayout(flll);
            p2.setLayout(flll);
            p3.setLayout(flll);
            pc.setLayout(flll);
            pn.setLayout(flll);
            p1.add(lb1);
            p1.add(department);
            p2.add(lb2);
            p2.add(grade);
            p3.add(lb3);
            p3.add(male);
            p3.add(female);
            pn.add(p1);pn.add(p2);pn.add(p3);
            f.add("Center", pc);
            pc.add(digister);pc.add(ok);
            f.add("South", ta);
            f.setSize(600, 400); f.setVisible(true);        
      
           
}    
    public static void main(String args[]) {
       new ApplicationTest();
    
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
    //实现当点击确定按钮时,把选择框,列表框,复选框组的文本信息添加到文本域            
        if(e.getSource()==ok) {
        if(digister.getState()==true) {    
            ta.setText("这是一名"+department.getSelectedItem());
            ta.append(grade.getSelectedItem());
            if(male.getState()==true) 
                { ta.append("性别为"+male.getLabel()+"性的学生");}
            else ta.append("性别为"+female.getLabel()+"性的学生");
        }
       }
    }

}


猜你喜欢

转载自blog.csdn.net/xxx_1_1/article/details/81987743