import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class ZhuCe extends JFrame implements ActionListener{
private JPanel jp=new JPanel();
private JCheckBox[] jcbArray={new JCheckBox("上网"),new JCheckBox("运动")};
private JRadioButton[] jrbArray={new JRadioButton("男"),new JRadioButton("女")};
private JButton[] jbArray={new JButton("提交"),new JButton("清空")};
private JLabel[] jlArray={new JLabel("性别"),new JLabel("爱好"),new JLabel("输入内容为:")};
private JTextField jtf=new JTextField();
private ButtonGroup bg=new ButtonGroup();
public ZhuCe(){
jp.setLayout(null);
for(int i=0;i<2;i++){
jrbArray[i].setBounds(40+i*100,40,80,30);
jcbArray[i].setBounds(40+i*120,100,120,30);
jp.add(jrbArray[i]);
jp.add(jcbArray[i]);
jrbArray[i].addActionListener(this);
jcbArray[i].addActionListener(this);
bg.add(jrbArray[i]);
if(i>1){
continue;
}
jlArray[i].setBounds(20,20+i*50,80,30);
jbArray[i].setBounds(400+i*120,200,80,30);
jp.add(jlArray[i]);
jp.add(jbArray[i]);
jbArray[i].addActionListener(this);
}
jlArray[2].setBounds(20,150,120,30);
jp.add(jlArray[2]);
jtf.setBounds(120,150,500,30);
jp.add(jtf);
jtf.setEditable(false);
this.add(jp);
this.setTitle("注册");
this.setBounds(100,100,700,280);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e){
if(e.getSource()==jbArray[1]){
bg.clearSelection();
for(int i=0;i<jcbArray.length;i++){
jcbArray[i].setSelected(false);
}
jtf.setText("");
}
else {
StringBuffer tmp1=new StringBuffer("你是一个");
StringBuffer tmp2=new StringBuffer();
for(int i=0;i<2;i++){
if(jrbArray[i].isSelected()){
tmp1.append(jrbArray[i].getText());
}
if(jcbArray[i].isSelected()){
tmp2.append(jcbArray[i].getText()+",");
}
}
if(tmp2.length()==0){
jtf.setText("兴趣爱好不能为空!!!");
}
else {
tmp1.append("生,你比较喜欢");
tmp1.append(tmp2.substring(0,tmp2.length()-1));
jtf.setText(tmp1.append("。").toString());
}
}
}
public static void main(String[] args){
ZhuCe zc=new ZhuCe();
}
}