import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class PersonalInformation extends JFrame {
JPanel Panel ;
JLabel lable1,lable2;
JCheckBox checkBox1,checkBox2,checkBox3;
ButtonGroup buttongroup;
JRadioButton radiobutton1,radiobutton2;
JScrollPane scrollpane;
JTextArea textarea;
public PersonalInformation(){
super("PersonalInformation");
Listener1 listener = new Listener1();
Box boxH1 = Box.createHorizontalBox();//创建一个大盒子,里面有小的盒子
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(Box.createHorizontalStrut(50));
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(Box.createHorizontalStrut(5));
boxH1.add(lable1);
boxH1.add(checkBox1);
boxH1.add(checkBox2);
boxH1.add(checkBox3);
lable1=new JLabel("兴趣");
checkBox1 =new JCheckBox("羽毛球");
checkBox1.addItemListener(listener);
checkBox2 =new JCheckBox("兵乓球");
checkBox2.addItemListener(listener);
checkBox3 =new JCheckBox("唱歌");
checkBox3.addItemListener(listener);
Listener2 l2 = new Listener2();
buttongroup=new ButtonGroup();
Box boxH2 = Box.createHorizontalBox();
boxH2.add(Box.createHorizontalStrut(5));
boxH2.add(lable2);
boxH2.add(Box.createHorizontalStrut(50));
boxH2.add(radiobutton1);
boxH2.add(Box.createHorizontalStrut(5));
boxH2.add(radiobutton2);
radiobutton1.addActionListener(l2);
radiobutton2.addActionListener(l2);
buttongroup.add(radiobutton1);
buttongroup.add(radiobutton2);
lable2=new JLabel("性别");
radiobutton1=new JRadioButton("男");
radiobutton2=new JRadioButton("女");
JPanel panel=new JPanel ();
Panel.setLayout(new GridLayout(2,1));
Panel.add(boxH1);
Panel.add(boxH2);
textarea = new JTextArea(200, 300);
scrollpane= new JScrollPane(textarea);
Container contentPane = getContentPane();
contentPane.add(panel, BorderLayout.NORTH);
contentPane.add(scrollpane, BorderLayout.CENTER);
setSize(400,300);
setVisible(true);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new PersonalInformation();
}
//步骤2
class Listener1 implements ItemListener{
public void itemStateChanged(ItemEvent e){
if(e.getSource()==checkBox1)
textarea.append(checkBox1.getText()+"\n");
else if(e.getSource()==checkBox2)
textarea.append(checkBox2.getText()+"\n");
else if(e.getSource()==checkBox3)
textarea.append(checkBox3.getText()+"\n");
}
}
class Listener2 implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==radiobutton1)
textarea.append(radiobutton1.getText()+"\n");
else if(e.getSource()==radiobutton2)
textarea.append(radiobutton2.getText()+"\n");
}
}
}