/**
* @(#)TestCheckbox.java
*
* TestCheckbox application
*
* @author
* @version 1.00 2007/1/20
*/
import java.awt.*;
import java.awt.event.*;
public class TestCheckbox extends Frame
{
Checkbox cb1=new Checkbox("Are you a boy or gril?",true);
CheckboxGroup cbg=new CheckboxGroup();
Checkbox cb2=new Checkbox("Boy",true,cbg);
Checkbox cb3=new Checkbox("Girl",false,cbg);
class CbItemListener implements ItemListener
{
public void itemStateChanged(ItemEvent e)
{
Checkbox cb=(Checkbox)e.getItemSelectable();
/*if(cb.getLabel().equals("Are you a boy or gril?"))
{
....
}*/
if(cb==cb1)
{
if(cb.getState()==true)
{
System.out.println("你选择了复选框");
}
else
{
System.out.println("你取消选择复选框");
}
}
else if(cb==cb2)
{
if(cb.getState()==true)
{
System.out.println("你是个男孩");
}
else
{
System.out.println("你是个女孩");
}
}
else
{
if(cb.getState()==true)
{
System.out.println("你是个女孩");
}
else
{
System.out.println("你是个男孩");
}
}
}
}
public TestCheckbox()
{
FlowLayout fl=new FlowLayout();
setLayout(fl);
add(cb1);
add(cb2);
add(cb3);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
e.getWindow().dispose();
}
});
CbItemListener cbit=new CbItemListener();
cb1.addItemListener(cbit);
cb2.addItemListener(cbit);
cb3.addItemListener(cbit);
}
public static void main(String[] args)
{
// TODO, add your application code
System.out.println("Hello World!");
TestCheckbox mainFrame=new TestCheckbox();
mainFrame.setTitle("TestCheckbox");
mainFrame.setBounds(300,200,400,400);
mainFrame.setVisible(true);
}
}