package Demo;
import java.awt.*;
import java.awt.event.*;
public class TestChioce extends Frame implements ItemListener,ActionListener{
/**
* @param args
*/
TextField name;
CheckboxGroup cg;
Choice career;
List city;
Checkbox[] favorite;
Checkbox sex1;
Checkbox sex2;
Button submit;
Button reset;
public TestChioce(){
super("注册窗口");
Panel p=new Panel();
p.setLayout(new FlowLayout(FlowLayout.LEFT,1,1));
name=new TextField(10);
cg=new CheckboxGroup();
sex1=new Checkbox("男",cg,false);
sex2=new Checkbox("女",cg,false);
Panel sp=new Panel();
sp.add(sex1);
sp.add(sex2);
career=new Choice();
career.add("IT技术人员");
career.add("工商管理");
career.add("教育");
career.add("金融");
city =new List(4);
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(sp);
p.add(new Label("职业:"));
p.add(career);
p.add(new Label("城市:"));
p.add(city);
p.add(new Label("爱好:"));
String[]sf={"旅游","读书","时装","汽车","健美"};
favorite=new Checkbox[sf.length];
for(int i=0;i<sf.length;i++){
favorite[i]=new Checkbox(sf[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() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
}
@Override
public void itemStateChanged(ItemEvent e) {
// TODO Auto-generated method stub
String s=e.getItem().toString();
Checkbox obj=(Checkbox)e.getItemSelectable();
if(obj.getState()==true){
System.out.print("您刚选中了项目"+s);
}else{
System.out.print("您刚取消了项目"+s);
}
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
if(e.getActionCommand().equals("提交")){
String info="您提交的信息如下:\n"+name.getText()+"\n 性别"+cg.getSelectedCheckbox().getLabel()+
"\n 职介"+career.getSelectedIndex()+"\n 城市"+city.getSelectedIndex()+"\n 爱好";
for(int i=0;i<favorite.length;i++){
if(favorite[i].getState()==true)
info+=favorite[i].getLabel();
else
info+="";
}
System.out.print(info);
}else{
System.exit(0);
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
TestChioce f=new TestChioce();
f.setSize(160,270);
f.setLocation(300,200);
f.setBackground(Color.pink);
f.setResizable(false);
f.setVisible(true);
}
package Demo;
import java.awt.*;
import java.awt.event.*;
public class TestEvent extends Frame implements ActionListener{
/**
* @param args
*/
Button b1, b2;
Label lbl;
public TestEvent() {
super("匿名类");
lbl = new Label("你好");
b1 = new Button("确定");
b2 = new Button("取消");
// b1.setActionCommand("cofirm");
setLayout(new FlowLayout());
add(lbl);
add(b1);
add(b2);
//MyListener my=new MyListener();
// b1.addActionListener(my);
// b2.addActionListener(my);
b1.addActionListener(this);
b2.addActionListener(this);
this.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});
setBounds(200, 200, 300, 200);
setVisible(true);
}
public static void main(String[] args) {
// TODO Auto-generated method stub
new TestEvent();
}
// class MyListener implements ActionListener{
public void actionPerformed(ActionEvent e) {
/*
Button b=new (Button)e.getSource();//获取事件源
if(b==b1){
lbl.setText("确定");
}
else{
lbl.setText("取消");
}
*/
String str = e.getActionCommand();// 获取动作命令,默认是按钮上的标签
if (e.equals("确定")) {
lbl.setText("确定");
} else {
lbl.setText("取消");
}
}
// }
}
}
}