注册窗口
package com.hbsi;
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TestChoice extends Frame implements ItemListener,ActionListener
{
TextField name;
CheckboxGroup g;
Panel panel ;
Choice career;
List city;
Checkbox[] favorite;
Checkbox sex1;
Checkbox sex2;
Button submit;
Button reset;
public TestChoice()
{
super("注册窗口");
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
name = new TextField(10);
g = new CheckboxGroup();
sex1 = new Checkbox("男",g,false);
sex2 = new Checkbox("女",g,false);
panel = new Panel();
panel.add(sex1);
panel.add(sex2);
career = new Choice();
career.add("IT技术人员");
career.add("工商管理");
career.add("教育");
career.add("金融");
city = new List(4,false);
city.add("北京");
city.add("上海");
city.add("天津");
city.add("广州");
city.add("太原");
city.add("石家庄");
city.add("哈尔宾");
city.add("三亚");
city.add("威海");
p.add(new Label("姓名"));
p.add(name);
p.add(new Label("性别"));
p.add(panel);
p.add(new Label("职业"));
p.add(career);
p.add(new Label("城市"));
p.add(city);
p.add(new Label("爱好:"));
String[] fav = {"旅游","读书","时装","汽车","健美"};
favorite = new Checkbox[fav.length];
for(int i=0; i<fav.length;i++)
{
favorite[i] = new Checkbox(fav[i]);
favorite[i].addItemListener(this);
p.add(favorite[i]);
}
add(p,BorderLayout.CENTER);
Panel psouth = new Panel();
psouth.setLayout(new GridLayout(1,2));
submit = new Button("提交");
reset = new Button("退出");
submit.addActionListener(this);
reset.addActionListener(this);
psouth.add(submit);
psouth.add(reset);
add(psouth,"South");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e)
{
String s =e.getItem().toString();
Checkbox obj = (Checkbox)e.getItemSelectable();
if(obj.getState()==true)
{
System.out.println("您刚选中了项目:" + s);
}
else
{
System.out.println("您刚才取消了项目:" +s);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("提交"))
{
String info = "您提交的信息如下:\n 姓名:" + name.getText() +
"\n 性别:" + g.getSelectedCheckbox().getLabel() +
"\n 职业:" + career.getSelectedItem() +
"\n 城市:" + city.getSelectedItem() +
"\n 爱好:";
for(int i=0; i<favorite.length; i++)
{
if(favorite[i].getState()==true)
info+=favorite[i].getLabel();
else
info+="";
}
System.out.println(info);
}
else
{
System.exit(0);
}
}
public static void main(String args[])
{
TestChoice obj=new TestChoice();
obj.setSize(170,300);
obj.setLocation(200,200);
obj.setVisible(true);
}
}
import java.awt.*;
import java.awt.event.*;
import javax.swing.JOptionPane;
public class TestChoice extends Frame implements ItemListener,ActionListener
{
TextField name;
CheckboxGroup g;
Panel panel ;
Choice career;
List city;
Checkbox[] favorite;
Checkbox sex1;
Checkbox sex2;
Button submit;
Button reset;
public TestChoice()
{
super("注册窗口");
Panel p = new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
name = new TextField(10);
g = new CheckboxGroup();
sex1 = new Checkbox("男",g,false);
sex2 = new Checkbox("女",g,false);
panel = new Panel();
panel.add(sex1);
panel.add(sex2);
career = new Choice();
career.add("IT技术人员");
career.add("工商管理");
career.add("教育");
career.add("金融");
city = new List(4,false);
city.add("北京");
city.add("上海");
city.add("天津");
city.add("广州");
city.add("太原");
city.add("石家庄");
city.add("哈尔宾");
city.add("三亚");
city.add("威海");
p.add(new Label("姓名"));
p.add(name);
p.add(new Label("性别"));
p.add(panel);
p.add(new Label("职业"));
p.add(career);
p.add(new Label("城市"));
p.add(city);
p.add(new Label("爱好:"));
String[] fav = {"旅游","读书","时装","汽车","健美"};
favorite = new Checkbox[fav.length];
for(int i=0; i<fav.length;i++)
{
favorite[i] = new Checkbox(fav[i]);
favorite[i].addItemListener(this);
p.add(favorite[i]);
}
add(p,BorderLayout.CENTER);
Panel psouth = new Panel();
psouth.setLayout(new GridLayout(1,2));
submit = new Button("提交");
reset = new Button("退出");
submit.addActionListener(this);
reset.addActionListener(this);
psouth.add(submit);
psouth.add(reset);
add(psouth,"South");
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(0);
}
});
}
public void itemStateChanged(ItemEvent e)
{
String s =e.getItem().toString();
Checkbox obj = (Checkbox)e.getItemSelectable();
if(obj.getState()==true)
{
System.out.println("您刚选中了项目:" + s);
}
else
{
System.out.println("您刚才取消了项目:" +s);
}
}
public void actionPerformed(ActionEvent e)
{
if(e.getActionCommand().equals("提交"))
{
String info = "您提交的信息如下:\n 姓名:" + name.getText() +
"\n 性别:" + g.getSelectedCheckbox().getLabel() +
"\n 职业:" + career.getSelectedItem() +
"\n 城市:" + city.getSelectedItem() +
"\n 爱好:";
for(int i=0; i<favorite.length; i++)
{
if(favorite[i].getState()==true)
info+=favorite[i].getLabel();
else
info+="";
}
System.out.println(info);
}
else
{
System.exit(0);
}
}
public static void main(String args[])
{
TestChoice obj=new TestChoice();
obj.setSize(170,300);
obj.setLocation(200,200);
obj.setVisible(true);
}
}