java选择框、单选框和单选按钮
选择框、单选框和单选按钮都是选择组件,选择组件有两种状态,一种是选中(on),另一种是未选中(off),它们提供一种简单的“on/off”选择功能,让用户在一组选择项目中作选择。
选择框
选择框(JCheckBox)的选中与否开状是一个小方框,被选中则在框中打勾。当在一个容器中有多个选择框,同时可以有多个选择框被选中,这样的选择框也称复选框。与选择框相关的接口是ItemListener,事件类是ItemEvent。
JCheckBox类常用的构造方法有以下3个:
1.JCheckBox():用空标题构造选择框。
2.JCheckBox(Strings):用给定的标题s构造选择框。
3.JCheckBox(Strings,booleanb):用给定的标题s构造选择框,参数b设置选中与否的初始状态。
JCheckBox类的其他常用方法如下:
1.getState():获取选择框的状态。
2.setState(booleanb):设置选择框的状态
3.getLabel():获取选择框的标题。
4.setLabel(Strings):设置选择框的标题。
5.isSelected():获取选择框是否被选中的状态。
6.itemStateChanged(ItemEvente):处理选择框事件的接口方法。
7.getItemSelectable():获取可选项,获取事件源。
8.addItemListener(ItemListenerl):为选择框设定监视器。
9.removeItemListener(ItemListenerl):移去选择框的监视器。
【例11-11】声明一个面板子类,面板子类对象有3个选择框。
classPanel1extendsJPanel{
JCheckBoxbox1,box2,box3;
Panel1(){
box1=newJCheckBox(“足球”);
box2=newJCheckBox(“排球”);
box2=newJCheckBox(“篮球”);
}
}
单选框
当在一个容器中放入多个选择框,且没有ButtonGroup对象将它们分组,则可以同时选中多个选择框。如果使用ButtonGroup对象将选择框分组,同一时刻组内的多个选择框只允许有一个被选中,称同一组内的选择框为单选框。单选框分组的方法是先创建ButtonGroup对象,然后将希望为同组的选择框添加到同一个ButtonGroup对象中。参见例6.2程序的面板子类Panel2的声明,组内有3个单选框。
单选按钮
单选按钮(JRadioButton)的功能与单选框相似。使用单选按钮的方法是将一些单选按钮用ButtonGroup对象分组,使同一组的单选按钮只允许有一个被选中。单选按钮与单选框的差异是显示的样式不同,单选按钮是一个圆形的按钮,单选框是一个小方框。
JRadioButton类的常用构造方法有以下几个:
1.JRadioButton():用空标题构造单选按钮。
2.JRadioButton(Strings):用给定的标题s构造单选按钮。
3.JRadioButton(Strings,booleanb):用给定的标题s构造单选按钮,参数b设置选中与否的初始状态。
单选按钮使用时需要使用ButtonGroup将单选按钮分组,单选按钮的分组方法是先创建对象,然后将同组的单选按钮添加到同一个ButtonGroup对象中。参见例6.2程序的子类panel1的声明,组内有3个单选按钮。
选择项目事件处理
用户对选择框或单选按钮做出选择后,程序应对这个选择作出必要的响应,程序为此要处理选择项目事件。选择项目处理程序的基本内容有:
1.监视选择项目对象的类要实现接口ItemListener,
2.程序要声明和建立选择对象,
3.为选择对象注册监视器,
4.编写处理选择项目事件的接口方法itemStateChanged(ItemEvente),在该方法内用getItemSelectable()方法获取事件源,并作相应处理。
【例11-12】处理选择项目事件的小应用程序。一个由3个单选按钮组成的产品选择组,当选中某个产品时,文本区将显示该产品的信息。一个由3个选择框组成的购买产品数量选择框组,当选择了购买数量后,在另一个文本框显示每台价格。
importjava.applet.*;
importjavax.swing.*;
importjava.awt.*;
importjava.awt.event.*;
classPanel1extendsJPanel{
JRadioButtonbox1,box2,box3;
ButtonGroupg;
Panel1(){
setLayout(newGridLayout(1,3));
g=newButtonGroup();
box1=newJRadioButton(MyWindow.fName[0]+"计算机",false);
box2=newJRadioButton(MyWindow.fName[1]+"计算机",false);
box3=newJRadioButton(MyWindow.fName[2]+"计算机",false);
g.add(box1);g.add(box2);g.add(box3);
add(box1);add(box2);add(box3);
add(newJLabel("计算机3选1"));
}
}
classPanel2extendsJPanel{
JCheckBoxbox1,box2,box3;
ButtonGroupg;
Panel2(){
setLayout(newGridLayout(1,3));
g=newButtonGroup();
box1=newJCheckBox("购买1台");
box2=newJCheckBox("购买2台");
box3=newJCheckBox("购买3台");
g.add(box1);g.add(box2);g.add(box3);
add(box1);add(box2);add(box3);
add(newJLabel("选择1、2或3"));
}
}
classMyWindowextendsJFrameimplementsItemListener{
Panel1panel1;
Panel2panel2;
JLabellabel1,label2;
JTextAreatext1,text2;
staticStringfName[]={"HP","IBM","DELL"};
staticdoublepriTbl[][]={{1.20,1.15,1.10},{1.70,1.65,1.60},{1.65,1.60,1.58}};
staticintproductin=-1;
MyWindow(Strings){
super(s);
Containercon=this.getContentPane();
con.setLayout(newGridLayout(3,2));
this.setLocation(100,100);
this.setSize(400,100);
panel1=newPanel1();panel2=newPanel2();
label1=newJLabel("产品介绍",JLabel.CENTER);
label2=newJLabel("产品价格",JLabel.CENTER);
text1=newJTextArea();text2=newJTextArea();
con.add(label1);con.add(label2);con.add(panel1);
con.add(panel2);con.add(text1);con.add(text2);
panel1.box1.addItemListener(this);
panel1.box2.addItemListener(this);
panel1.box3.addItemListener(this);
panel2.box1.addItemListener(this);
panel2.box2.addItemListener(this);
panel2.box3.addItemListener(this);
this.setVisible(true);this.pack();
}
publicvoiditemStateChanged(ItemEvente){//选项状态已改变
if(e.getItemSelectable()==panel1.box1){//获取可选项
production=0;
text1.setText(fName[0]+"公司生产");text2.setText("");
}
elseif(e.getItemSelectable()==panel1.box2){
production=1;
text1.setText(fName[1]+"公司生产");text2.setText("");
}
elseif(e.getItemSelectable()==panel1.box3){
production=2;
text1.setText(fName[2]+"公司生产");text2.setText("");
}
else{
if(production==-1)return;
if(e.getItemSelectable()==panel2.box1){
text2.setText(""+priTbl[production][0]+"万元/台");
}
elseif(e.getItemSelectable()==panel2.box2){
text2.setText(""+priTbl[production][1]+"万元/台");
}
elseif(e.getItemSelectable()==panel2.box3){
text2.setText(""+priTbl[production][2]+"万元/台");
}
}
}
}
publicclassExample6_2extendsApplet{
MyWindowmyWin=newMyWindow("选择项目处理示例程序");
}
以上所述就是本文的全部内容了,希望大家能够喜欢。