zoukankan      html  css  js  c++  java
  • Java知多少(87)选择框和单选按钮(转)

    选择框、单选框和单选按钮都是选择组件,选择组件有两种状态,一种是选中(on),另一种是未选中(off),它们提供一种简单的 “on/off”选择功能,让用户在一组选择项目中作选择。

    选择框

    选择框(JCheckBox)的选中与否开状是一个小方框,被选中则在框中打勾。当在一个容器中有多个选择框,同时可以有多个选择框被选中,这样的选择框也称复选框。与选择框相关的接口是ItemListener,事件类是ItemEvent。
    JCheckBox类常用的构造方法有以下3个:

    1. JCheckBox():用空标题构造选择框。
    2. JCheckBox(String s):用给定的标题s构造选择框。
    3. JCheckBox(String s, boolean b):用给定的标题s构造选择框,参数b设置选中与否的初始状态。

    JCheckBox类的其他常用方法如下:

    1. getState():获取选择框的状态。
    2. setState(boolean b):设置选择框的状态
    3. getLabel():获取选择框的标题。
    4. setLabel(String s):设置选择框的标题。
    5. isSelected():获取选择框是否被选中的状态。
    6. itemStateChanged(ItemEvent e):处理选择框事件的接口方法。
    7. getItemSelectable():获取可选项,获取事件源。
    8. addItemListener(ItemListener l):为选择框设定监视器。
    9. removeItemListener(ItemListener l):移去选择框的监视器。

    【例 11-11】声明一个面板子类,面板子类对象有3个选择框。

    复制代码
    class Panel1 extends JPanel{
        JCheckBox box1,box2,box3;
        Panel1(){
            box1 = new JCheckBox(“足球”);
            box2 = new JCheckBox(“排球”);
            box2 = new JCheckBox(“篮球”);
        }
    }
    复制代码

    单选框

    当在一个容器中放入多个选择框,且没有ButtonGroup对象将它们分组,则可以同时选中多个选择框。如果使用ButtonGroup对象将选择框分组,同一时刻组内的多个选择框只允许有一个被选中,称同一组内的选择框为单选框。单选框分组的方法是先创建ButtonGroup对象,然后将希望为同组的选择框添加到同一个ButtonGroup对象中。参见例6.2程序的面板子类Panel2的声明,组内有3个单选框。

    单选按钮

    单选按钮(JRadioButton)的功能与单选框相似。使用单选按钮的方法是将一些单选按钮用ButtonGroup对象分组,使同一组的单选按钮只允许有一个被选中。单选按钮与单选框的差异是显示的样式不同,单选按钮是一个圆形的按钮,单选框是一个小方框。
    JRadioButton类的常用构造方法有以下几个:

    1. JRadioButton():用空标题构造单选按钮。
    2. JRadioButton(String s):用给定的标题s构造单选按钮。
    3. JRadioButton(String s,boolean b):用给定的标题s构造单选按钮,参数b设置选中与否的初始状态。

    单选按钮使用时需要使用ButtonGroup将单选按钮分组,单选按钮的分组方法是先创建对象,然后将同组的单选按钮添加到同一个ButtonGroup对象中。参见例6.2程序的子类panel1的声明,组内有3个单选按钮。

    选择项目事件处理

    用户对选择框或单选按钮做出选择后,程序应对这个选择作出必要的响应,程序为此要处理选择项目事件。选择项目处理程序的基本内容有:

    1. 监视选择项目对象的类要实现接口ItemListener,
    2. 程序要声明和建立选择对象,
    3. 为选择对象注册监视器,
    4. 编写处理选择项目事件的接口方法itemStateChanged(ItemEvent e),在该方法内用getItemSelectable()方法获取事件源,并作相应处理。

    【例 11-12】处理选择项目事件的小应用程序。一个由3个单选按钮组成的产品选择组,当选中某个产品时,文本区将显示该产品的信息。一个由3个选择框组成的购买产品数量选择框组,当选择了购买数量后,在另一个文本框显示每台价格。

    复制代码
     1 import java.applet.*;
     2 import javax.swing.*;
     3 import java.awt.*;
     4 import java.awt.event.*;
     5 class Panel1 extends JPanel{
     6     JRadioButton box1,box2,box3;
     7     ButtonGroup g;
     8     Panel1(){
     9         setLayout(new GridLayout(1,3));
    10         g = new ButtonGroup();
    11         box1 = new JRadioButton(MyWindow.fName[0]+"计算机",false);
    12         box2 = new JRadioButton(MyWindow.fName[1]+"计算机",false);
    13         box3 = new JRadioButton(MyWindow.fName[2]+"计算机",false);
    14         g.add(box1);g.add(box2);g.add(box3);
    15         add(box1);add(box2);add(box3);
    16         add(new JLabel("计算机3选1") );
    17     }
    18 }
    19 class Panel2 extends JPanel{
    20     JCheckBox box1,box2,box3;
    21     ButtonGroup g;
    22     Panel2(){
    23         setLayout(new GridLayout(1,3));
    24         g = new ButtonGroup();
    25         box1 = new JCheckBox("购买1台 ");
    26         box2 = new JCheckBox("购买2台 ");
    27         box3 = new JCheckBox("购买3台 ");
    28         g.add(box1);g.add(box2);g.add(box3);
    29         add(box1);add(box2);add(box3);
    30         add(new JLabel(" 选择1、2或3"));
    31     }
    32 }
    33 class MyWindow extends JFrame implements ItemListener{
    34     Panel1 panel1;
    35     Panel2 panel2;
    36     JLabel label1,label2;
    37     JTextArea text1,text2;
    38     static String fName[] = {"HP","IBM","DELL"};
    39     static double priTbl[][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}};
    40     static int productin = -1;
    41     MyWindow(String s){
    42         super(s);
    43         Container con = this.getContentPane();
    44         con.setLayout(new GridLayout(3,2));
    45         this.setLocation(100,100);
    46         this.setSize(400,100);
    47         panel1 = new Panel1();panel2 = new Panel2();
    48         label1 = new JLabel("产品介绍",JLabel.CENTER);
    49         label2 = new JLabel("产品价格",JLabel.CENTER);
    50         text1 = new JTextArea();text2 = new JTextArea();
    51         con.add(label1);con.add(label2);con.add(panel1);
    52         con.add(panel2);con.add(text1);con.add(text2);
    53         panel1.box1.addItemListener(this);
    54         panel1.box2.addItemListener(this);
    55         panel1.box3.addItemListener(this);
    56         panel2.box1.addItemListener(this);
    57         panel2.box2.addItemListener(this);
    58         panel2.box3.addItemListener(this);
    59         this.setVisible(true);this.pack();
    60     }
    61     public void itemStateChanged(ItemEvent e){  //选项状态已改变
    62         if(e.getItemSelectable()==panel1.box1){  //获取可选项
    63             production =0;
    64             text1.setText(fName[0]+"公司生产");text2.setText("");
    65         }
    66         else if(e.getItemSelectable()==panel1.box2){
    67             production =1;
    68             text1.setText(fName[1]+"公司生产");text2.setText("");
    69         }
    70         else if(e.getItemSelectable()==panel1.box3){
    71             production =2;
    72             text1.setText(fName[2]+"公司生产");text2.setText("");
    73         }
    74         else{
    75             if(production ==-1) return;
    76             if(e.getItemSelectable()==panel2.box1){
    77                 text2.setText(""+priTbl[production][0]+"万元/台");
    78             }
    79             else if(e.getItemSelectable()==panel2.box2){
    80                 text2.setText(""+priTbl[production][1]+"万元/台");
    81             }
    82             else if(e.getItemSelectable()==panel2.box3){
    83                 text2.setText(""+priTbl[production][2]+"万元/台");
    84             }
    85         }
    86     }
    87 }
    88 public class Example6_2 extends Applet{
    89     MyWindow myWin = new MyWindow("选择项目处理示例程序");
    90 }
    复制代码

    系列文章:

    http://www.cnblogs.com/Coda/p/4564462.html
     
  • 相关阅读:
    diary and html 文本颜色编辑,行距和其它编辑总汇
    bash coding to changeNames
    virtualbox ubuntu 网络连接 以及 连接 secureCRT
    linux 学习6 软件包安装
    linux 学习8 权限管理
    vim 使用2 转载 为了打开方便
    ubuntu
    linux 学习15 16 启动管理,备份和恢复
    linux 学习 14 日志管理
    linux 学习 13 系统管理
  • 原文地址:https://www.cnblogs.com/softidea/p/4564712.html
Copyright © 2011-2022 走看看