/*ch0709.java*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class ch0709 extends JFrame implements ActionListener,ItemListener
{ JTextArea area=new JTextArea(6,30);
  String City[]={"北京","上海","天津","开封","宜昌"};
  JCheckBox cb[]=new JCheckBox[5];
  JRadioButton radio[]=new JRadioButton[5];
  ButtonGroup bg=new ButtonGroup();
  JPanel jp1=new JPanel();
  JPanel jp2=new JPanel();
  JPanel jp3=new JPanel();

  //创建JScrollPane组件并指定将文本区置入其显示区域中
  int v=ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS;
  int h=ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS;
  JScrollPane jsp=new JScrollPane(area,v,h);
  public ch0709()
  { super("复选框和单选按钮应用实例");
    Container c=getContentPane();
    c.setLayout(new BorderLayout());
    jp1.add(new Label("问卷调查"));
    jp1.add(jsp);
    jp2.add(new Label("您心目中的大城市有"));
    for(int i=0;i<5;i++)
    { cb[i]=new JCheckBox(City[i]);
      jp2.add(cb[i]);
      cb[i].addActionListener(this);
      cb[i].addItemListener(this);
    }
    jp3.add(new Label("人口最多的城市是"));
    for(int i=0;i<5;i++)
    { radio[i]=new JRadioButton(City[i],false);
      bg.add(radio[i]);
      jp3.add(radio[i]);
      radio[i].addActionListener(this);
      radio[i].addItemListener(this);   //对单选按钮加监听
    }
    c.add("North",jp1);c.add("Center",jp2);c.add("South",jp3);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setSize(300,200);
    setVisible(true);
  }

  public void actionPerformed(ActionEvent e)
  { area.append("  "+e.getActionCommand()+"\n");
  }

  public void itemStateChanged(ItemEvent e)
  { for(int i=0;i<5;i++)
    { if(e.getSource()==radio[i])
        area.append("你点击的是单选按钮： ");
      if(e.getSource()==cb[i])
        area.append("你点击的是复选按钮： ");
    }
  }

  public static void main(String args[])
  {   new ch0709();
  }
}
