关于单选框和复选框的使用

首先我们要学习的是关于单选框的知识

最重要的就是将单选框加入到group中;

public class JButtonDemo extends JFrame {
    public JButtonDemo(){
        Container container = this.getContentPane();
        JRadioButton button1 = new JRadioButton("b1");
        JRadioButton button2 = new JRadioButton("b2");
        JRadioButton button3 = new JRadioButton("b3");
        ButtonGroup buttonGroup = new ButtonGroup();//设置一个按钮的组,把所有的单选按钮都归到这个组合里面(不然无法实现单选的功能);
        buttonGroup.add(button1);
        buttonGroup.add(button2);
        buttonGroup.add(button3);

        container.add(button1,BorderLayout.CENTER);
        container.add(button2,BorderLayout.NORTH);
        container.add(button3,BorderLayout.SOUTH);
        this.setVisible(true);
        this.setBounds(100,100,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

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

然后我们学习的是复选框,没什么难度,主要是去记名字;


public class JButtonDemo extends JFrame {
    public JButtonDemo(){
        Container container = this.getContentPane();

        JCheckBox box1 = new JCheckBox("1");
        JCheckBox box2 = new JCheckBox("2");
        JCheckBox box3 = new JCheckBox("3");
        container.add(box1,BorderLayout.SOUTH);
        container.add(box2,BorderLayout.NORTH);
        container.add(box3,BorderLayout.CENTER);
        this.setVisible(true);
        this.setBounds(100,100,600,400);
        this.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);

    }

    public static void main(String[] args) {
        new JButtonDemo();
    }
}
posted on 2022-04-06 20:54  damango  阅读(52)  评论(0编辑  收藏  举报