博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

jcheckbox 用例

Posted on 2012-08-07 18:18  紫冰龙  阅读(151)  评论(0编辑  收藏  举报
import javax.swing.*;

import java.awt.FlowLayout;
import java.awt.event.*;

public class Exec61 extends JFrame implements ActionListener{
    JTextField text;
    JCheckBox jc1,jc2,jc3;
    Exec61(){
        setTitle("Test");
        setSize(400,500);
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        this.setLayout(new FlowLayout());
        jc1 = new JCheckBox("足球");
        ButtonGroup g = new ButtonGroup();
        
        jc2 = new JCheckBox("篮球");
        jc3 = new JCheckBox("排球");
        jc1.addActionListener(this);
        jc2.addActionListener(this);
        jc3.addActionListener(this);
        g.add(jc1);
        g.add(jc2);
        g.add(jc3);
                
        text = new JTextField(10);
        
        add(jc1);
        add(jc2);
        add(jc3);
        add(text);
        
        pack();
        setVisible(true);
        
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        JCheckBox jc = (JCheckBox)(e.getSource());
        text.setText(jc.getActionCommand());
    }
    public static void main(String[] args) {
        new Exec61();
    }
}