JavaSE系列代码41:设置复选框和单选按钮组

Method rewriting
Subclasses can also hide methods. Subclasses hide inherited methods through method rewriting. Method rewrite means that a method is defined in a subclass, and its name, return type, number of parameters and type are exactly the same as those inherited from the parent class. Subclass can change the state and behavior of the parent class to its own state and behavior by overriding the method.

import java.awt.*; 
public class Javase_41 extends Frame
{
  static Frame frm=new Frame("复选框和单选按钮组选取框");
  static Checkbox chk1=new Checkbox("租体",true);  //设置chk1为选中状态
  static Checkbox chk2=new Checkbox("斜体");
  static Checkbox chk3=new Checkbox("下划线");
  static Checkbox chk_g1=new Checkbox("红色");
  static Checkbox chk_g2=new Checkbox("绿色",true);  //设置chk_g2为选中状态
  static Checkbox chk_g3=new Checkbox("蓝色");
  public static void main(String[] args)
  {
    CheckboxGroup grp=new CheckboxGroup();  //创建一个单选按钮组对象
    frm.setLocation(200,150);
    frm.setSize(250,220);
    frm.setLayout(null);           //取消页面设置
    chk1.setBounds(20,30,120,20);  //设置复选框的位置和大小
    chk2.setBounds(20,50,120,20);
    chk3.setBounds(20,70,120,20);
    chk_g1.setBounds(20,90,120,20);
    chk_g2.setBounds(20,110,120,20);
    chk_g3.setBounds(20,130,120,20);
    chk_g1.setCheckboxGroup(grp);   //将chk_g1加入grp组中
    chk_g2.setCheckboxGroup(grp);
    chk_g3.setCheckboxGroup(grp);
    frm.add(chk1);
    frm.add(chk2);
    frm.add(chk3);
    frm.add(chk_g1);
    frm.add(chk_g2);
    frm.add(chk_g3);
    frm.setVisible(true);
  }
}
发布了52 篇原创文章 · 获赞 162 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/blog_programb/article/details/105394220