zoukankan      html  css  js  c++  java
  • JAVA 单选按钮、复选按钮

    //单选按钮和复选按钮
    import java.awt.*;
    import javax.swing.*;
    public class Jiemian6 extends JFrame{
        
        JPanel mb1,mb2,mb3;    //面板定义
        JButton an1,an2;    //按钮定义
        JLabel bq1,bq2;        //标签定义
        JCheckBox fxk1,fxk2,fxk3;        //多选框定义
        JRadioButton dx1,dx2;    //单选按钮定义
        ButtonGroup dxz;    //把单选按钮放进一个组里,默认功能是只能选一个,相当于html里面的 name
        
        public static void main(String[] args){
            //运行本类的构造方法
            Jiemian6 jiemian    =    new Jiemian6();
            
        }
        
        public Jiemian6(){
            
            //创建面板
            mb1        =    new JPanel();
            mb2        =    new JPanel();
            mb3        =    new JPanel();
            
            //创建按钮
            bq1    =    new JLabel("特长:");
            bq2    =    new JLabel("性别:");
            an1    =    new JButton("注册");
            an2    =    new JButton("取消");
            fxk1    =    new JCheckBox("音乐");
            fxk2    =    new JCheckBox("体育");
            fxk3    =    new JCheckBox("文艺");
            dx1        =    new JRadioButton("男");
            dx2        =    new JRadioButton("女");
            
            //使用网格布局
            this.setLayout(new GridLayout(3,1));
            
            //添加进组使其只能选择一个
            dxz    =    new ButtonGroup();
            dxz.add(dx1);
            dxz.add(dx2);
            
            //向面板添加按钮
            mb1.add(bq1);
            mb1.add(fxk1);
            mb1.add(fxk2);
            mb1.add(fxk3);
            mb2.add(bq2);
            mb2.add(dx1);
            mb2.add(dx2);
            mb3.add(an1);
            mb3.add(an2);
                
            //向界面添加面板
            this.add(mb1);
            this.add(mb2);
            this.add(mb3);
            
            //设置窗口标题
            this.setTitle("布局综合应用");
            //设置窗口的宽高
            this.setSize(300,150);
            //设置窗口出现对于屏幕的位置
            this.setLocation(100,100);
            //禁止拉大拉小
            this.setResizable(false);
            //关闭窗口后释放资源
            this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            //输出窗口
            this.setVisible(true);
        }
        
    }
  • 相关阅读:
    Educational Codeforces Round 86 (Rated for Div. 2) D. Multiple Testcases
    Educational Codeforces Round 86 (Rated for Div. 2) C. Yet Another Counting Problem
    HDU
    HDU
    HDU
    HDU
    Good Bye 2019 C. Make Good (异或的使用)
    Educational Codeforces Round 78 (Rated for Div. 2) C. Berry Jam
    codeforces 909C. Python Indentation
    codeforces1054 C. Candies Distribution
  • 原文地址:https://www.cnblogs.com/phpyangbo/p/5158983.html
Copyright © 2011-2022 走看看