用swing控件写的小程序,做个记录吧,,,


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();
    }
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值